0
0
SASSmarkup~15 mins

@extend directive in SASS - Deep Dive

Choose your learning style9 modes available
Overview - @extend directive
What is it?
The @extend directive in Sass lets you share styles between selectors by making one selector inherit the styles of another. Instead of copying styles manually, you tell Sass to extend a selector, so it combines their styles in the final CSS. This helps keep your stylesheets clean and avoids repeating the same code. It works by merging selectors that share the same styles.
Why it matters
Without @extend, you would have to copy and paste the same styles in many places, which makes your CSS bigger and harder to maintain. If you want to change a style, you’d have to update it everywhere manually. @extend solves this by letting you write styles once and reuse them safely, saving time and reducing mistakes. This makes your website faster to build and easier to update.
Where it fits
Before learning @extend, you should understand basic CSS selectors and how Sass variables and nesting work. After mastering @extend, you can explore other Sass features like mixins and functions, which offer different ways to reuse styles and add logic to your stylesheets.
Mental Model
Core Idea
@extend lets one CSS selector borrow the styles of another by combining their selectors in the final CSS output.
Think of it like...
Imagine you have a recipe card for chocolate cake. Instead of rewriting the whole recipe every time you want to make a chocolate cake with nuts or frosting, you just say 'use the chocolate cake recipe' and add your extra steps. @extend is like referencing the original recipe instead of copying it.
Original CSS selectors and styles:

  .button {
    color: white;
    background: blue;
  }

  .alert {
    color: red;
  }

Using @extend:

  .special-button {
    @extend .button;
    font-weight: bold;
  }

Final CSS output:

  .button, .special-button {
    color: white;
    background: blue;
  }

  .special-button {
    font-weight: bold;
  }

  .alert {
    color: red;
  }
