Discover how a simple branching tool can save you hours of repetitive styling work!
Why @else and @else if branches in SASS? - Purpose & Use Cases
Imagine you are styling a website and want different button colors based on the button type. You write separate CSS rules for each type manually, repeating similar code again and again.
This manual way is slow and messy. If you want to add a new button type or change the logic, you must rewrite or copy-paste many rules. It's easy to make mistakes or forget to update some styles.
@else and @else if branches let you write one clean block of code that checks conditions step-by-step. This way, you only write the shared structure once and handle different cases clearly and safely.
.button-primary { color: blue; }
.button-secondary { color: gray; }
.button-danger { color: red; }@if $type == 'primary' { color: blue; } @else if $type == 'secondary' { color: gray; } @else { color: red; }
This lets you create flexible, easy-to-maintain styles that adapt automatically based on conditions, saving time and reducing errors.
When building a theme with many button styles, you can use @else if branches to set colors, sizes, or fonts based on the button's purpose without repeating code.
Manual CSS for many conditions is repetitive and error-prone.
@else and @else if branches let you handle multiple conditions cleanly in one place.
This improves code clarity, maintenance, and flexibility in styling.
Practice
@else if branch do in Sass?Solution
Step 1: Understand
The@ifand@else ifroles@ifchecks the first condition. If false,@else ifchecks another condition.Step 2: Identify
@else ifbehavior@else ifonly runs if the previous@ifor@else ifwas false, allowing multiple checks.Final Answer:
Checks another condition if the previous@ifwas false -> Option DQuick Check:
@else if= checks next condition [OK]
@else if checks next condition only if previous fails [OK]- Thinking
@else ifruns when previous is true - Confusing
@else ifwith@else - Believing
@else ifends the condition chain
@else if in Sass?Solution
Step 1: Recall correct
In Sass,@else ifsyntax@else ifis written as two words without parentheses around the condition.Step 2: Check each option
@else if $color == red { color: red; } uses correct syntax:@else if $color == red { ... }. @elseif $color == red { color: red; } misses space. @else if ($color == red) { color: red; } uses parentheses which Sass does not require. @else if $color = red { color: red; } uses single equals which is assignment, not comparison.Final Answer:
@else if $color == red { color: red; } -> Option BQuick Check:
Correct@else ifsyntax = @else if $color == red { color: red; } [OK]
@else if [OK]- Writing
@elseifas one word - Using parentheses around condition
- Using single equals instead of double equals
$color: blue;
@if $color == red {
background: red;
} @else if $color == green {
background: green;
} @else {
background: black;
}Solution
Step 1: Check the value of
The variable$color$coloris set toblue.Step 2: Evaluate conditions in order
First@ifchecks if$color == red(false). Then@else ifchecks if$color == green(false). Since both are false,@elseruns, setting background to black.Final Answer:
black -> Option AQuick Check:
None matched, so@elseruns = black [OK]
@if or @else if matches, @else runs [OK]- Assuming
@else ifruns when condition is false - Thinking
@elseis optional and skipped - Confusing variable value with condition
@if $size == large {
font-size: 2rem;
} @else if $size = medium {
font-size: 1.5rem;
} @else {
font-size: 1rem;
}Solution
Step 1: Check condition operators
In Sass, conditions use double equals==to compare values, not single equals=which assigns values.Step 2: Locate the error
The@else ifline uses$size = mediumwhich is assignment, causing a syntax error.Final Answer:
Using single equals (=) instead of double equals (==) in@else if-> Option CQuick Check:
Comparison needs ==, not = [OK]
- Using = instead of ==
- Forgetting to declare variables
- Misplacing curly braces
$theme is set to null?$theme: null;
@if $theme == light {
background: white;
} @else if $theme == dark {
background: black;
} @else {
background: gray;
}Solution
Step 1: Understand
nullin Sass conditionsnullis a special value that does not equal strings likelightordark.Step 2: Evaluate conditions with
Both$theme = null@if $theme == lightand@else if $theme == darkare false. So,@elseruns, setting background to gray.Final Answer:
gray -> Option AQuick Check:
nullmatches no condition, so@elseruns [OK]
null never equals strings; @else handles it [OK]- Assuming
nullequals a string - Forgetting
@elseruns when no conditions match - Expecting
nullto output literally
Ready to prove it?
Pass the test and this pattern moves unknown
