TExcelTAD
Home › Sass › Sass @if/@else

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.

scss
@if <condition> {
  …
} @else if <condition> {
  …
} @else {
  …
}

A simple condition

scss · try it yourself
$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.

scss · try it yourself
@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.

scss · try it yourself
@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:

scss · try it yourself
$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:

scss
$compact: true;
.toolbar { padding: if($compact, 4px, 12px); }   // padding: 4px

Note: 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 @if inside a mixin or @function instead.

@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?

  1. false, null, 0 and ""
  2. Only false and null
  3. Only false
  4. false, null and ()

When is an @if condition evaluated?

  1. When the page loads
  2. Each time the window is resized
  3. When Sass compiles the stylesheet
  4. When a user clicks

How do you write 'else if' in Sass?

  1. @elseif
  2. @else if
  3. @elif
  4. else if