Discover how a small Sass trick can save you hours of repetitive styling work!
Extending vs mixing comparison in SASS - When to Use Which
Imagine you have many buttons on your website, each with slightly different colors but mostly the same style. You write the same style rules again and again for each button.
Writing repeated styles manually wastes time and makes your code long and hard to update. If you want to change the common style, you must edit every button's code separately, risking mistakes and inconsistency.
Using extending or mixins in Sass lets you share styles easily. You write common styles once and reuse them, so your code stays clean and changes apply everywhere automatically.
.btn-primary { color: white; background: blue; padding: 1rem; }
.btn-secondary { color: white; background: gray; padding: 1rem; }@mixin button-style { color: white; padding: 1rem; }
.btn-primary { @include button-style; background: blue; }
.btn-secondary { @include button-style; background: gray; }You can build consistent, easy-to-maintain styles that save time and reduce errors across your whole website.
A design system with many components sharing base styles but needing small differences uses mixins or extends to keep code DRY and updates simple.
Manual repetition wastes time and causes errors.
Extending and mixins share styles to keep code clean.
They make updates fast and consistent across many elements.
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
