0
0
SASSmarkup~15 mins

Extending vs mixing comparison in SASS - Trade-offs & Expert Analysis

Choose your learning style9 modes available
Overview - Extending vs mixing comparison
What is it?
In Sass, 'extending' and 'mixing' are two ways to reuse styles in your CSS. Extending lets one selector inherit styles from another, sharing the same CSS rules. Mixing means including a set of styles defined in a reusable block called a mixin, which can also accept inputs to customize the styles. Both help keep your styles organized and avoid repeating code.
Why it matters
Without extending or mixing, you would write the same CSS styles many times, making your code long, hard to maintain, and prone to mistakes. These features save time and reduce errors by letting you write styles once and reuse them. They also help keep your website consistent and easier to update.
Where it fits
Before learning this, you should understand basic CSS selectors and how Sass variables and nesting work. After mastering extending and mixing, you can explore advanced Sass features like functions, control directives, and creating design systems with reusable components.
Mental Model
Core Idea
Extending shares existing styles by linking selectors, while mixing copies reusable style blocks into selectors, sometimes with customization.
Think of it like...
Think of extending like sharing a recipe card with friends so they cook the same dish, while mixing is like giving them a cooking kit with ingredients and instructions they can adjust to taste.
Selectors and styles flow:

  [Base Selector]─────┐
        │            │
        ▼            ▼
  [Extending Selector]  [Mixin]
        │                 │
        ▼                 ▼
  Shared CSS rules   Copied CSS rules

