Discover how one simple directive can save you hours of tedious CSS updates!
Why @extend directive in SASS? - Purpose & Use Cases
Imagine you are styling a website with many buttons that share similar styles. You copy and paste the same CSS rules for each button type manually.
When you want to change a common style, you have to update every copied block. This is slow, error-prone, and can cause inconsistent styles if you miss one.
The @extend directive lets you share styles by extending existing selectors. Change the base style once, and all extended selectors update automatically.
.btn-primary { color: white; background: blue; }
.btn-secondary { color: white; background: gray; }
.btn-primary-large { color: white; background: blue; font-size: 1.5rem; }.btn-base { color: white; }
.btn-primary { @extend .btn-base; background: blue; }
.btn-secondary { @extend .btn-base; background: gray; }
.btn-primary-large { @extend .btn-primary; font-size: 1.5rem; }You can write cleaner, DRY (Don't Repeat Yourself) styles that are easier to maintain and update across your whole site.
On a large website, you want all buttons to share the same font and padding but differ in colors. Using @extend, you define these shared styles once and extend them for each button type.
Manually copying styles leads to mistakes and extra work.
@extend shares styles between selectors efficiently.
It helps keep your CSS clean, consistent, and easy to update.
Practice
@extend directive do in Sass?Solution
Step 1: Understand the purpose of
The@extend@extenddirective is used to share styles between selectors in Sass.Step 2: Identify what
It copies the styles of one selector into another, reducing repetition.@extenddoesFinal Answer:
It allows one selector to reuse styles from another selector. -> Option DQuick Check:
@extend= reuse styles [OK]
@extend shares styles between selectors [OK]- Thinking
@extendcreates new properties - Confusing
@extendwith imports - Using
@extendto define variables
.button to .submit in Sass?Solution
Step 1: Recall correct
The correct syntax is@extendsyntax@extendfollowed by the selector name without quotes or parentheses.Step 2: Check each option
.submit { @extend .button; } uses@extend .button;which is correct. Others use wrong syntax like parentheses or quotes.Final Answer:
.submit { @extend .button; } -> Option AQuick Check:
Correct syntax =@extend .selector;[OK]
@extend followed by selector without quotes or parentheses [OK]- Adding parentheses after selector
- Using quotes around selector
- Using wrong function-like syntax
.alert { color: red; font-weight: bold; }
.error { @extend .alert; }What CSS will be generated?
Solution
Step 1: Understand
@extendoutput@extendmerges selectors so they share the same style block.Step 2: Apply to given code
Both.alertand.errorshare the same styles in one combined selector.Final Answer:
.alert, .error { color: red; font-weight: bold; } -> Option BQuick Check:
Selectors combined with@extend[OK]
@extend combines selectors sharing styles [OK]- Expecting duplicated style blocks
- Thinking styles copy separately
- Splitting properties between selectors
.card { border: 1px solid black; }
.panel { @extend card; }Solution
Step 1: Check
The selector to extend must include the dot for class selectors.@extendselector syntaxStep 2: Identify error in code
@extend card;misses the dot, should be@extend .card;.Final Answer:
Missing dot beforecardin@extendselector. -> Option AQuick Check:
Class selectors need dot in@extend[OK]
@extend [OK]- Omitting dot before class name
- Thinking
@extendcan't be used inside rules - Confusing property errors with selector errors
.warning style that shares all styles from .alert but also adds background-color: yellow;. Which Sass code correctly does this?Solution
Step 1: Understand
You can use@extendusage with additional styles@extendinside a selector and add other properties after it.Step 2: Check each option
.warning { @extend .alert; background-color: yellow; }correctly uses@extend .alert;then addsbackground-colorin the same block. Others have syntax errors: parentheses after selector, missing dot beforealert, or missing semicolon.Final Answer:
.warning { @extend .alert; background-color: yellow; } -> Option CQuick Check:
@extendplus extra styles in same block [OK]
@extend first, then add extra styles in same block [OK]- Adding parentheses after selector
- Omitting dot before class name in
@extend - Missing semicolon after
@extend
Ready to prove it?
Pass the test and this pattern moves unknown
