Sass Variables

Beginner
9 min read
Updated: Nov 2026
5 examples
Dart Sass

Definition and Usage

Sass variables store a value under a name that starts with $. Declare with $name: value;, then reuse the name anywhere a value is allowed.

Compiled away.

Unlike CSS custom properties, Sass variables do not appear in the CSS output. They are replaced with their values at compile time.

Imperative.

If you use a variable and later change it, earlier uses keep the old value. CSS variables are declarative and can update both earlier and later uses.

!default for libraries.

Assigns only if the variable is undefined or null. Configure with @use … with (...).

Syntax

SCSS
$base-color: #c6538c;
$border-dark: rgba($base-color, 0.88);

.alert {
  border: 1px solid $border-dark;
}

$brand: #036 !default;     // only if unset / null
$theme: dark !global;      // assign global from a local block

@use "library" with (
  $black: #222
);

Pattern: declare $name → use it in values → use !default for configurable defaults → respect scope (global vs local).

Common Patterns

What you will use most often:

PatternResult idea
$name: value;Declare / overwrite a variable
$name: value !default;Set only if undefined or null
$name: value !global;Assign the existing global from a block
Top-level vs inside { }Global vs local scope
@use "lib" with (...)Override a library’s !default vars
$font-size / $font_sizeSame variable (hyphen = underscore)

SCSS Examples

Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).

Example 1 — Official Declare and Use

Store a color, derive another value, use both in CSS.

SCSS
$base-color: #c6538c;
$border-dark: rgba($base-color, 0.88);

.alert {
  border: 1px solid $border-dark;
}

How It Works

  1. $base-color holds the brand color.
  2. $border-dark builds on it with rgba.
  3. CSS only sees the final computed value.
  4. Matches the official Sass docs sample.

Example 2 — Official Imperative Updates

Changing a variable later does not rewrite earlier uses.

SCSS
$variable: value 1;
.rule-1 {
  value: $variable;
}

$variable: value 2;
.rule-2 {
  value: $variable;
}

How It Works

  1. .rule-1 captured value 1.
  2. Reassignment only affects later code.
  3. Unlike CSS vars, earlier uses do not update.
  4. Matches the official Sass vs CSS sample.

Example 3 — Official Global vs Local Scope

Top-level variables are global; inside a block they are usually local.

SCSS
$global-variable: global value;

.content {
  $local-variable: local value;
  global: $global-variable;
  local: $local-variable;
}

.sidebar {
  global: $global-variable;
  // $local-variable is not in scope here
}

How It Works

  1. Top-level $global-variable is visible everywhere after.
  2. $local-variable lives only inside .content.
  3. Shadowing: a local can reuse a global name safely.
  4. !global writes the existing global from a block.

Example 4 — !default Keeps the First Value

A later !default does not overwrite an already-set variable.

SCSS
$brand: #036 !default;
$brand: #c6538c !default;

.button {
  background: $brand;
}

How It Works

  1. First !default sets $brand to #036.
  2. Second !default is skipped (already defined).
  3. Useful when a consumer sets values first.
  4. Also assigns when the current value is null.

Example 5 — Official Configure with @use … with

Override a library’s top-level !default variables when loading it.

SCSS
$black: #000 !default;
$border-radius: 0.25rem !default;
$box-shadow: 0 0.5rem 1rem rgba($black, 0.15) !default;

code {
  border-radius: $border-radius;
  box-shadow: $box-shadow;
}
SCSS
@use "library" with (
  $black: #222,
  $border-radius: 0.1rem
);

How It Works

  1. Library exposes configurable !default vars.
  2. @use with overrides before the module runs.
  3. $box-shadow still uses the configured $black.
  4. Matches the official configure-modules sample.

Edge Cases & Pitfalls

Most variable bugs are mixing up Sass vars with CSS custom properties, or fighting scope.

CSS

Sass $vars ≠ CSS --vars

Need runtime theming in the browser? Use CSS custom properties (often injected via declarations with #{ }).

scope

Locals do not leak

Variables inside { } stay local unless you use !global on an already-declared global.

#{}

No dynamic variable names

Cannot write $theme-#{$name}. Use a map and map.get().

@if

Declare before flow-control assignment

In @if / loops, assign to an outer variable that already exists (even as null).

Sass Support

Variables are core Sass; module configuration needs Dart Sass @use.

FeatureStatus
$name: valueSupported
!default / !globalSupported
@use … with (...)Dart Sass 1.23+
Hyphen = underscore namesSupported

Compile with the Dart Sass CLI.

Key Takeaways

  • $name: value stores a compile-time value.
  • Compiled away—not the same as CSS -- variables.
  • !default + @use with configure libraries.
  • Scope matters—locals stay local; use maps for dynamic names.

One line: use $variables to name values once, keep them unit-clean, and configure shared libraries with !default.

Compile Notes

Practical checklist:

checklist.txt
1. Declare $name: value; near the top (or in a module)
2. Reuse $name in property values and calculations
3. Use !default for library-configurable settings
4. Configure with @use "lib" with ( $var: value )
5. For dynamic names, use a map—not $name-#{$key}

Frequently Asked Questions

Names that start with $ and hold a value. You declare $name: value; then use $name anywhere that value is allowed.
Sass variables are compiled away and have one value at a time (imperative). CSS custom properties stay in the CSS and can differ per element (declarative).
It assigns a value only if the variable is undefined or null. Libraries use it so users can configure values before the library runs.
It assigns to the global variable from inside a local block. The global variable must already exist at the top level.
No. Sass treats hyphens and underscores as the same in identifiers, so both names refer to one variable.
No. Use a map and map.get() when you need dynamic names, instead of $theme-#{$name}.

Next: Sass Comments

Write silent and loud comments that document your variables and mixins.

Comments →

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.

8 people found this page helpful