Sass @else Rule

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
If / else branch

What You’ll Learn

Sass @else is the fallback block after an @if. This page focuses on the simple two-way pattern @if … @else …: themes, variants, and function returns. Multi-condition @else if chains are covered on a separate page.

01

Fallback

@else

02

Pair

After @if

03

Exclusive

One branch

04

Themes

Light / dark

05

Variants

Primary / ghost

06

Practice

5 examples

What Is @else?

Official docs: an @if rule can optionally be followed by an @else rule, written @else { … }. This rule’s block is evaluated if the @if expression returns false (more precisely: if it is falsey).

  • Must come right after an @if (same chain).
  • Runs only when the preceding @if condition fails.
  • Exactly one of @if / @else runs in a two-branch pair.
  • Compile-time only—never appears in the CSS file.
💡
Beginner tip

@if alone can skip styles. Add @else when you need a guaranteed alternate: “if light theme … otherwise dark theme.”

📝 Syntax

styles.scss
@mixin theme-colors($light-theme: true) {
  @if $light-theme {
    background-color: #f2ece4;
    color: #036;
  } @else {
    background-color: #6b717f;
    color: #d2e1dd;
  }
}

The @else block is attached to the @if with } @else { (same line or next). It is not a second, separate rule floating elsewhere in the file.

🤝 How @else Pairs With @if

Review @if for truthiness: only false and null are falsey. When the @if condition is falsey, Sass evaluates the @else block instead.

@if conditionWhat runs
Truthy@if block only
Falsey@else block only

⚖️ @else vs a Second @if

Two separate @if blocks can both emit CSS if both conditions pass. An @if / @else pair is mutually exclusive—ideal for opposites like primary vs ghost, filled vs outline, or light vs dark.

styles.scss
// Exclusive: only one branch
@if $primary { … } @else { … }

// Not exclusive: both can run
@if $primary { … }
@if not $primary { … } // easy to drift out of sync

📄 What About @else if?

When you need more than two outcomes (up / right / down / left, multiple sizes, and so on), Sass lets you chain @else if conditions before a final @else. That multi-branch pattern has its own tutorial so this page can stay focused on the clear two-way @else fallback.

💡
Scope of this page

Master @if + @else for binary choices first. Continue with the @else if page when you need a longer decision chain.

⚡ Quick Reference

GoalCode
Two-way branch@if $x { … } @else { … }
Theme switch@if $light { … } @else { … }
Variant styles@if $primary { … } @else { … }
Function return@if $halve { @return … } @else { @return … }
Optional onlyUse plain @if (no else)
Many conditionsUse @else if (separate page)

Examples Gallery

Each example uses a simple @if / @else pair (no @else if chains). Open View Compiled CSS for verified output.

📚 Getting Started

Binary choices: one outcome or the other.

Example 1 — Light / Dark Theme

Official-style mixin: light colors in @if, dark colors in @else.

styles.scss
$light-background: #f2ece4;
$light-text: #036;
$dark-background: #6b717f;
$dark-text: #d2e1dd;

@mixin theme-colors($light-theme: true) {
  @if $light-theme {
    background-color: $light-background;
    color: $light-text;
  } @else {
    background-color: $dark-background;
    color: $dark-text;
  }
}

.banner {
  @include theme-colors($light-theme: true);

  body.dark & {
    @include theme-colors($light-theme: false);
  }
}

How It Works

true takes the @if branch; false takes @else. Each include emits exactly one color pair.

Example 2 — Primary vs Ghost Button

Shared layout, exclusive fill styles in each branch.

styles.scss
@mixin button($primary: true) {
  display: inline-block;
  padding: 0.5rem 1rem;
  border-radius: 4px;

  @if $primary {
    background: #036;
    color: #fff;
    border: 1px solid #036;
  } @else {
    background: transparent;
    color: #036;
    border: 1px solid #036;
  }
}

.cta {
  @include button(true);
}

.ghost {
  @include button(false);
}

How It Works

Padding and radius are shared. The @else branch supplies the outline look without duplicating the whole mixin.

📈 Functions, Density & Content

Return values and content switches with a clean fallback.

Example 3 — @else Inside a Function

Return one of two computed sizes.

styles.scss
@use "sass:math";

@function half-or-full($size, $halve: false) {
  @if $halve {
    @return math.div($size, 2);
  } @else {
    @return $size;
  }
}

.box {
  width: half-or-full(100px);
  height: half-or-full(100px, true);
}

How It Works

Default path returns the full size via @else. Passing true takes the half branch instead.

Example 4 — Density Flag + Icon Fill

Config and mixin both use exclusive @else fallbacks.

styles.scss
$prefers-dense: false;

.card {
  @if $prefers-dense {
    padding: 0.5rem;
    gap: 0.25rem;
  } @else {
    padding: 1.25rem;
    gap: 0.75rem;
  }
}

@mixin icon($filled: false) {
  width: 1.25rem;
  height: 1.25rem;

  @if $filled {
    background: currentColor;
  } @else {
    border: 2px solid currentColor;
    background: transparent;
  }
}

.star {
  @include icon(true);
}

.outline {
  @include icon();
}

How It Works

Dense mode is off, so the card takes the roomy @else spacing. Icons choose fill vs outline the same way.

Example 5 — Uppercase vs Normal Content

Transform a string in one branch; leave it alone in the other.

styles.scss
@use "sass:string";

@mixin label-case($text, $uppercase: false) {
  @if $uppercase {
    content: string.to-upper-case($text);
    letter-spacing: 0.06em;
  } @else {
    content: $text;
    letter-spacing: normal;
  }
}

.upper {
  &::before {
    @include label-case("hello", true);
  }
}

.normal {
  &::before {
    @include label-case("hello");
  }
}

How It Works

The @else path is the default “leave it alone” behavior; the @if path opts into uppercase styling.

🚀 Real-World Use Cases

  • Theme pairs — light vs dark token sets.
  • Component variants — primary vs secondary, filled vs outline.
  • Density modes — compact vs comfortable spacing.
  • Function defaults — return A or B from a helper.
  • Content transforms — optional uppercase / truncated labels.

🧠 How Compilation Works

1

Evaluate @if

Sass checks whether the condition is truthy or falsey.

Check
2

Pick one branch

Truthy → @if block; falsey → @else block.

Choose
3

Skip the other

The unused branch emits no CSS.

Exclusive
4

CSS ships

Only the winning branch’s styles appear in the output.

⚠️ Common Pitfalls

  • Orphan @else — it must follow an @if in the same chain.
  • Two @if instead of @else — conditions can both pass and double-emit styles.
  • Assuming 0 is falsey@else still follows Sass truthiness rules.
  • Stuffing many cases into @else — when you need three+ outcomes, use @else if.
  • Forgetting shared styles — put common declarations outside the @if / @else pair.

💡 Best Practices

✅ Do

  • Use @else for mutually exclusive alternatives
  • Keep shared properties outside both branches
  • Name flags clearly ($light-theme, $primary)
  • Default the common case in @if or the parameter default
  • Link back to @if when teaching truthiness

❌ Don’t

  • Use @else when you only need optional extra styles
  • Duplicate entire mixins for two variants
  • Hide a long keyword list inside one @else
  • Expect @else to run in the browser
  • Mix unrelated concerns in the fallback branch

Key Takeaways

Knowledge Unlocked

Five things to remember about Sass @else

The exclusive fallback when an @if condition fails.

5
Core concepts
🤝 02

After @if

same chain

Pair
⚖️ 03

Exclusive

one side wins

Binary
🎨 04

Variants

themes & modes

API
05

Compile

CSS from winner

Output

❓ Frequently Asked Questions

An @else block follows an @if and runs only when that @if expression is falsey. It provides the alternate branch of a two-way compile-time choice.
No. @else must immediately follow an @if (or, on another page, an @else if chain). It is not a standalone at-rule.
In a simple @if / @else pair, exactly one block is evaluated: the @if block if the condition is truthy, otherwise the @else block.
No. @else is the final fallback with no extra condition. @else if adds another tested condition and is covered on a separate tutorial page.
No. Like @if, it is compile-time only. Browsers see only the styles from the branch that ran.
Use @else when the two outcomes are mutually exclusive (light vs dark, primary vs ghost). Two separate @if blocks can both run if you are not careful.
Did you know?

Official Sass docs introduce @else right after @if with a theme mixin—because light/dark is the classic mutually exclusive pair where a bare @if is not enough.

Conclusion

@else completes a two-way compile-time choice: when @if fails, the fallback branch runs instead. Use it for exclusive variants and themes; continue with @else if when you need more than two outcomes.

Continue with @else if or @if.

Next: Sass @else if

Chain multiple conditions for keyword APIs and token scales.

@else if rule →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful