📖 Scenario: You are building a website and want to reuse a style block for buttons. Instead of repeating the same styles, you will create a Sass mixin to keep your code clean and easy to update.
🎯 Goal: Create a Sass mixin named button-style that sets the background color to blue, text color to white, and padding to 1rem. Then use this mixin inside a CSS class .btn.
📋 What You'll Learn
Define a mixin called button-style using @mixin
Inside the mixin, set background-color to blue
Set color to white inside the mixin
Set padding to 1rem inside the mixin
Create a CSS class .btn that includes the button-style mixin using @include
💡 Why This Matters
🌍 Real World
Mixins let you write reusable style blocks in Sass, saving time and making your CSS easier to maintain.
💼 Career
Web developers use Sass mixins to create scalable and clean style sheets for websites and apps.
Progress0 / 4 steps
1
Define the button-style mixin
Write a Sass mixin named button-style using @mixin button-style that sets background-color to blue, color to white, and padding to 1rem inside curly braces.
SASS
Hint
Use @mixin followed by the mixin name and curly braces to group styles.
2
Create the .btn class
Create a CSS class named .btn using .btn { } syntax. Leave it empty for now.
SASS
Hint
Use a dot before the class name and curly braces to create a CSS class.
3
Include the button-style mixin inside .btn
Inside the .btn class, use @include button-style; to apply the mixin styles.
SASS
Hint
Use @include followed by the mixin name and a semicolon inside the class.
4
Complete the Sass code for button styling
Make sure the Sass code has the @mixin button-style defined and the .btn class includes the mixin with @include button-style;.
SASS
Hint
Review your code to ensure the mixin and include are correctly written.
Practice
(1/5)
1. What is the main purpose of using @mixin in Sass?
easy
A. To create reusable blocks of CSS code
B. To define variables for colors
C. To import external CSS files
D. To write JavaScript inside Sass
Solution
Step 1: Understand what @mixin does
@mixin is used to write reusable CSS blocks that can be included multiple times.
Step 2: Compare options
Only 'To create reusable blocks of CSS code' correctly describes this purpose. Variables, imports, and JavaScript are unrelated to @mixin.
Final Answer:
To create reusable blocks of CSS code -> Option A
Quick Check:
Mixin = reusable CSS block [OK]
Hint: Mixins bundle CSS to reuse easily [OK]
Common Mistakes:
Confusing mixins with variables
Thinking mixins import files
Assuming mixins run JavaScript
2. Which of the following is the correct syntax to define a mixin named button-style in Sass?
easy
A. @include button-style { color: blue; }
B. @mixin button-style { color: blue; }
C. @mixin: button-style { color: blue; }
D. mixin button-style() { color: blue; }
Solution
Step 1: Recall mixin definition syntax
The correct way to define a mixin is using @mixin name { ... }.
Step 2: Check each option
@mixin button-style { color: blue; } uses @mixin button-style { ... }, which is correct. @include button-style { color: blue; } uses @include which is for using mixins, not defining. Options C and D have invalid syntax.
Final Answer:
@mixin button-style { color: blue; } -> Option B
Quick Check:
Define mixin with @mixin [OK]
Hint: Define mixins with @mixin, use with @include [OK]
A. Mixin name should be borderRadius without hyphen
B. Missing semicolon after border-radius property
C. Using @mixin instead of @include to apply the mixin
D. Parameters cannot be passed to mixins
Solution
Step 1: Check mixin application syntax
Mixins are defined with @mixin but applied with @include.
Step 2: Identify the incorrect usage
The code uses @mixin border-radius(10px); inside .box, which is wrong. It should be @include border-radius(10px);.
Final Answer:
Using @mixin instead of @include to apply the mixin -> Option C
Quick Check:
Apply mixins with @include [OK]
Hint: Use @include to apply mixins, not @mixin [OK]
Common Mistakes:
Confusing @mixin and @include
Thinking parameters are not allowed
Assuming hyphens are invalid in names
5. You want to create a mixin responsive-text that sets font size differently for small and large screens. Which of these mixin definitions correctly uses parameters and media queries?
The mixin should accept two sizes: one for small screens and one for large screens using a media query with min-width 600px.
Step 2: Analyze each option
@mixin responsive-text($small-size, $large-size) {
font-size: $small-size;
@media (min-width: 600px) {
font-size: $large-size;
}
} correctly defines two parameters and applies font sizes with a media query for screens wider than 600px. @mixin responsive-text {
font-size: 12px;
@media (min-width: 600px) {
font-size: 18px;
}
} has no parameters, so not flexible. @mixin responsive-text($size) {
font-size: $size;
@media (min-width: 600px) {
font-size: $size * 2;
}
} tries to multiply a variable inside CSS, which Sass does not support directly without interpolation or calculation. @mixin responsive-text($small, $large) {
font-size: $small;
@media (max-width: 600px) {
font-size: $large;
}
} uses max-width media query incorrectly for large screens.