Discover how one small change can update many styles at once without repeating yourself!
Why Chained extensions in SASS? - Purpose & Use Cases
Imagine you have several button styles that share common parts. You write the same CSS rules again and again for each button type.
When you want to change a shared style, you must update every button style manually. This wastes time and can cause mistakes or inconsistencies.
Chained extensions let you write shared styles once and extend them step-by-step. This keeps your code DRY and easy to update.
.btn-primary { color: white; background: blue; padding: 1rem; }
.btn-secondary { color: white; background: gray; padding: 1rem; }%btn-base { padding: 1rem; color: white; }
.btn-primary { @extend %btn-base; background: blue; }
.btn-secondary { @extend %btn-base; background: gray; }You can build complex style hierarchies that are easy to maintain and update with just a few changes.
In a website with many button types, chained extensions let you update padding or font color once and see it apply everywhere instantly.
Writing shared styles once saves time and reduces errors.
Chained extensions let styles build on each other cleanly.
Maintaining large style systems becomes simpler and faster.
Practice
@extend in Sass allow you to do?Solution
Step 1: Understand
@extendbasics@extendlets one selector reuse styles from another selector or placeholder.Step 2: Recognize chaining effect
Chaining means a selector extends another selector that itself extends a base style, passing styles along.Final Answer:
Make one selector inherit styles from another that already extends a base style -> Option BQuick Check:
Chained@extend= inherit styles step-by-step [OK]
- Confusing
@extendwith creating new CSS properties - Thinking
@extendcompiles to JavaScript - Believing
@extendadds inline HTML styles
Solution
Step 1: Identify correct
The correct syntax uses@extendsyntax@extendkeyword with a percent sign for placeholders and dot for classes.Step 2: Check chaining format
Chaining means one selector extends a placeholder, another extends that selector, both using@extend.Final Answer:
.button {@extend %base;} .primary {@extend .button;} -> Option CQuick Check:
Correct@extendsyntax uses @extend keyword and proper selector symbols [OK]
- Omitting the @ symbol before extend
- Using 'extends' instead of 'extend'
- Missing % for placeholders or . for classes
%base { color: blue; }
.button {@extend %base; font-weight: bold;}
.primary {@extend .button; background: yellow; }What CSS will be generated for
.primary?Solution
Step 1: Trace
.button extends %base, so it gets@extendchaincolor: blue;. .primary extends .button, so it inherits bothcolor: blue;andfont-weight: bold;.Step 2: Combine styles for .primary
.primary addsbackground: yellow;to inherited styles, so final CSS includes all three properties.Final Answer:
.primary { color: blue; font-weight: bold; background: yellow; } -> Option AQuick Check:
Chained extensions combine all inherited styles [OK]
- Ignoring inherited styles from extended selectors
- Only applying direct styles of .primary
- Confusing placeholders with classes in output
%base { color: red; }
.button { @extend %base }
.primary { @extend .button; }Solution
Step 1: Check
Every@extendsyntax@extendstatement must end with a semicolon. The line@extend %baseis missing it.Step 2: Verify chaining and placeholders
Extending placeholders and chaining extensions are valid in Sass, so no error there.Final Answer:
Missing semicolon after@extend %base-> Option DQuick Check:
Always end@extendwith semicolon [OK]
- Thinking chaining extensions is invalid
- Believing placeholders can't be extended
- Forgetting semicolons after statements
.primary and .secondary. Using chained extensions, which Sass code correctly shares the base styles and adds variant colors?Solution
Step 1: Use placeholder for base styles
Base styles should be in a placeholder (%button-base) to avoid generating extra CSS.Step 2: Chain extensions for variants
.button extends %button-base, then .primary and .secondary extend .button, inheriting base styles and adding colors.Final Answer:
%button-base { padding: 1rem; border-radius: 0.5rem; } .button {@extend %button-base;} .primary {@extend .button; background: blue;} .secondary {@extend .button; background: gray;} -> Option AQuick Check:
Use placeholders and chain extensions for clean, reusable styles [OK]
- Using class selectors instead of placeholders for base styles
- Not chaining extensions, repeating base styles
- Extending classes that don't extend placeholders
Ready to prove it?
Pass the test and this pattern moves unknown
