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
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).
Changing a variable later does not rewrite earlier uses.
SCSS
$variable: value 1;
.rule-1 {
value: $variable;
}
$variable: value 2;
.rule-2 {
value: $variable;
}
Compiled CSS
.rule-1 {
value: value 1;
}
.rule-2 {
value: value 2;
}
How It Works
.rule-1 captured value 1.
Reassignment only affects later code.
Unlike CSS vars, earlier uses do not update.
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
}
Compiled CSS
.content {
global: global value;
local: local value;
}
.sidebar {
global: global value;
}
How It Works
Top-level$global-variable is visible everywhere after.
$local-variable lives only inside .content.
Shadowing: a local can reuse a global name safely.
!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.
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.
Practice
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}.