The Sass @if rule is written @if <expression> { … } and controls whether its block gets evaluated — including emitting any styles as CSS.
The expression usually returns true or false. If it is truthy, the block runs; if it is falsey, the block is skipped. This page focuses on single-branch @if. Two-way fallbacks use @else; multi-branch chains use @else if.
Official pattern.
Per the Sass docs: use @if inside mixins for optional knobs (for example $circle) so styles compile only when needed.
Compile-time only.
Browsers never see @if — only CSS from blocks that ran.
Truthiness surprise.
Only false and null are falsey. Empty strings, empty lists, and 0 are truthy in Sass.
Prefer explicit comparisons when zero should mean “off.”
Edge Cases & Pitfalls
Most @if bugs are truthiness assumptions or expecting runtime behavior.
truthy
0 and "" are truthy
Only false and null fail. Write @if $n != 0 when zero should skip a block.
runtime
Not a browser condition
@if cannot react to hover, scroll, or media at runtime. Use CSS for those.
else
Need an alternate branch?
Use @else or @else if — this page stays on single-branch optional styles.
css if()
Different from CSS if()
CSS is gaining its own if(). Sass @if is compile-time and never ships to the browser.
Toolchain
Sass Support
@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 @if.
Remember
Key Takeaways
Branch:@if <expr> { … } runs only when truthy.
Falsey: only false and null.
Combine: use and, or, and not in conditions.
Need a fallback? Continue with @else / @else if.
One line:@if is a compile-time switch — truthy runs the block; falsey skips it.
Compile
Compile Notes
@if never reaches the browser. Conceptual flow:
flow.txt
1. Evaluate the expression
2. Decide truthiness (only false and null fail)
3. Truthy → evaluate the block and emit CSS
4. Falsey → skip the block entirely
5. Continue compiling the rest of the stylesheet
Frequently Asked Questions
It controls whether its block is evaluated (including emitting CSS). If the expression is truthy, the block runs; if it is falsey, the block is skipped.
Only false and null. Every other value is truthy—including 0, empty strings, and empty lists.
Yes. Conditional expressions may use boolean operators: @if $a and $b { … }, @if $a or $b { … }, @if not $a { … }.
No. @if is compile-time only. Browsers see only the styles from blocks that evaluated to true.
@if decides whether one block runs. @else provides the alternate branch when the @if condition fails. See the dedicated Sass @else Rule page for two-way branches.
Inside mixins and functions for optional styles, feature flags, keyword checks, and any compile-time branch based on arguments or config variables.