The @extend directive helps you reuse styles from one CSS selector in another. It keeps your code shorter and easier to manage.
@extend directive in SASS
@extend selector;The selector is the CSS class or element whose styles you want to reuse.
You write @extend inside a CSS rule to inherit styles from another rule.
.button-primary use all styles from .button plus its own background color..button-primary { @extend .button; background-color: blue; }
.alert-error inherits styles from .alert and adds red text color..alert-error { @extend .alert; color: red; }
h1 element will have all styles from .title plus a bigger font size.h1 {
@extend .title;
font-size: 2rem;
}This example shows a base button style in .button. The .button-primary and .button-danger classes reuse the base styles using @extend and add their own colors.
In the browser, you will see three buttons with consistent padding and shape but different background colors.
@use 'sass:color'; .button { padding: 1rem 2rem; border-radius: 0.5rem; background-color: #eee; color: #333; border: none; cursor: pointer; } .button-primary { @extend .button; background-color: #007bff; color: white; } .button-danger { @extend .button; background-color: #dc3545; color: white; } /* HTML example to use these classes */ /* <button class="button">Normal</button> <button class="button-primary">Primary</button> <button class="button-danger">Danger</button> */
Do not overuse @extend because it can create complex CSS selectors that are hard to debug.
@extend works best with classes, not with IDs or element selectors.
Use @extend to keep your styles DRY (Don't Repeat Yourself) and easier to maintain.
@extend lets one CSS rule reuse styles from another.
It helps keep your CSS code shorter and organized.
Use it to share common styles like buttons or alerts across different classes.
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
