Sub topic 4
Variables for reusable values
This chapter assumes you can compile a .scss file with the Sass command line and that you know core CSS. It covers how Sass variables are declared, how scope works, and where they differ from CSS custom properties.
Declaring and using a variable
A Sass variable is a name starting with $ that holds a value. You assign it with a colon and use it anywhere a value is allowed.
$primary: #3366cc;
$radius: 4px;
$font-stack: "Helvetica Neue", Arial, sans-serif;
.card {
border: 1px solid $primary;
border-radius: $radius;
font-family: $font-stack;
}The compiler replaces each variable with its value. The CSS output contains no variables:
.card {
border: 1px solid #3366cc;
border-radius: 4px;
font-family: "Helvetica Neue", Arial, sans-serif;
}Variables can hold numbers with units, colors, strings, lists, booleans, null, and maps. Hyphens and underscores in names are treated as the same character, so $font-stack and $font_stack refer to one variable. Pick one style and keep to it.
Scope
A variable declared at the top level of a file is global to that file. A variable declared inside a block, such as a rule or a mixin, is local to that block. Using a local variable outside its block causes an Undefined variable error.
If you assign a name inside a block that already exists globally, Sass creates a new local variable. The global one is unchanged.
$accent: #3366cc;
.a {
$accent: #cc3333;
color: $accent;
}
.b {
color: $accent;
}Here .a compiles to color: #cc3333 and .b compiles to color: #3366cc. To change the global variable from inside a block, add the !global flag. Most projects avoid this, because it makes the final value depend on compile order.
Default values
The !default flag assigns a value only if the variable is not already set.
$radius: 8px !default;Library authors use this so that users can override settings. When you load a module with @use, you can pass your own value through with. That mechanism is covered in the chapter on @use and @forward.
Sass variables versus CSS custom properties
CSS has its own variables, called custom properties, such as --primary. They look similar but behave differently.
| Feature | Sass variable | CSS custom property |
|---|---|---|
| Resolved | At compile time | In the browser, at runtime |
| Visible in output CSS | No | Yes |
| Changeable per element or media query | No | Yes |
| Usable in Sass math and functions | Yes | Not directly |
A Sass variable cannot change when a user switches to dark mode, because the browser never sees it. A common pattern is to keep design tokens in Sass and write them into custom properties where runtime changes are needed.
$primary: #3366cc;
:root {
--primary: #{$primary};
}The #{} wrapper is interpolation. Sass does not evaluate expressions inside a custom property value, so without the wrapper the output would contain the text $primary.
Common errors
- Undefined variable. The variable is used before it is declared, declared in another block, or declared in a file that was not loaded.
- Unexpected override. A local assignment shadows a global variable with the same name. Use distinct names for locals.
- Expecting runtime behavior. Changing a Sass variable cannot affect a page that is already loaded.
Exercise
A file declares $gap: 1rem; at the top level. A rule then contains $gap: 2rem; margin: $gap;. A later rule contains padding: $gap;. What value does each of margin and padding get?
Show answer
margin gets 2rem, because the assignment inside the rule creates a local variable that shadows the global one. padding gets 1rem, because the global variable was never changed. Adding !global to the assignment inside the first rule would make the later padding also compile to 2rem.