Understanding Sass @extend Limitations and Gotchas
📖 Scenario: You are working on a website's styles using Sass. You want to reuse styles efficiently but need to understand how @extend works and its limitations to avoid unexpected results.
🎯 Goal: Build a small Sass stylesheet that demonstrates how @extend works, shows its limitations, and how to avoid common gotchas.
📋 What You'll Learn
Create base styles for buttons
Use @extend to reuse button styles
Add a configuration variable to control a style condition
Show how extending multiple selectors can cause unexpected CSS output
Add a final style to fix or clarify the extension behavior
💡 Why This Matters
🌍 Real World
In real projects, reusing styles saves time and keeps CSS consistent. Understanding @extend helps avoid messy CSS and unexpected selector combinations.
💼 Career
Front-end developers often use Sass for styling. Knowing @extend limitations helps write clean, maintainable stylesheets and debug CSS issues.
Progress0 / 4 steps
1
Create base button styles
Create a Sass class called .button with these styles: padding: 1rem 2rem;, border-radius: 0.5rem;, and background-color: blue;.
SASS
Hint
Use .button { ... } and add the styles inside the curly braces.
2
Add a config variable for button color
Create a Sass variable called $primary-color and set it to blue. Then update the background-color in .button to use $primary-color.
SASS
Hint
Define $primary-color before using it in .button.
3
Use @extend and show multiple selector extension
Create a class .primary-button that uses @extend .button. Then create a class .alert with color: red;. Finally, create a class .alert-button that extends both .button and .alert using two @extend statements.
SASS
Hint
Use @extend inside the classes to reuse styles.
4
Add a final style to clarify extension behavior
Add a style for .alert-button that sets font-weight: bold; to show how you can add extra styles after extending multiple selectors.
SASS
Hint
You can add normal styles after using @extend to customize the class.
Practice
(1/5)
1. What is the main limitation of using @extend in Sass?
easy
A. It merges selectors which can cause unexpected CSS output
B. It duplicates all styles instead of reusing them
C. It only works with ID selectors
D. It cannot be used inside nested rules
Solution
Step 1: Understand what @extend does
@extend reuses 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 A
Quick Check:
@extend merges selectors = A [OK]
Hint: Remember: @extend merges selectors, not duplicates styles [OK]
Common Mistakes:
Thinking @extend duplicates styles
Believing @extend only works with IDs
Assuming @extend cannot be nested
2. Which of the following is the correct syntax to extend a placeholder selector %button-base inside a class .primary-button?
easy
A. .primary-button { @extend button-base; }
B. .primary-button { @extend %button-base; }
C. .primary-button { extend %button-base; }
D. .primary-button { @extend #button-base; }
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-base correctly.
Final Answer:
.primary-button { @extend %button-base; } -> Option B
Quick Check:
Use @extend %placeholder syntax = C [OK]
Hint: Always prefix placeholder selectors with % when extending [OK]
Extending regular classes merges selectors and outputs all styles, duplicating styles is inefficient, and limiting @extend to media queries doesn't solve unwanted output.
Final Answer:
Use placeholder selectors (e.g., %base-style) and extend them -> Option D
Quick Check:
Placeholders prevent unwanted CSS output = A [OK]
Hint: Use %placeholders with @extend to avoid extra CSS [OK]
Common Mistakes:
Extending regular classes causing large CSS output
Copy-pasting styles instead of reusing
Misusing @extend inside media queries
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.