TExcelTAD
Home › Sass › Sass Variables

Sass Variables

A Sass variable stores a value under a name that starts with $. Wherever you use the name, the compiler substitutes the value. Variables are the simplest and most-used Sass feature: they keep colours, font stacks, spacing and breakpoints in one place.

scss
$name: value;

Declaring and using variables

scss · try it yourself
$font-stack: "Inter", system-ui, sans-serif;
$text-color: #2d2a32;
$brand: #cc6699;
$space: 12px;

body {
  font-family: $font-stack;
  color: $text-color;
}

.badge {
  background: $brand;
  color: white;
  padding: $space * 0.5 $space;
  border-radius: $space;
}
body {
  font-family: "Inter", system-ui, sans-serif;
  color: #2d2a32;
}

.badge {
  background: #cc6699;
  color: white;
  padding: 6px 12px;
  border-radius: 12px;
}

A variable can hold any Sass value:

Type Examples
Number (with or without unit) 12, 1.5, 16px, 2rem, 50%
String "Inter", bold, sans-serif
Color #cc6699, rgb(204 102 153), teal
Boolean true, false
Null null (properties set to null are omitted)
List 4px 8px, Arial, sans-serif
Map (small: 4px, large: 16px)

Note: Hyphens and underscores are treated as the same: $font-size and $font_size refer to the same variable.

Scope: global vs local

A variable declared at the top level of a file is global. A variable declared inside a block ({ }) is local to that block and its nested blocks.

scss · try it yourself
$color: navy;          // global

.card {
  $color: crimson;     // local: shadows the global one
  border-color: $color;
}

.footer {
  color: $color;       // still navy
}
.card {
  border-color: crimson;
}

.footer {
  color: navy;
}

The local $color inside .card did not change the global value. If you really want to assign to the global variable from inside a block, add the !global flag: $color: crimson !global;. Use it sparingly — it makes styles harder to follow.

Default values with !default

!default assigns a value only if the variable is not already set (or is null). Libraries use it so that users can configure them before loading:

scss · try it yourself
$primary: #0d6efd !default;
$primary: #198754 !default;   // ignored: already has a value

.btn-primary {
  background: $primary;
}
.btn-primary {
  background: #0d6efd;
}

With the module system you override a library's !default variables using @use ... with (...), which you'll see in the Partials & @use/@forward lesson:

scss
@use "library" with ($primary: #cc6699);

Null values disappear

If a property's value is null, Sass simply leaves the declaration out. This is handy for optional settings:

scss · try it yourself
$shadow: null;
$radius: 6px;

.panel {
  box-shadow: $shadow;
  border-radius: $radius;
}
.panel {
  border-radius: 6px;
}

Sass variables are compile-time

Sass variables are replaced with their values when the CSS is generated. The browser never sees $brand, so you cannot change a Sass variable with JavaScript or in a media query at runtime. For values that must change in the browser (like a dark-mode toggle), use CSS custom properties (--brand) — often set from Sass variables. See the CSS Custom Properties vs Sass Variables lesson.

Tip: Name variables by purpose, not appearance: $color-danger survives a redesign better than $red.

Test yourself

What does $size: 20px !default; do if $size is already 16px?

  1. Changes it to 20px
  2. Keeps 16px
  3. Throws an error
  4. Creates a second variable

A variable declared inside .card { ... } is…

  1. Global
  2. Local to that block (and nested blocks)
  3. Available in every file
  4. Turned into a CSS custom property

What happens to color: $c; when $c is null?

  1. Outputs color: null;
  2. Compilation error
  3. The declaration is omitted
  4. Outputs color: ;