Discover how a small Sass trick can save you hours of repetitive work!
Why extending reduces duplication in SASS - The Real Reasons
Imagine you are styling a website with many buttons that share similar colors and fonts. You write the same color and font styles again and again for each button type.
When you want to change the button color, you have to find and update every single place manually. This is slow and easy to miss, causing inconsistent styles.
Using extending in Sass lets you write shared styles once and reuse them. Change the shared style once, and all buttons update automatically.
.btn-primary { color: white; background: blue; font-weight: bold; }
.btn-secondary { color: white; background: gray; font-weight: bold; }%button-base { color: white; font-weight: bold; }
.btn-primary { @extend %button-base; background: blue; }
.btn-secondary { @extend %button-base; background: gray; }You can keep your styles DRY (Don't Repeat Yourself), making your code easier to maintain and update.
On a large website, you can create a base button style and extend it for all button variations, so changing the base updates every button instantly.
Writing shared styles once saves time and effort.
Extending reduces errors from repeated code.
Maintaining and updating styles becomes simple and fast.
Practice
@extend in Sass?Solution
Step 1: Understand the purpose of
@extend@extendallows one selector to inherit styles from another, avoiding repetition.Step 2: Identify the benefit
This sharing reduces duplicated CSS rules, making code cleaner and smaller.
Final Answer:
It reduces repeated CSS by sharing styles between selectors. -> Option AQuick Check:
Extending reduces duplication = B [OK]
@extend shares styles to avoid repeats [OK]- Thinking
@extendadds new styles instead of sharing - Confusing
@extendwith mixins - Believing
@extendcompiles to JavaScript
%button-base in Sass?Solution
Step 1: Recall correct
@extendsyntaxThe correct keyword is
@extendwith an '@' symbol and a semicolon inside the block.Step 2: Match syntax with options
.btn {@extend %button-base;} uses
@extend %button-base;correctly inside the selector block.Final Answer:
.btn {@extend %button-base;} -> Option BQuick Check:
Correct@extendsyntax = C [OK]
- Missing '@' symbol before extend
- Using 'extends' instead of 'extend'
- Omitting semicolon after extend statement
%base { color: blue; font-weight: bold; }
.title {@extend %base; font-size: 2rem;}
.subtitle {@extend %base; font-size: 1.5rem;}What CSS will be generated?
Solution
Step 1: Understand
@extendbehaviorExtending a placeholder merges selectors sharing the same styles into one CSS rule.
Step 2: Analyze generated CSS
Both
.titleand.subtitlesharecolor: blueandfont-weight: boldin a combined selector. Their font sizes remain separate.Final Answer:
.title, .subtitle { color: blue; font-weight: bold; } .title { font-size: 2rem; } .subtitle { font-size: 1.5rem; } -> Option AQuick Check:
Shared styles combined, unique styles separate = A [OK]
- Expecting duplicated shared styles for each selector
- Thinking placeholder selectors appear in CSS
- Confusing
@extendwith mixins copying styles
%card { padding: 1rem; border: 1px solid #ccc; }
.panel { @extend card; background: white; }Solution
Step 1: Check
@extendsyntaxThe placeholder selector
%cardmust be referenced with the '%' in the extend statement.Step 2: Identify the error
The code uses
@extend card;missing the '%', causing a syntax error.Final Answer:
Missing '%' before 'card' in the extend statement. -> Option DQuick Check:
Extend placeholder needs '%' = D [OK]
- Forgetting '%' in extend target
- Thinking placeholders can't be extended
- Misunderstanding property units
@extend with a placeholder selector help maintain your Sass code better compared to copying styles into each button class?Solution
Step 1: Understand style duplication problem
Copying shared styles into each button class repeats code, making updates tedious and CSS larger.
Step 2: Explain
@extendadvantageUsing
@extendwith a placeholder merges shared styles into one CSS rule, reducing duplication and file size.Step 3: Benefits for maintenance
Updating shared styles in one place updates all buttons, making code easier to maintain.
Final Answer:
It creates one combined CSS rule for shared styles, reducing file size and easing updates. -> Option CQuick Check:
Extending merges shared styles = A [OK]
- Thinking extend duplicates styles instead of merging
- Confusing extend with JavaScript generation
- Believing styles are disabled until enabled
Ready to prove it?
Pass the test and this pattern moves unknown
