Conditional mixins let you change styles based on conditions. This helps you write flexible and reusable CSS.
Conditional mixins with @if in SASS
@mixin mixin-name($param) { @if $param == value { // styles here } @else if $param == other-value { // other styles } @else { // default styles } }
Use @if inside a mixin to check conditions.
You can use @else if and @else for multiple cases.
@mixin theme-color($theme) { @if $theme == light { background-color: white; color: black; } @else if $theme == dark { background-color: black; color: white; } @else { background-color: gray; color: black; } }
@mixin padding-size($size) { @if $size == small { padding: 0.5rem; } @else if $size == large { padding: 2rem; } @else { padding: 1rem; } }
This example creates a button style mixin that changes styles based on the button type. Then it applies the mixin to three button classes with different types.
@use "sass:color"; @mixin button-style($type) { @if $type == primary { background-color: blue; color: white; border: none; } @else if $type == secondary { background-color: white; color: blue; border: 2px solid blue; } @else { background-color: gray; color: black; border: none; } padding: 0.75rem 1.5rem; border-radius: 0.5rem; font-weight: bold; cursor: pointer; } .button-primary { @include button-style(primary); } .button-secondary { @include button-style(secondary); } .button-default { @include button-style(default); }
Always test your conditions to make sure styles apply as expected.
Use meaningful parameter names to keep your mixins clear.
Conditional mixins help reduce repeated CSS and keep your code DRY (Don't Repeat Yourself).
Conditional mixins let you write flexible styles based on input values.
Use @if, @else if, and @else inside mixins for different cases.
This technique helps keep your CSS clean and reusable.
Practice
@if directive inside a Sass mixin do?Solution
Step 1: Understand the role of
The@ifin Sass mixins@ifdirective checks a condition and applies styles only when that condition is true.Step 2: Compare with other options
Looping is done with@foror@each, importing uses@import, and defining classes is done with selectors, not@if.Final Answer:
It applies styles only if a condition is true. -> Option DQuick Check:
@ifcontrols conditional style application [OK]
- Confusing @if with loops like @for or @each
- Thinking @if imports files
- Assuming @if creates CSS selectors
@if inside a Sass mixin?Solution
Step 1: Review correct
The correct syntax uses@ifsyntax in Sass mixins@iffollowed by a condition without parentheses, then curly braces for the block:@if $flag { ... }.Step 2: Check each option
@mixin example($flag) { @if $flag { color: red; } } matches correct syntax. @mixin example($flag) { if ($flag) { color: red; } } misses@beforeif. @mixin example($flag) { @if ($flag) color: red; } incorrectly uses parentheses and misses braces. @mixin example($flag) { @if $flag: { color: red; } } uses colon incorrectly.Final Answer:
@mixin example($flag) { @if $flag { color: red; } } -> Option AQuick Check:
Correct@ifsyntax = @mixin example($flag) { @if $flag { color: red; } } [OK]
- Omitting @ before if
- Using parentheses around condition
- Using colon instead of braces
@mixin color-choice($color) {
@if $color == 'red' {
color: red;
} @else if $color == 'blue' {
color: blue;
} @else {
color: black;
}
}
.test {
@include color-choice('blue');
}What color will the
.test class have?Solution
Step 1: Check the mixin call parameter
The mixin is called with'blue'as the argument for$color.Step 2: Follow the conditional branches
The first condition checks if$color == 'red'which is false. The second condition@else if $color == 'blue'is true, socolor: blue;is applied.Final Answer:
blue -> Option CQuick Check:
Input 'blue' triggers blue color [OK]
- Confusing single quotes with no quotes
- Ignoring @else if branch
- Assuming default else applies first
@mixin size($value) {
@if $value > 10
font-size: 2rem;
@else
font-size: 1rem;
}Solution
Step 1: Check syntax for @if and @else blocks
In Sass, when using@ifand@elseinside mixins, blocks must be wrapped in curly braces{ }.Step 2: Identify missing braces
The code lacks braces after@if $value > 10and@else, causing syntax errors.Final Answer:
Missing curly braces after @if and @else blocks -> Option AQuick Check:
Always use braces for conditional blocks [OK]
- Omitting braces for single-line blocks
- Using wrong comparison operators
- Thinking variable names are restricted
@if to achieve this?Solution
Step 1: Check comparison operators and string quotes
Sass uses==for comparison and strings must be quoted. @mixin status-bg($status) { @if $status == 'success' { background-color: green; } @else if $status == 'warning' { background-color: yellow; } @else if $status == 'error' { background-color: red; } @else { background-color: gray; } } uses==and quotes correctly.Step 2: Review other options for errors
@mixin status-bg($status) { @if $status = 'success' { background-color: green; } @else if $status = 'warning' { background-color: yellow; } @else if $status = 'error' { background-color: red; } @else { background-color: gray; } } uses single=which is assignment, not comparison. @mixin status-bg($status) { @if $status == success { background-color: green; } @else if $status == warning { background-color: yellow; } @else if $status == error { background-color: red; } @else { background-color: gray; } } misses quotes around strings. @mixin status-bg($status) { @if $status === 'success' { background-color: green; } @else if $status === 'warning' { background-color: yellow; } @else if $status === 'error' { background-color: red; } @else { background-color: gray; } } uses===which is invalid in Sass.Final Answer:
@mixin status-bg($status) { @if $status == 'success' { background-color: green; } @else if $status == 'warning' { background-color: yellow; } @else if $status == 'error' { background-color: red; } @else { background-color: gray; } } -> Option BQuick Check:
Use == and quotes for string comparison in Sass [OK]
- Using = instead of == for comparison
- Omitting quotes around strings
- Using === which is invalid in Sass
Ready to prove it?
Pass the test and this pattern moves unknown