Extending links selectors to share rules.
Mixing copies rules into selectors.
Build-Up - 7 Steps
1
FoundationWhat is Sass Extending
🤔
Concept: Introducing the @extend feature to share styles between selectors.
In Sass, you can write: .button { background: blue; color: white; } .alert { @extend .button; border: 1px solid red; } This means .alert will get all styles from .button plus its own.
Result
The CSS output combines selectors: .button, .alert { background: blue; color: white; } .alert { border: 1px solid red; } Both .button and .alert share the blue background and white text.
Understanding that @extend merges selectors to share styles helps you write less CSS and keep styles consistent.
2
FoundationWhat is Sass Mixing
🤔
Concept: Introducing mixins as reusable style blocks that can be included anywhere.
You define a mixin: @mixin button-styles { background: blue; color: white; } Then use it: .alert { @include button-styles; border: 1px solid red; } This copies the styles inside the mixin into .alert.
Result
The CSS output is: .alert { background: blue; color: white; border: 1px solid red; } .alert has all styles copied from the mixin plus its own.
Knowing mixins copy styles lets you customize and reuse style blocks flexibly.
3
IntermediateHow Extending Affects CSS Output
🤔Before reading on: Do you think extending duplicates CSS rules or merges selectors? Commit to your answer.
Concept: Extending merges selectors in the final CSS instead of duplicating rules.
When you extend a selector, Sass finds all rules for the original selector and adds the extending selector to those rules. This means selectors share the same CSS block, reducing duplication. Example: .card { padding: 10px; } .alert { @extend .card; border: 1px solid red; } Output: .card, .alert { padding: 10px; } .alert { border: 1px solid red; } No repeated padding rules.
Result
CSS is smaller and selectors are grouped, which can improve performance and maintainability.
Understanding selector merging explains why extending can reduce CSS size but may cause unexpected selector combinations.
4
IntermediateHow Mixing Affects CSS Output
🤔Before reading on: Does mixing copy styles or merge selectors? Commit to your answer.
Concept: Mixing copies the styles inside the mixin into each place it is included, duplicating CSS rules.
Each time you include a mixin, Sass inserts its styles directly into that selector's block. Example: @mixin card { padding: 10px; } .alert { @include card; border: 1px solid red; } .notice { @include card; background: yellow; } Output: .alert { padding: 10px; border: 1px solid red; } .notice { padding: 10px; background: yellow; } Padding is duplicated in both selectors.
Result
CSS size grows with each mixin use, but selectors remain separate and clear.
Knowing mixins copy styles helps you balance reuse with CSS size and clarity.
5
IntermediateCustomizing Mixins with Arguments
🤔Before reading on: Can mixins accept inputs to change styles? Commit to yes or no.
Concept: Mixins can take parameters to customize the styles they add.
You can define mixins with arguments: @mixin button($bg-color, $text-color) { background: $bg-color; color: $text-color; } .alert { @include button(red, white); border: 1px solid darkred; } .success { @include button(green, white); } Output: .alert { background: red; color: white; border: 1px solid darkred; } .success { background: green; color: white; } Each selector gets customized styles.
Result
Mixins become flexible tools to create variations without repeating code.
Understanding mixin parameters unlocks powerful, reusable style patterns.
6
AdvancedLimitations and Risks of Extending
🤔Before reading on: Does extending always produce clean CSS? Commit to yes or no.
Concept: Extending can cause unexpected selector combinations and CSS bloat if used carelessly.
Because extending merges selectors, it can create large combined selectors that match more elements than intended. Example: %button-base { padding: 10px; } .button { @extend %button-base; } .alert { @extend %button-base; } Output: .button, .alert { padding: 10px; } If you extend many selectors from the same base, the combined selector list grows, which can slow browsers. Also, extending non-placeholder selectors can duplicate unwanted styles.
Result
Extending can cause CSS selectors to become complex and hard to debug.
Knowing extending merges selectors warns you to use it carefully to avoid bloated or confusing CSS.
7
ExpertChoosing Between Extending and Mixing
🤔Before reading on: Which is better for small reusable styles, extending or mixing? Commit to your choice.
Concept: Choosing between extend and mixin depends on tradeoffs: CSS size, selector complexity, and customization needs.
Use @extend when: - You want to share simple styles without duplication. - You want to keep CSS size small. - You don't need to customize styles per use. Use @mixin when: - You need to customize styles with parameters. - You want to avoid complex selector combinations. - You accept some CSS duplication for clarity. In large projects, mixing is often safer and more predictable, while extending can optimize CSS size if used carefully. Advanced tools analyze CSS output to decide the best approach.
Result
You make informed decisions balancing maintainability, performance, and flexibility.
Understanding the tradeoffs helps you write scalable, maintainable Sass code suited to your project's needs.
Under the Hood
Sass processes @extend by finding all CSS rules for the target selector and adding the extending selector to those rules, merging selectors into combined groups. This happens during compilation before CSS is output. For @mixin, Sass copies the mixin's style declarations directly into each place it is included, inserting the styles inline. Mixins can accept parameters, which Sass replaces with actual values during compilation. Extending affects the selector tree, while mixing affects the declaration blocks.
Why designed this way?
Extending was designed to reduce CSS duplication by merging selectors, saving file size and improving performance. Mixing was created to allow reusable style blocks with customization, trading off some duplication for flexibility. The two approaches reflect different needs: sharing styles vs. copying styles. Alternatives like functions or placeholders exist, but extending and mixing provide clear, readable syntax for common reuse patterns.
Sass Compilation Flow:

Source Sass File
  │
  ├─ @extend: Find selectors to merge
  │      │
  │      ▼
  │  Merge selectors in CSS rules
  │
  ├─ @mixin: Copy declarations
  │      │
  │      ▼
  │  Insert declarations inline
  │
  ▼
Output CSS File

