Mixins help you write code once and use it many times. This stops you from repeating the same styles over and over.
Why mixins eliminate duplication in SASS
Introduction
Syntax
SASS
@mixin mixin-name($param1, $param2) { // styles here } @include mixin-name(value1, value2);
Use @mixin to define reusable styles.
Use @include to add those styles where you want.
Examples
.box to avoid repeating the flex styles.SASS
@mixin center { display: flex; justify-content: center; align-items: center; } .box { @include center; height: 100px; width: 100px; background: lightblue; }
SASS
@mixin button-style($color) { background-color: $color; border: none; padding: 0.5rem 1rem; color: white; border-radius: 0.25rem; } .btn-primary { @include button-style(blue); } .btn-danger { @include button-style(red); }
Sample Program
This example shows a box with text centered both horizontally and vertically. The centering styles come from a mixin in SASS, which avoids repeating the flexbox code.
SASS
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Mixin Example</title> <style> /* Compiled CSS from SASS */ .box { display: flex; justify-content: center; align-items: center; height: 100px; width: 100px; background: lightblue; margin: 1rem; font-family: Arial, sans-serif; } </style> </head> <body> <div class="box">Centered Text</div> </body> </html>
Important Notes
Mixins make your CSS DRY (Don't Repeat Yourself).
You can add parameters to mixins to make them flexible.
Using mixins helps keep your styles consistent and easy to update.
Summary
Mixins let you write styles once and reuse them everywhere.
They stop you from copying and pasting the same code.
Mixins can take inputs to customize styles for different uses.
Practice
1. Why do Sass mixins help reduce duplication in your stylesheets?
easy
Solution
Step 1: Understand what duplication means in CSS
Duplication means writing the same style code multiple times in different places.Step 2: See how mixins solve duplication
Mixins let you write a style block once and reuse it by calling the mixin wherever needed.Final Answer:
Because they let you write a style once and reuse it multiple times -> Option BQuick Check:
Mixins reduce duplication = reuse styles [OK]
Hint: Mixins = write once, use many times [OK]
Common Mistakes:
- Thinking mixins compress CSS files
- Confusing mixins with CSS selectors
- Believing mixins generate random styles
2. Which of the following is the correct syntax to define a mixin in Sass?
easy
Solution
Step 1: Recall the keyword to define a mixin
The correct keyword to define a mixin is@mixin.Step 2: Check the syntax format
The mixin is defined with@mixin name { ... }. Other options use incorrect keywords.Final Answer:
@mixin example { color: red; } -> Option AQuick Check:
Define mixin with @mixin [OK]
Hint: Define mixins with @mixin keyword [OK]
Common Mistakes:
- Using @include to define instead of call
- Using @define which is invalid
- Confusing @style with mixin syntax
3. What will be the output CSS of this Sass code?
@mixin border-radius($radius) {
border-radius: $radius;
}
.box {
@include border-radius(10px);
background: blue;
}medium
Solution
Step 1: Understand mixin with parameter
The mixinborder-radiustakes a parameter$radiusand setsborder-radiusproperty.Step 2: See how mixin is included
The class.boxincludes the mixin with argument10px, soborder-radius: 10px;is added.Final Answer:
.box { border-radius: 10px; background: blue; } -> Option DQuick Check:
Mixin parameter replaced correctly [OK]
Hint: Mixin parameters replace variables inside [OK]
Common Mistakes:
- Expecting variable names in output CSS
- Ignoring mixin parameters
- Thinking mixin causes syntax errors
4. Identify the error in this Sass code that tries to use a mixin:
@mixin text-style {
font-size: 16px;
color: black;
}
.title {
text-style;
font-weight: bold;
}medium
Solution
Step 1: Check how mixins are called
Mixins must be called with@include mixin-name;inside selectors.Step 2: Identify the error in usage
The code usestext-style;without@include, so it won't apply the mixin styles.Final Answer:
Mixin is not called with @include keyword -> Option AQuick Check:
Call mixins with @include [OK]
Hint: Always use @include to call mixins [OK]
Common Mistakes:
- Forgetting @include before mixin name
- Adding parameters when none needed
- Placing mixin inside selector definition
5. You want to create a button style that can have different background colors but share the same padding and border radius. How can mixins help you avoid duplication?
@mixin button-style($bg-color) {
background-color: $bg-color;
padding: 1rem 2rem;
border-radius: 0.5rem;
}
.primary-btn {
@include button-style(blue);
}
.secondary-btn {
@include button-style(gray);
}hard
Solution
Step 1: Analyze the mixin with parameter
The mixinbutton-styledefines padding and border-radius once and accepts a background color parameter.Step 2: See how different buttons reuse the mixin
Both.primary-btnand.secondary-btninclude the mixin with different colors, reusing common styles without duplication.Final Answer:
Mixins let you write padding and border-radius once and reuse with different colors -> Option CQuick Check:
Mixins reuse common styles with custom inputs [OK]
Hint: Use parameters in mixins for flexible reuse [OK]
Common Mistakes:
- Thinking mixins require rewriting all styles
- Believing mixins can't take parameters
- Assuming mixins increase CSS size unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
