Sass Extend

Sass @extend – Complete Practical Guide
The Sass @extend directive allows one selector to inherit styles from another selector.
Think of @extend as CSS inheritance at the selector level.
1. What Is Sass @extend?
@extend tells Sass:
“This selector should behave like that selector.”
Instead of repeating the same CSS rules, Sass merges selectors during compilation.
2. Basic Syntax
3. Simple Example
Compiled CSS
- No duplication
- Cleaner CSS output
4. Why Use @extend?
Without @extend:
Duplicate styles
Bigger CSS files
Harder maintenance
With @extend:
DRY (Don’t Repeat Yourself)
Smaller compiled CSS
Semantic relationships
5. @extend vs Copy-Paste
Copy-Paste
@extend
6. Extending Multiple Selectors
7. Placeholder Selectors (%) – BEST PRACTICE
Why placeholders?
Never appear in compiled CSS
Used only for extension
Prevent selector bloat
Example
Compiled CSS
- Clean
- Efficient
- Recommended
8. Extending with Nested Selectors
Sass correctly resolves nested selectors.
9. @extend and Pseudo-Classes
Sass merges selectors safely.
10. When NOT to Use @extend
Avoid @extend when:
- Styles are unrelated
- You need different properties
- You are working with utility classes
- You want predictable selector output
In these cases → use mixins instead.
11. @extend vs Mixins
| Feature | @extend | Mixins |
|---|---|---|
| Reuses styles | Yes | Yes |
| Duplicates CSS | No | Yes |
| Selector merging | Yes | No |
| Accepts parameters | No | Yes |
| Best for | Shared semantics | Dynamic styles |
- Extend = inheritance
- Mixin = composition
12. Common Mistakes
Extending real classes instead of placeholders
Extending deeply nested selectors
Using
@extendlike a mixinCausing large selector chains
Extending third-party classes
13. Performance & Output Considerations
@extendruns at compile timeNo runtime cost
Can reduce CSS size
Poor usage can increase selector complexity
14. Interview-Level Questions
Q: What does @extend do in Sass?
It allows one selector to inherit styles from another.
Q: Does @extend duplicate CSS?
No, it merges selectors.
Q: Best practice for @extend?
Use placeholder selectors (%).
Q: Difference between @extend and mixins?
Selector merging vs CSS duplication.
Best Practices Summary
Prefer
%placeholdersover real classesUse
@extendfor shared base stylesAvoid extending deeply nested selectors
Use mixins for configurable styles
Keep selector relationships meaningful
Final Summary
@extendenables selector-level inheritanceReduces CSS duplication
Best used with placeholders
Powerful but must be used carefully
Essential tool for clean Sass architecture
