Sass @if/@else
@if lets Sass choose which styles to output based on a condition. It's evaluated at compile time, so only the matching branch ends up in the CSS.
@if <condition> {
…
} @else if <condition> {
…
} @else {
…
}A simple condition
$rounded: true;
.card {
padding: 16px;
@if $rounded {
border-radius: 12px;
}
}.card {
padding: 16px;
border-radius: 12px;
}
Change $rounded to false and run again — the border-radius disappears.
@else if and @else
Conditions are checked in order; the first true one wins.
@mixin text-size($size) {
@if $size == small {
font-size: 0.875rem;
} @else if $size == large {
font-size: 1.25rem;
font-weight: 600;
} @else {
font-size: 1rem;
}
}
.caption { @include text-size(small); }
.lead { @include text-size(large); }
.body { @include text-size(normal); }.caption {
font-size: 0.875rem;
}
.lead {
font-size: 1.25rem;
font-weight: 600;
}
.body {
font-size: 1rem;
}
Truthiness
In Sass, only false and null are falsy. Everything else is truthy — including 0, empty strings "" and empty lists (). That differs from JavaScript.
| Value | Truthy? |
|---|---|
false, null |
No |
true |
Yes |
0, 0px |
Yes |
"" (empty string) |
Yes |
() (empty list) |
Yes |
Choosing a readable text colour
A practical example: pick black or white text depending on how light a background is. color.channel() reads the lightness of a colour in the HSL space.
@use "sass:color";
@function text-on($bg) {
@if color.channel($bg, "lightness", $space: hsl) > 60% {
@return #1a1a1a;
} @else {
@return white;
}
}
$swatches: (sun: #ffd54f, sea: #1565c0, rose: #cc6699);
@each $name, $bg in $swatches {
.swatch-#{$name} {
background: $bg;
color: text-on($bg);
}
}.swatch-sun {
background: #ffd54f;
color: #1a1a1a;
}
.swatch-sea {
background: #1565c0;
color: white;
}
.swatch-rose {
background: #cc6699;
color: white;
}
Combining conditions
Use and, or and not:
$theme: dark;
$high-contrast: true;
body {
@if $theme == dark and $high-contrast {
background: black;
color: white;
} @else if $theme == dark {
background: #1e1e1e;
color: #ddd;
} @else if not $high-contrast {
background: #fafafa;
}
}body {
background: black;
color: white;
}
The old if() function
Older code often uses the if($condition, $if-true, $if-false) function for a short choice inside a value:
$compact: true;
.toolbar { padding: if($compact, 4px, 12px); } // padding: 4pxNote: Current Dart Sass deprecates this function form (it prints "The Sass if() syntax is deprecated in favor of the modern CSS syntax"), because CSS itself is gaining an
if()function. In new code, use an@ifinside a mixin or@functioninstead.
@if vs CSS conditions
@if runs while compiling, so it can't react to the browser — screen size, user preferences or classes added by JavaScript. For those, output CSS conditions (@media, @supports, :hover, .is-open) instead.
Test yourself
Which values are falsy in Sass?
- false, null, 0 and ""
- Only false and null
- Only false
- false, null and ()
When is an @if condition evaluated?
- When the page loads
- Each time the window is resized
- When Sass compiles the stylesheet
- When a user clicks
How do you write 'else if' in Sass?
- @elseif
- @else if
- @elif
- else if