Build-Up - 7 Steps
1
FoundationBasic CSS selectors and styles
🤔
Concept: Understanding how CSS selectors target elements and apply styles.
CSS uses selectors like .class or #id to apply styles to HTML elements. For example, .button { color: white; } makes all elements with class 'button' have white text.
Result
You can style elements on a webpage by writing CSS rules with selectors.
Knowing how selectors work is essential before learning how to share or reuse styles.
2
FoundationIntroduction to Sass and nesting
🤔
Concept: Learning how Sass lets you write CSS more cleanly with nesting and variables.
Sass is a tool that helps write CSS faster and cleaner. Nesting means writing selectors inside others to show hierarchy, like: .nav { ul { margin: 0; } } This compiles to .nav ul { margin: 0; } in CSS.
Result
You write less code and see clearer structure in your stylesheets.
Nesting helps organize styles, setting the stage for more advanced reuse techniques like @extend.
3
IntermediateUsing @extend to share styles
🤔Before reading on: do you think @extend copies styles or merges selectors? Commit to your answer.
Concept: @extend lets one selector inherit styles from another by merging selectors in the output CSS.
Instead of copying styles, you write: .alert { color: red; } .error { @extend .alert; font-weight: bold; } Sass combines selectors so both .alert and .error share the red color style.
Result
The compiled CSS has .alert, .error { color: red; } and .error { font-weight: bold; }
Understanding that @extend merges selectors helps you write DRY (Don't Repeat Yourself) CSS and keeps your output smaller.
4
IntermediateHow @extend affects selector groups
🤔Before reading on: if you extend a selector inside a nested rule, do you think the output selector stays nested or flattens? Commit to your answer.
Concept: @extend merges selectors exactly where the original selector appears, affecting how selectors group in the final CSS.
If you have nested selectors like: .nav { ul { @extend .list; } } And .list { margin: 0; }, the output CSS will combine .list and .nav ul selectors, flattening the nesting.
Result
Final CSS might look like .list, .nav ul { margin: 0; }
Knowing how @extend merges selectors helps avoid unexpected CSS output and keeps styles predictable.
5
IntermediateLimitations and pitfalls of @extend
🤔Before reading on: do you think @extend works with media queries and pseudo-classes without issues? Commit to your answer.
Concept: @extend has limitations, especially with complex selectors, media queries, and pseudo-classes, which can cause unexpected CSS or errors.
For example, extending a selector inside a media query or with pseudo-classes like :hover can produce duplicated or broken CSS. Sass tries to merge selectors but sometimes creates large selector groups or invalid CSS.
Result
You might see very long selectors or styles that don't apply as expected.
Understanding these limits helps you decide when to use @extend or choose alternatives like mixins.
6
AdvancedHow @extend reduces CSS size and improves maintenance
🤔Before reading on: do you think @extend always reduces CSS size compared to mixins? Commit to your answer.
Concept: @extend reduces CSS size by merging selectors instead of duplicating styles, but it depends on usage patterns.
Mixins copy styles every time they are used, increasing CSS size. @extend merges selectors, so styles appear once with multiple selectors. However, if overused or used with complex selectors, @extend can create large selector groups that hurt performance.
Result
Proper use of @extend leads to smaller CSS and easier updates, but misuse can cause bloated selectors.
Knowing when @extend helps or hurts CSS size is key to writing efficient stylesheets.
7
ExpertInternal selector merging and specificity effects
🤔Before reading on: does @extend change CSS specificity or just selectors? Commit to your answer.
Concept: @extend merges selectors but does not increase specificity; it can affect how styles override each other in subtle ways.
When Sass merges selectors, it creates combined selector groups. This does not add specificity but can change which styles apply if multiple rules target the same element. Also, @extend does not copy pseudo-elements or pseudo-classes fully, which can cause unexpected behavior.
Result
You may see style conflicts or overrides that are hard to debug if you don't understand how @extend affects specificity and selector groups.
Understanding the internal merging and specificity impact helps avoid subtle bugs and write predictable CSS.
Under the Hood
@extend works by scanning your Sass code for selectors you want to extend. It then finds all places where the original selector appears and merges the extending selector into those places. This means the final CSS has combined selectors sharing the same styles instead of duplicated style blocks. Internally, Sass builds a map of selectors and merges them during compilation, carefully handling nesting and media queries where possible.
Why designed this way?
The @extend directive was designed to reduce repetition and CSS file size by sharing styles without copying. Early CSS lacked variables or functions, so developers repeated code. Sass introduced @extend to merge selectors, making stylesheets easier to maintain and faster to load. Alternatives like mixins copy styles, increasing size, so @extend offers a more efficient reuse method. However, merging selectors is complex, so Sass balances power with some limitations.
Sass source code:

  .base {
    color: blue;
  }

  .special {
    @extend .base;
    font-weight: bold;
  }

Compilation process:

  [Find .base selector] ←───┐
                            │
  [Find .special @extend] ──┤──> [Merge selectors: .base, .special]
                            │
  [Generate combined CSS rule]

Final CSS:

  .base, .special {
    color: blue;
  }

  .special {
    font-weight: bold;
  }
Myth Busters - 4 Common Misconceptions
Quick: Does @extend copy styles into the new selector or merge selectors? Commit to your answer.
Common Belief:Many think @extend copies the styles from one selector into another like a copy-paste.
Tap to reveal reality
Reality:@extend does not copy styles; it merges selectors so they share the same style block in the final CSS.
Why it matters:Thinking it copies styles leads to expecting duplicated CSS and misunderstanding how changes affect all selectors.
Quick: Can @extend be safely used inside media queries without issues? Commit to your answer.
Common Belief:Some believe @extend works perfectly inside media queries and nested selectors without side effects.
Tap to reveal reality
Reality:@extend inside media queries can cause duplicated or invalid CSS because selector merging across media queries is complex.
Why it matters:Using @extend incorrectly in media queries can break responsive designs or cause bloated CSS.
Quick: Does @extend increase CSS specificity of the extending selector? Commit to your answer.
Common Belief:People often think @extend increases specificity because it adds selectors to the rule.
Tap to reveal reality
Reality:@extend merges selectors but does not increase specificity; specificity depends on the original selectors.
Why it matters:Misunderstanding specificity can cause unexpected style overrides and debugging headaches.
Quick: Is @extend always better than mixins for reusing styles? Commit to your answer.
Common Belief:Many assume @extend is always the best way to reuse styles because it reduces CSS size.
Tap to reveal reality
Reality:While @extend reduces duplication, it can create complex selector groups and unexpected CSS; mixins offer safer, more predictable reuse at the cost of larger CSS.
Why it matters:Choosing @extend blindly can lead to maintenance problems and performance issues.
Expert Zone
1
Using @extend with placeholder selectors (%placeholder) avoids generating unwanted CSS selectors and keeps output clean.
2
Extending selectors with pseudo-classes or pseudo-elements can produce unexpected results because @extend merges only the base selectors.
3
The order of @extend statements affects the final CSS output and can influence which styles override others.
When NOT to use
Avoid @extend when working with complex selectors involving pseudo-classes, media queries, or when you need to add unique styles per selector. Use mixins or functions instead for safer, more predictable style reuse.
Production Patterns
In real projects, developers use @extend mainly with placeholder selectors to share base styles without generating extra CSS. They combine @extend with mixins for flexible style reuse and carefully avoid extending selectors inside media queries to prevent bloated CSS.
Connections
Mixin (Sass)
Alternative approach for style reuse
Knowing @extend and mixins together helps choose the best reuse method: @extend merges selectors to reduce CSS size, while mixins copy styles for flexibility.
DRY Principle (Software Engineering)
Applies the 'Don't Repeat Yourself' rule to CSS
Understanding @extend as a tool to avoid repeating styles connects to the broader software principle of writing maintainable, reusable code.
Database Normalization
Similar pattern of avoiding duplication
Just like @extend avoids repeating CSS styles by referencing shared selectors, database normalization avoids repeating data by referencing shared tables, showing a cross-domain pattern of efficient reuse.
Common Pitfalls
#1Extending a regular class instead of a placeholder causes unwanted CSS selectors.
Wrong approach:.button { color: blue; } .special { @extend .button; font-weight: bold; }
Correct approach:%button { color: blue; } .special { @extend %button; font-weight: bold; }
Root cause:Extending regular classes duplicates selectors in CSS output, creating extra unwanted styles.
#2Using @extend inside media queries leads to duplicated or broken CSS.
Wrong approach:@media (max-width: 600px) { .mobile { @extend .button; } } .button { color: blue; }
Correct approach:.button { color: blue; } @media (max-width: 600px) { .mobile { color: blue; } }
Root cause:@extend does not merge selectors across media queries well, causing invalid or duplicated CSS.
#3Expecting @extend to increase specificity and override other styles.
Wrong approach:.alert { color: red; } .error { @extend .alert; color: black; }
Correct approach:.alert { color: red; } .error { color: black; }
Root cause:@extend merges selectors but does not increase specificity; later rules override earlier ones based on CSS cascade.
Key Takeaways
@extend lets one selector share styles by merging selectors in the final CSS, avoiding style duplication.
It helps keep stylesheets smaller and easier to maintain but has limits with complex selectors and media queries.
Using placeholder selectors with @extend prevents unwanted CSS output and keeps your styles clean.
Understanding how @extend affects selector merging and specificity is key to avoiding subtle bugs.
Choosing between @extend and mixins depends on your needs for CSS size versus flexibility.