Chained extensions let you reuse styles from one selector to another in a chain. This helps keep your CSS neat and avoids repeating code.
Chained extensions in SASS
%base-style { property: value; }
.selector1 { @extend %base-style; }
.selector2 { @extend .selector1; }Use @extend to inherit styles from another selector or placeholder.
Chained extensions mean one selector extends another that already extends a base style.
.btn-primary extends the base button style. Then .btn-special extends .btn-primary, so it gets all styles from both.%button-base {
padding: 1rem;
border-radius: 0.5rem;
background-color: blue;
color: white;
}
.btn-primary {
@extend %button-base;
font-weight: bold;
}
.btn-special {
@extend .btn-primary;
background-color: green;
}%text-base {
font-family: Arial, sans-serif;
font-size: 1rem;
}
.title {
@extend %text-base;
font-weight: bold;
}
.subtitle {
@extend .title;
font-style: italic;
}This Sass code defines a base box style with padding, border, and background color. Then .box-primary extends it and adds bold text. .box-alert and .box-info extend .box-primary but change background colors. This shows chained extensions in action.
In HTML, three boxes will show with shared padding and border, but different background colors and bold text.
@use 'sass:color'; %base-box { padding: 1rem; border: 2px solid black; border-radius: 0.5rem; background-color: color.scale(blue, $lightness: 20%); color: white; } .box-primary { @extend %base-box; font-weight: bold; } .box-alert { @extend .box-primary; background-color: color.scale(red, $lightness: 30%); } .box-info { @extend .box-primary; background-color: color.scale(green, $lightness: 30%); } /* HTML to test styles */ /* <div class="box-primary">Primary Box</div> <div class="box-alert">Alert Box</div> <div class="box-info">Info Box</div> */
Chained extensions help avoid repeating styles but be careful not to create very long chains, which can be hard to follow.
Use placeholders (starting with %) for base styles to avoid generating extra CSS selectors.
Check the compiled CSS in your browser DevTools to see how extensions combine selectors.
Chained extensions let one selector inherit styles from another that already extends a base style.
This keeps CSS clean and easy to update.
Use @extend with placeholders or selectors to share styles step-by-step.
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