Extending changes selectors grouping.
Mixing inserts declarations directly.
Myth Busters - 4 Common Misconceptions
Quick: Does @extend copy styles into new selectors or merge selectors? Commit to your answer.
Common Belief:Many think @extend copies styles like mixins do.
Tap to reveal reality
Reality:@extend merges selectors so they share the same CSS rules instead of copying styles.
Why it matters:Misunderstanding this leads to unexpected CSS output with large combined selectors or missing styles.
Quick: Can mixins cause CSS duplication? Commit yes or no.
Common Belief:Some believe mixins never increase CSS size because they reuse code.
Tap to reveal reality
Reality:Mixins copy styles each time they are included, which can increase CSS size if overused.
Why it matters:Ignoring this can cause bloated CSS files that slow down page loading.
Quick: Does extending work well with complex selectors? Commit yes or no.
Common Belief:People often think extending works perfectly with any selector type.
Tap to reveal reality
Reality:Extending complex or nested selectors can create unexpected combined selectors that match unintended elements.
Why it matters:This can cause styling bugs that are hard to debug and fix.
Quick: Can mixins accept parameters to customize styles? Commit yes or no.
Common Belief:Some think mixins are static and cannot be customized.
Tap to reveal reality
Reality:Mixins can take arguments, making them flexible and reusable with different values.
Why it matters:Not knowing this limits how effectively you can reuse and customize styles.
Expert Zone
1
Extending placeholder selectors (%name) avoids unintended selector merging and keeps CSS cleaner.
2
Mixins can include control directives like @if and @for, enabling dynamic style generation.
3
Using @extend inside nested selectors can cause selector explosion, so it requires careful planning.
When NOT to use
Avoid @extend when selectors are complex or when you need highly customized styles; prefer mixins or functions instead. Avoid mixins when CSS size is critical and styles are simple and shared; prefer extending or placeholders. For very dynamic styles, consider CSS custom properties or utility-first CSS frameworks.
Production Patterns
In production, teams often use placeholders with @extend for base styles to minimize CSS size, and mixins with parameters for components needing variation. Tools like stylelint help catch misuse. Large projects combine both, balancing maintainability and performance.
Connections
Object-Oriented Programming Inheritance
Extending in Sass is similar to class inheritance where a subclass inherits properties from a parent class.
Understanding inheritance in programming helps grasp how extending shares styles by linking selectors.
Function Calls in Programming
Mixins behave like functions that can be called with arguments to produce customized output.
Knowing how functions accept parameters and return results clarifies how mixins provide reusable, customizable styles.
Recipe Sharing in Cooking
Extending is like sharing a recipe card exactly, while mixing is like giving a cooking kit with ingredients to adjust.
This connection helps understand the difference between sharing fixed styles and copying customizable style blocks.
Common Pitfalls
#1Using @extend on regular selectors causing unexpected selector combinations.
Wrong approach:.alert { @extend .button; } .button { background: blue; } /* This merges selectors and can cause .alert to inherit unwanted styles or create large selector groups */
Correct approach:%button-base { background: blue; } .alert { @extend %button-base; } /* Using placeholder selectors avoids merging with unrelated selectors */
Root cause:Misunderstanding that @extend merges selectors, not just copies styles.
#2Overusing mixins leading to large CSS files with duplicated styles.
Wrong approach:@mixin card { padding: 10px; border: 1px solid black; } .alert { @include card; } .notice { @include card; } /* Padding and border duplicated in both selectors */
Correct approach:%card-base { padding: 10px; border: 1px solid black; } .alert { @extend %card-base; } .notice { @extend %card-base; } /* Styles shared via extending, CSS size reduced */
Root cause:Not balancing reuse methods and ignoring CSS output size.
#3Not using mixin parameters, causing repeated similar mixins.
Wrong approach:@mixin button-blue { background: blue; color: white; } @mixin button-red { background: red; color: white; } .alert { @include button-red; } .success { @include button-blue; } /* Multiple similar mixins instead of one flexible one */
Correct approach:@mixin button($bg, $color) { background: $bg; color: $color; } .alert { @include button(red, white); } .success { @include button(blue, white); } /* One mixin with parameters for flexibility */
Root cause:Not leveraging mixin arguments to reduce duplication.
Key Takeaways
Extending shares styles by merging selectors, reducing CSS duplication but can create complex selector groups.
Mixing copies reusable style blocks into selectors, allowing customization but increasing CSS size with each use.
Use placeholders with extending to avoid unintended selector merging and keep CSS clean.
Mixins with parameters provide flexible, reusable styles that adapt to different needs.
Choosing between extending and mixing depends on balancing CSS size, maintainability, and customization.