Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Avoiding selector bloat from @extend in Sass
📖 Scenario: You are creating styles for a simple website with buttons. You want to reuse some button styles without making your CSS selectors too long or repeated.
🎯 Goal: Build Sass code that uses @extend carefully to avoid creating very long combined selectors in the final CSS.
📋 What You'll Learn
Create a base button style class
Create a secondary button style class that extends the base button
Add a special button style that extends the secondary button
Use placeholder selectors to avoid selector bloat
💡 Why This Matters
🌍 Real World
Web developers often reuse button styles across a website. Using @extend with placeholders helps keep CSS clean and efficient.
💼 Career
Understanding how to avoid selector bloat with @extend is important for writing maintainable Sass in professional front-end development.
Progress0 / 4 steps
1
Create base button style
Create a placeholder selector called %button-base with these styles: padding: 1rem;, border-radius: 0.5rem;, and font-weight: bold;
SASS
Hint
Use %button-base as a placeholder selector with @extend later.
2
Create secondary button style extending base
Create a class selector called .btn-secondary that @extends %button-base and adds background-color: lightgray; and color: black;
SASS
Hint
Use @extend %button-base; inside .btn-secondary.
3
Create special button style extending secondary
Create a class selector called .btn-special that @extends .btn-secondary and adds border: 2px solid blue;
SASS
Hint
Extend .btn-secondary inside .btn-special to reuse styles.
4
Add a button class that uses the base placeholder
Create a class selector called .btn that @extends %button-base and adds background-color: blue; and color: white;
SASS
Hint
Use @extend %button-base; inside .btn to reuse base styles without selector bloat.
Practice
(1/5)
1. What is the main problem with using @extend in Sass without placeholders?
easy
A. It creates long combined selectors causing selector bloat.
B. It duplicates all CSS properties in the output.
C. It prevents styles from being reused.
D. It only works inside mixins.
Solution
Step 1: Understand what @extend does
@extend shares 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 A
Quick Check:
@extend causes selector bloat = A [OK]
Hint: Remember: @extend merges selectors, causing long combined lists [OK]
Common Mistakes:
Thinking @extend duplicates properties instead of combining selectors
Confusing @extend with mixins
Believing @extend only works inside mixins
2. Which of the following is the correct way to define a placeholder selector in Sass?
easy
A. %button { color: blue; }
B. .button { color: blue; }
C. #button { color: blue; }
D. &button { color: blue; }
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.