Discover why copying styles feels easy at first but becomes a nightmare as your project grows.
Why Extend limitations and gotchas in SASS? - Purpose & Use Cases
Imagine you have many button styles that share some colors and shapes. You try to copy and paste the same styles into each button's code.
If you change a color or shape, you must find and update every button style manually. This wastes time and can cause mistakes or inconsistent designs.
Sass's @extend lets you reuse styles by linking selectors. Change the shared style once, and all buttons update automatically.
.btn-primary { background: blue; border-radius: 5px; }
.btn-secondary { background: blue; border-radius: 5px; }.btn-primary { background: blue; border-radius: 5px; }
.btn-secondary { @extend .btn-primary; }You can write cleaner, DRY (Don't Repeat Yourself) styles that are easier to maintain and update.
A website with many buttons and alerts can share base styles. When the brand color changes, update one place and all related elements update instantly.
Manual copying of styles is slow and error-prone.
@extend links selectors to share styles efficiently.
Maintaining and updating styles becomes faster and safer.
Practice
@extend in Sass?Solution
Step 1: Understand what
@extenddoes@extendreuses styles by merging selectors in the final CSS output.Step 2: Identify the limitation
Merging selectors can cause unexpected combined selectors, leading to unwanted CSS rules.Final Answer:
It merges selectors which can cause unexpected CSS output -> Option AQuick Check:
@extendmerges selectors = A [OK]
@extend merges selectors, not duplicates styles [OK]- Thinking
@extendduplicates styles - Believing
@extendonly works with IDs - Assuming
@extendcannot be nested
%button-base inside a class .primary-button?Solution
Step 1: Recall placeholder selector syntax
Placeholder selectors start with%and must be referenced exactly with@extend %name.Step 2: Check each option
Only .primary-button { @extend %button-base; } uses@extend %button-basecorrectly.Final Answer:
.primary-button { @extend %button-base; } -> Option BQuick Check:
Use@extend %placeholdersyntax = C [OK]
- Omitting the % in the extend statement
- Using wrong keywords like 'extend' without @
- Confusing ID (#) with placeholder (%) selectors
.alert { color: red; }
.error { @extend .alert; font-weight: bold; }What will be the compiled CSS output?
Solution
Step 1: Understand
The@extendmerges selectors.errorselector extends.alert, so both selectors share thecolor: red;style.Step 2: Check the output structure
The merged selector.alert, .errorhascolor: red;, and.errorhas its ownfont-weight: bold;.Final Answer:
.alert, .error { color: red; } .error { font-weight: bold; } -> Option AQuick Check:
Selectors merged, styles combined = D [OK]
- Expecting duplicated styles instead of merged selectors
- Thinking extended styles appear only inside the extending selector
- Ignoring that
@extendmerges selectors
.btn { color: blue; }
.special-btn { @extend .btn; background: yellow; }
.btn-primary { @extend .btn; }Why might the compiled CSS include unexpected selectors combined with
.btn-primary?Solution
Step 1: Understand how
All selectors extending@extendmerges selectors.btnare merged into one combined selector group in the output CSS.Step 2: Identify why unexpected combinations happen
Since both.special-btnand.btn-primaryextend.btn, their selectors get merged, causing combined selectors.Final Answer:
Because@extendmerges all selectors that extend.btninto one group -> Option CQuick Check:
@extendmerges selectors = B [OK]
- Thinking
@extendduplicates styles - Believing
@extendonly works with placeholders - Assuming
@extendcannot be used with classes
@extend. Which approach is best?Solution
Step 1: Understand placeholder selectors
Placeholder selectors start with%and do not output CSS unless extended.Step 2: Why use placeholders with
Extending placeholders avoids unwanted CSS output because placeholders themselves don't generate CSS.@extendStep 3: Evaluate other options
Extending regular classes merges selectors and outputs all styles, duplicating styles is inefficient, and limiting@extendto media queries doesn't solve unwanted output.Final Answer:
Use placeholder selectors (e.g.,%base-style) and extend them -> Option DQuick Check:
Placeholders prevent unwanted CSS output = A [OK]
@extend to avoid extra CSS [OK]- Extending regular classes causing large CSS output
- Copy-pasting styles instead of reusing
- Misusing
@extendinside media queries
Ready to prove it?
Pass the test and this pattern moves unknown
