Sub topic 4
Variables for reusable values
A Sass variable stores a value under a name so you can define it once and use it in many places. This chapter covers how to declare variables, which value types they hold, how scope works, how !default and !global change that behavior, and how Sass variables differ from CSS custom properties.
It assumes you can write plain CSS (selectors, properties, media queries) and compile a file with the sass command, as shown in the setup chapter.
Declaring and using a variable
A variable name starts with $. You assign a value with a colon, and you end the declaration with a semicolon, as you would a CSS property.
$primary: #3366cc;
$font-stack: system-ui, sans-serif;
$radius: 4px;
.button {
font-family: $font-stack;
border: 1px solid $primary;
border-radius: $radius;
}Compiling this produces plain CSS. The variables are gone, and each use is replaced by its value:
.button {
font-family: system-ui, sans-serif;
border: 1px solid #3366cc;
border-radius: 4px;
}Two naming rules matter in practice:
- Hyphens and underscores are interchangeable in Sass identifiers.
$font-sizeand$font_sizerefer to the same variable. Pick one style and stay with it. Hyphens are the usual choice. - A declaration must come before any use of the variable in the file's evaluation order. Sass reads top to bottom.
Value types
A variable can hold any Sass value, not just a color or a length.
| Type | Example |
|---|---|
| Number, with or without a unit | `$gap: 1.5rem;` or `$ratio: 1.25;` |
| String | `$family: "Inter";` |
| Color | `$brand: #3366cc;` |
| Boolean | `$debug: false;` |
| Null | `$shadow: null;` |
| List | `$breakpoints: 40rem 64rem 80rem;` |
| Map | `$colors: (primary: #3366cc);` |
Maps get their own chapter. For now, treat a list as a sequence of values separated by spaces or commas.
A variable whose value is null makes any property that uses it disappear from the output. This lets you toggle declarations on and off:
$shadow: null;
.card {
padding: 1rem;
box-shadow: $shadow;
}This compiles to .card { padding: 1rem; }. The box-shadow line is omitted because its value is null.
Variables can build on each other
A variable's value can be an expression that uses other variables. This keeps related values connected, so changing one input updates the others.
$base-gap: 0.5rem;
$card-padding: $base-gap * 2;
$border-color: #3366cc;
.card {
padding: $card-padding;
border: 1px solid $border-color;
}Arithmetic like $base-gap * 2 has a few rules about division and units. The chapter on operators and sass:math covers them. Here you only need to know that multiplying a length by a plain number works as you would expect.
Scope: where a variable can be seen
A variable declared at the top level of a file is global to that file. A variable declared inside a block (curly braces) is local and exists only inside that block.
Assigning to a name that already exists globally, from inside a block, creates a new local variable. It does not change the global one. The local variable shadows the global one for the rest of that block.
$size: 1rem;
.a {
$size: 2rem;
font-size: $size;
}
.b {
font-size: $size;
}.a {
font-size: 2rem;
}
.b {
font-size: 1rem;
}The $size: 2rem inside .a never touched the global value, so .b still gets 1rem.
Changing a global from inside a block with !global
If you really want to overwrite the global value from within a block, add the !global flag:
$size: 1rem;
.a {
$size: 2rem !global;
font-size: $size;
}
.b {
font-size: $size;
}Now both .a and .b get 2rem. Sass evaluates the file top to bottom, so everything after .a sees the changed value, and anything before it does not.
Default values with !default
The !default flag assigns a value only if the variable is not already defined, or is null. Library and configuration files use it to supply fallbacks that the caller can override.
// _button.scss
$radius: 4px !default;
$padding: 0.5rem 1rem !default;
.button {
padding: $padding;
border-radius: $radius;
}// main.scss
@use 'button' with ($radius: 8px);Compiling main.scss yields a .button rule with border-radius: 8px and padding: 0.5rem 1rem. The with clause set $radius before the module's own !default assignment ran, so that assignment was skipped. $padding was not configured, so its default applied.
Two limits apply to with:
- Only variables declared with
!defaultcan be configured this way. Configuring any other variable is a compile error. - A module can be configured only the first time it is loaded. The module system chapter explains why.
Private variables
A name that begins with - or _ is private to the file that declares it when other files load it with @use. Use this for internal helper values you do not want callers to depend on.
// _tokens.scss
$space: 8px;
$-internal-gap: $space * 3;Another file can read tokens.$space, but tokens.$-internal-gap is an error.
Sass variables versus CSS custom properties
CSS has its own variables, called custom properties (--name). They look similar but work at different times.
| Row | Sass variable | CSS custom property |
|---|---|---|
| Syntax | `$primary` | `var(--primary)` |
| Resolved | At compile time | In the browser, at runtime |
| Present in output CSS | No | Yes |
| Can change per element or by media query | No | Yes, through the cascade |
| Can be used in Sass math and functions | Yes | No, the value is opaque to Sass |
| Can be changed by JavaScript | No | Yes |
The two work well together. Sass can hold the source value and emit it as a custom property. Writing a Sass variable into a custom property needs interpolation (#{}), because Sass treats custom property values as raw text:
$primary: #3366cc;
:root {
--primary: #{$primary};
}
.link {
color: var(--primary);
}Without #{}, the output would contain the literal text $primary, and the browser would ignore it.
Use a Sass variable when you need the value at build time. Examples are computing a darker shade, using it in a media query, or generating class names. Use a custom property when the value must change in the browser, for example for dark mode or user theming.
Common errors
- Undefined variable. The message
Undefined variable.means the name is misspelled, is declared later in the file, or lives in a module you have not loaded. With@use, remember the namespace:tokens.$space, not$space. - Unexpected shadowing. You assign to a name inside a block and expect the global to change. It does not unless you use
!global. - Too many variables. A variable used once adds a name to remember without saving anything. Create a variable when a value repeats, or when it carries meaning that a bare number does not, such as
$header-height. - Raw
$namein the output. This usually means you placed a variable in a custom property value or an unquoted selector context without interpolation.
Exercise
A file starts with $gap: 8px;. It then has the rule .a { $gap: 16px; margin: $gap; }, followed by .b { margin: $gap; }. What margin does each rule get in the compiled CSS? What changes if the line in .a becomes $gap: 16px !global;?
Show answer
As written, .a gets margin: 16px and .b gets margin: 8px, because the assignment inside .a creates a local variable that shadows the global one. With !global, .a still gets 16px, but the global value is overwritten, so .b, which is evaluated afterward, also gets 16px.
Exercise
A partial _theme.scss declares $accent: #cc3333 !default;. Write the line in main.scss that loads it and sets the accent color to #3366cc. Why would the same line fail if the partial had declared $accent: #cc3333; without !default?
Show answer
@use 'theme' with ($accent: #3366cc);. Configuration through with works only for variables declared with !default. Without the flag, Sass reports an error because the variable is not configurable.