The Sass @else if rule adds another tested branch after an @if. Its block runs only when every preceding condition was falsey and this expression is truthy.
Chain as many @else ifs as you need. The first truthy branch wins; later branches are skipped. End with a plain @else as the final fallback (soft default or @error).
Official pattern.
Per the Sass docs: write @else if <expr> { … } after @if to choose among several options (directions, tones, sizes) at compile time.
First match wins.
Order matters. Put specific tests first; use a final @else for everything else.
Truthiness.
Only false and null are falsey in Sass. Empty strings, empty lists, and 0 are truthy.
Syntax
Syntax
SCSS
@use "sass:math";
@mixin triangle($size, $color, $direction) {
height: 0;
width: 0;
border-color: transparent;
border-style: solid;
border-width: math.div($size, 2);
@if $direction == up {
border-bottom-color: $color;
} @else if $direction == right {
border-left-color: $color;
} @else if $direction == down {
border-top-color: $color;
} @else if $direction == left {
border-right-color: $color;
} @else {
@error "Unknown direction #{$direction}.";
}
}
Pattern: @if → one or more @else if → optional final @else.
Reference
How It Chains
Where @else if fits in control flow:
Form
Meaning
@if … @else if …
Second (or later) tested branch
… @else if … @else { }
Final untested fallback after the chain
Inside mixins / functions
Map keywords to styles or @return values
Final @else { @error … }
Fail loudly on unknown API values
Also used with
Related Rules
Start every chain with @if. End with @else when you need a fallback. Conditions often use boolean operators and equality. Unknown keywords commonly call @error.
Code
SCSS Examples
Ordered @if / @else if chains (often with a final @else). Open View Compiled CSS for verified output.
Example 1 — Directional Triangle
Official-style mixin: one CSS triangle, four directions, @error otherwise.
SCSS
@use "sass:math";
@mixin triangle($size, $color, $direction) {
height: 0;
width: 0;
border-color: transparent;
border-style: solid;
border-width: math.div($size, 2);
@if $direction == up {
border-bottom-color: $color;
} @else if $direction == right {
border-left-color: $color;
} @else if $direction == down {
border-top-color: $color;
} @else if $direction == left {
border-right-color: $color;
} @else {
@error "Unknown direction #{$direction}.";
}
}
.next {
@include triangle(5px, black, right);
}
.up {
@include triangle(8px, #036, up);
}
Call it inside each media query with a different name.
Unknown names fall back to mobile padding via @else.
Keeps breakpoint values in one place to edit.
Edge Cases & Pitfalls
Most @else if bugs are order, missing fallbacks, or overlapping separate @if blocks.
order
First match wins
Put specific tests before broad ones. Once a branch runs, later @else ifs are skipped.
fallback
End public APIs with @else
Use soft defaults when safe, or @error when unknown keywords would produce broken CSS.
chain
Prefer one chain over many @if
Separate @if blocks can all run. A single @if / @else if / @else chain keeps outcomes exclusive.
maps
Large keyword sets may prefer maps
Long chains work, but a map + map.get (with @error on missing keys) can scale cleaner.
Toolchain
Sass Support
@else if is a core Dart Sass control rule. It is not a browser CSS feature — support means your Sass compiler understands it.
Implementation
Status
Dart Sass
Supported (recommended)
LibSass / node-sass
Supported (deprecated toolchain)
Ruby Sass
Supported (end-of-life)
Browsers
N/A — only see compiled CSS
Prefer Dart Sass for new projects. SCSS and indented syntax both support @else if.
Remember
Key Takeaways
Chain:@if → @else if… → optional @else.
First match wins — later branches are skipped.
Fallback: soft default or @error in final @else.
Compile-time only — browsers see one winning branch.
One line:@else if is an ordered, exclusive test after @if.
Compile
Compile Notes
@else if never reaches the browser. Conceptual flow:
flow.txt
1. Evaluate @if — if truthy, run it and stop
2. Else evaluate each @else if in order — first truthy wins
3. If none matched, run the final @else (if present)
4. Final stylesheet contains only that branch’s CSS
Frequently Asked Questions
It adds another tested branch after an @if. The block runs only if every preceding condition was falsey and this @else if expression is truthy.
As many as you need. Sass evaluates them in order and runs only the first truthy block in the chain.
No. Once one block in the @if / @else if / @else chain succeeds, the rest are skipped.
@else if has its own condition. A plain @else has no condition—it is the final fallback when every prior test failed.
Often yes for public APIs—unknown keywords should fail loudly. Soft defaults in @else are fine when a safe fallback exists.
No. Like @if and @else, it is compile-time only. Browsers see styles from the winning branch only.