Using @extend in Sass helps reuse styles but can create very long CSS selectors. Avoiding this bloat keeps your CSS smaller and faster to load.
Avoiding selector bloat from @extend in SASS
Start learning this pattern below
Jump into concepts and practice - no test required
@extend <selector>;@extend copies styles from one selector to another.
It combines selectors in the output CSS, which can make selectors very long.
.button styles with .primary using @extend..button { padding: 1rem; background: blue; } .primary { @extend .button; color: white; }
.error gets all styles from .alert plus its own..alert { border: 1px solid red; } .error { @extend .alert; background: pink; }
This example shows how @extend on normal classes creates combined selectors, which can get long. Using a placeholder selector (starting with %) avoids adding extra selectors to the output CSS, reducing bloat.
@use 'sass:color'; .button { padding: 1rem; background-color: blue; color: white; } .primary { @extend .button; font-weight: bold; } .secondary { @extend .button; background-color: color.scale(blue, $lightness: 30%); } /* Avoid selector bloat by using placeholders */ %button-base { padding: 1rem; background-color: blue; color: white; } .primary-alt { @extend %button-base; font-weight: bold; } .secondary-alt { @extend %button-base; background-color: color.scale(blue, $lightness: 30%); }
Using placeholder selectors (like %button-base) helps avoid adding extra selectors to your CSS output.
Be careful: @extend merges selectors, so extending many selectors can create very long combined selectors.
Consider using mixins if you want to avoid selector bloat but still reuse styles.
@extend shares styles but can create long combined selectors.
Use placeholder selectors to avoid adding extra selectors and reduce CSS size.
Mixins are an alternative to @extend when you want to avoid selector bloat.
Practice
@extend in Sass without placeholders?Solution
Step 1: Understand what
@extenddoes@extendshares styles by combining selectors in the output CSS.Step 2: Identify the problem with combining selectors
When many selectors are combined, the CSS file grows longer and harder to maintain, called selector bloat.Final Answer:
It creates long combined selectors causing selector bloat. -> Option AQuick Check:
@extend causes selector bloat = A [OK]
- Thinking @extend duplicates properties instead of combining selectors
- Confusing @extend with mixins
- Believing @extend only works inside mixins
Solution
Step 1: Recall placeholder selector syntax
Placeholder selectors start with a percent sign (%) in Sass.Step 2: Match the syntax to the options
Only %button { color: blue; } uses %button, which is the correct placeholder syntax.Final Answer:
%button { color: blue; } -> Option AQuick Check:
Placeholder selector syntax = %name [OK]
- Using class selector syntax (.) instead of %
- Using ID selector (#) for placeholders
- Confusing & with placeholder syntax
%btn { color: red; }
.primary { @extend %btn; }
.secondary { @extend %btn; }What will the compiled CSS look like?
Solution
Step 1: Understand placeholder selectors output
Placeholder selectors do not appear in the compiled CSS; only selectors that extend them appear combined.Step 2: Combine selectors that extend the placeholder
.primary and .secondary both extend %btn, so they are combined into one selector with the shared styles.Final Answer:
.primary, .secondary { color: red; } -> Option BQuick Check:
Placeholder selectors disappear, extended selectors combine [OK]
- Expecting %btn to appear in CSS
- Thinking styles duplicate for each selector
- Confusing combined selectors with separate blocks
@extend without placeholders?.btn { color: green; }
.primary { @extend .btn; }
.secondary { @extend .btn; }Solution
Step 1: Understand @extend with normal selectors
Extending a normal class merges selectors including the original class, creating longer combined selectors.Step 2: Identify the problem caused
This merging causes selector bloat because the original selector (.btn) stays in output and combines with all extending selectors.Final Answer:
It causes selector bloat by combining all selectors including .btn. -> Option CQuick Check:
@extend without placeholders causes selector bloat [OK]
- Thinking @extend requires placeholders
- Expecting duplicated styles instead of combined selectors
- Believing styles won't apply to extending selectors
@extend. Which approach is best?Solution
Step 1: Understand selector bloat causes
@extend merges selectors, which can cause long combined selectors and bloat.Step 2: Compare placeholders and mixins
Placeholders reduce bloat but still combine selectors. Mixins copy styles without merging selectors, avoiding bloat.Step 3: Choose the best approach to avoid bloat
Using mixins copies styles directly, preventing selector bloat while sharing styles.Final Answer:
Use a mixin to include styles in each button class. -> Option DQuick Check:
Mixins avoid selector bloat better than @extend [OK]
- Thinking placeholders fully prevent bloat
- Using @extend on normal selectors causing bloat
- Copy-pasting styles manually instead of using mixins
