A @mixin is a reusable block of CSS styles in Sass. It lets you write styles once and include them wherever needed, saving time and avoiding repetition.
Click to reveal answer
beginner
How do you define a mixin named button-style in Sass?
You write @mixin button-style { /* styles here */ }. Inside the curly braces, you put the CSS rules you want to reuse.
Click to reveal answer
beginner
How do you include a mixin in a CSS rule?
Use @include mixin-name; inside the CSS selector where you want the styles to appear.
Click to reveal answer
intermediate
Can mixins accept parameters? Why is this useful?
Yes, mixins can take parameters to customize styles. This makes them flexible, like a recipe where you can change ingredients to get different results.
Click to reveal answer
intermediate
What is the difference between a mixin and a placeholder selector in Sass?
Mixins can include CSS and accept parameters, and you include them with @include. Placeholders are like invisible selectors used with @extend and can't take parameters.
Click to reveal answer
What keyword do you use to define a mixin in Sass?
A@function
B@include
C@extend
D@mixin
✗ Incorrect
You define a mixin with @mixin. @include is used to add the mixin styles.
How do you add a mixin's styles inside a CSS rule?
A@mixin mixin-name;
B@extend mixin-name;
C@include mixin-name;
D@use mixin-name;
✗ Incorrect
Use @include mixin-name; to insert the mixin's styles.
Which of these is a benefit of using mixins?
AAutomatically minify CSS
BWrite CSS only once and reuse it
CReplace HTML tags
DCreate JavaScript functions
✗ Incorrect
Mixins let you write CSS once and reuse it, saving time and avoiding repetition.
How do you define a mixin to accept a parameter named color?
A@mixin example($color) { ... }
B@include example(color) { ... }
C@mixin example { color: color; }
D@include example { color: color; }
✗ Incorrect
You define the mixin with parameters like @mixin example($color) { ... } to use the parameter inside.
Which Sass feature is best for reusable styles that need parameters?
AMixins
BFunctions
CVariables
DPlaceholder selectors
✗ Incorrect
Mixins can take parameters and include reusable styles, making them ideal for this.
Explain what a mixin is in Sass and how it helps in writing CSS.
Think of a mixin like a reusable recipe for CSS styles.
You got /4 concepts.
Describe how to define a mixin with parameters and how to use it in your styles.
Parameters let you customize the mixin each time you use it.
You got /3 concepts.
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.