0
0
SASSmarkup~5 mins

Conditional mixins with @if in SASS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a mixin in Sass?
A mixin is a reusable block of styles that you can include in other selectors to avoid repeating code.
Click to reveal answer
beginner
How does the @if directive work inside a Sass mixin?
The @if directive lets you add conditions inside a mixin to apply styles only when certain rules are true.
Click to reveal answer
intermediate
Example: What will this Sass code do? @mixin color-theme($theme) { @if $theme == light { background: white; color: black; } @else if $theme == dark { background: black; color: white; } } .button { @include color-theme(dark); }
It will create a .button class with a black background and white text because the mixin applies styles for the 'dark' theme.
Click to reveal answer
intermediate
Why use conditional mixins instead of writing separate classes?
Conditional mixins keep your code DRY (Don't Repeat Yourself) by reusing style blocks with small changes based on conditions.
Click to reveal answer
beginner
Can you use multiple @if, @else if, and @else inside one mixin?
Yes, you can chain multiple conditions to handle different cases inside a single mixin.
Click to reveal answer
What does the @if directive do inside a Sass mixin?
ARuns JavaScript code
BDefines a new variable
CImports another Sass file
DAdds styles only if a condition is true
How do you include a mixin named 'button-style' in Sass?
A@mixin button-style;
B@include button-style;
C@use button-style;
D@import button-style;
Which of these is a valid condition in a Sass @if statement?
A$color == blue
B$color == "blue"
C$color = blue
Dcolor == blue
What happens if none of the @if or @else if conditions match and there is an @else block?
AAn error occurs
BNothing happens
CThe @else block runs
DThe first @if block runs anyway
Why are conditional mixins helpful in Sass?
AThey allow dynamic styling based on variables
BThey replace HTML tags
CThey make CSS files bigger
DThey run JavaScript in CSS
Explain how you would use a conditional mixin in Sass to change button colors based on a theme variable.
Think about checking the theme variable inside the mixin and applying different colors.
You got /5 concepts.
    Describe the benefits of using @if inside mixins compared to writing separate CSS classes for each style variation.
    Consider how conditional mixins keep your code clean and flexible.
    You got /4 concepts.