Extending and mixing help reuse styles in Sass so you don't repeat yourself. They make your CSS cleaner and easier to manage.
Extending vs mixing comparison in SASS
@extend .selector-to-extend; @mixin mixin-name { property: value; } @include mixin-name;
@extend copies styles from one selector to another by merging selectors in the CSS output.
@mixin defines reusable style blocks, and @include inserts those styles where needed.
@extend to share styles from .button to .primary-button..button { background: blue; color: white; } .primary-button { @extend .button; font-weight: bold; }
@mixin to define styles and @include to add them to .primary-button.@mixin button-styles { background: blue; color: white; } .primary-button { @include button-styles; font-weight: bold; }
@extend merges selectors, so both .alert and .error share styles..alert { border: 1px solid red; padding: 1rem; } .error { @extend .alert; background: pink; }
@mixin copies styles directly into .error, keeping selectors separate.@mixin alert-styles { border: 1px solid red; padding: 1rem; } .error { @include alert-styles; background: pink; }
This example shows two ways to reuse styles. The blue buttons share styles using @extend, so their CSS selectors are combined. The alert and error boxes show how @mixin copies styles separately, so the error box has its own background color added.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Extending vs Mixing Example</title> <style> /* Compiled CSS from Sass example */ .button, .primary-button { background: blue; color: white; } .primary-button { font-weight: bold; } .alert, .error { border: 1px solid red; padding: 1rem; } .error { background: pink; } </style> </head> <body> <button class="button">Normal Button</button> <button class="primary-button">Primary Button</button> <div class="alert">Alert Box</div> <div class="error">Error Box</div> </body> </html>
@extend merges selectors in the final CSS, which can reduce file size but may cause unexpected style inheritance.
@mixin copies styles wherever included, which can increase CSS size but keeps selectors separate and predictable.
Use @extend for simple shared styles and @mixin when you need more control or parameters.
@extend shares styles by combining selectors in the CSS output.
@mixin copies style blocks into selectors where included.
Choose @extend for smaller CSS and @mixin for flexible, reusable styles.
Practice
@extend and @mixin in Sass?Solution
Step 1: Understand
@extendbehavior@extendmerges selectors that share the same styles, reducing CSS size by combining rules.Step 2: Understand
@mixinbehavior@mixincopies the style block wherever it is included, allowing flexible reuse but increasing CSS size.Final Answer:
@extendshares styles by combining selectors,@mixincopies styles into selectors. -> Option AQuick Check:
@extendcombines,@mixincopies [OK]
- Confusing which one copies vs combines styles
- Thinking @extend creates new CSS files
- Believing @mixin deletes unused styles
button-style in Sass?Solution
Step 1: Recall mixin inclusion syntax
To use a mixin, Sass requires@include mixin-name;syntax.Step 2: Differentiate from other directives
@extendis for extending selectors,@mixindefines mixins, and@useimports modules.Final Answer:
@include button-style; -> Option BQuick Check:
Use@includeto add mixins [OK]
- Using @extend instead of @include for mixins
- Confusing @mixin (definition) with @include (usage)
- Using @use for mixins instead of modules
@mixin red-text { color: red; }
.button { @include red-text; }What CSS will be generated?Solution
Step 1: Understand mixin inclusion
The mixinred-textdefinescolor: red;. Including it copies this style into.button.Step 2: Check generated CSS
The CSS will have.button { color: red; }because the mixin's styles are copied inside the selector.Final Answer:
.button { color: red; } -> Option DQuick Check:
Mixin copies styles inside selector [OK]
- Expecting selectors to merge like @extend
- Thinking mixin creates separate class
- Assuming syntax error without @extend
.alert { color: red; }
.error { @extend .alert; }
.error { background: yellow; }Solution
Step 1: Check @extend usage
@extendmerges selectors with the same styles. Extending.alertinside.erroris valid.Step 2: Confirm multiple declarations for .error
Multiple declarations for.errorare allowed and combined in CSS output.Final Answer:
No error; code compiles and merges selectors. -> Option AQuick Check:
@extend merges selectors, multiple declarations allowed [OK]
- Thinking duplicate selectors cause errors
- Believing @extend must come before class definition
- Assuming @extend cannot be inside selector
Solution
Step 1: Understand the need for customization
Customizing colors requires flexibility to pass different values for each button.Step 2: Compare @extend and @mixin for this use case
@extendmerges selectors but cannot accept parameters.@mixincan accept parameters to customize styles.Step 3: Choose the best approach
Using a@mixinwith a color parameter copies styles with different colors without duplicating selectors.Final Answer:
Use@mixinwith a color parameter to copy styles with different colors. -> Option CQuick Check:
Mixins with parameters allow flexible style reuse [OK]
- Trying to pass parameters to @extend (not supported)
- Using @extend which merges selectors but lacks flexibility
- Avoiding Sass features and writing repetitive CSS
Ready to prove it?
Pass the test and this pattern moves unknown
