Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 39 ยท Variables, Nesting and Partials

Sass Variables

Master Sass variables: declare values with $, reuse them everywhere, understand scope, !default and !global flags, and combine them with CSS custom properties.

A variable is a name for a value you want to reuse. In Sass every variable starts with a dollar sign. You assign it with a colon, just like a CSS property, and you use it anywhere a value is allowed. Change the variable once and every place that uses it updates the next time you compile. This is the single feature that convinces most people to adopt Sass.

Declaring and using variables

SCSS
$brand: #0ea5e9;
$font-stack: "Inter", system-ui, sans-serif;
$gap: 16px;

body {
  font-family: $font-stack;
  color: $brand;
  padding: $gap;
}

Variables can hold any Sass value: numbers with units, colors, strings, lists, maps, booleans and null. Data types and operators lists them all.

Naming rules

  • Names are case-sensitive: $Brand and $brand are different.
  • Hyphens and underscores are treated as the same character: $main-color and $main_color are one variable. Pick hyphens (the CSS style) and stay consistent.
  • Choose names that say what the value means ($color-danger) rather than what it is ($red). If the design changes red to orange, the name still makes sense.

Variables in more places than values

You can use a variable as an argument, in a calculation, in a media query condition, and inside strings or selectors with interpolation:

SCSS
@use "sass:math";

$breakpoint-md: 768px;
$columns: 12;

@media (min-width: $breakpoint-md) {
  .col { width: math.div(100%, $columns); }
}

Division uses math.div() from the built-in sass:math module; Data types and operators explains why. (In a real file the @use line goes at the very top.)

Variables in action

Default values with !default

Variables are often configuration: a library offers $primary and lets the user override it. The !default flag assigns a value only if the variable is not already set (or is null).

SCSS
// _library.scss
$primary: #0ea5e9 !default;
$radius: 4px !default;

If the file that uses the library sets $primary first, that value wins; otherwise the default applies. With the module system you pass the overrides when you load the module, using @use "library" with ($primary: #7c3aed);, which Module configuration covers. Every well-built Sass library marks its public settings with !default.

Scope: where a variable exists

Variables declared at the top of a file are global to that file (and to others through modules). Variables declared inside a block, such as a rule or a mixin, are local: they exist only inside that block.

SCSS
$color: red;          // global

.card {
  $color: blue;       // new local variable, shadows the global one
  border-color: $color;   // blue
}

.badge {
  border-color: $color;   // red, the global is untouched
}

Assigning to a name inside a block creates a new local variable that shadows the global one; it does not overwrite it. That surprises people coming from JavaScript, where assignment changes the outer variable.

Changing a global from inside a block with !global

If you really need to modify the global variable from inside a block, add the !global flag:

SCSS
$theme-count: 0;

@mixin register-theme {
  $theme-count: $theme-count + 1 !global;
}

Use it sparingly: mutating globals makes code hard to follow. When you see !global in the wild, it is usually a counter or an accumulator in a library. Prefer functions that return values.

Inner blocks can read variables from outer blocks. Control-flow blocks (@if, @each, @for, @while) are special: assigning to an existing variable inside them updates that variable instead of shadowing it, which is how loops build up a value:

Scope and shadowing

Variables and CSS custom properties

Sass variables and CSS custom properties (--brand) look alike but live in different worlds:

Sass variable CSS custom property
Exists At compile time only In the browser, at run time
Syntax $brand --brand / var(--brand)
Changeable by JavaScript No Yes
Can differ per element No Yes (inheritance, themes)
Can be used in Sass math Yes No (just text)

A good pattern is to define a token in Sass and expose it as a custom property. Because Sass treats the contents of a custom property as raw text, you must use interpolation #{ } to inject a Sass value into it:

Sass values inside custom properties

Without #{ }, the browser would receive the literal text $primary. Sass does the substitution; JavaScript or a class on <html> can then re-point --primary at run time. Design tokens and theming builds a whole system on this idea.

A variable that is only used once is rarely worth its own name. A variable that appears in a dozen places, or that the design team might change, always is.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Are $main-color and $main_color the same variable?

    Sass treats hyphens and underscores in identifiers as identical. Pick one style and stay consistent.
  2. 2. What does !default do in $radius: 4px !default;?

    !default lets a user or earlier file set the variable first. The assignment only applies when the variable is undefined or null.
  3. 3. Inside .card { $color: blue; } with a global $color: red, what is $color after the rule?

    Assigning inside a block creates a shadowing local. Use !global to modify the global variable.
  4. 4. How do you put a Sass variable into a CSS custom property?

    Custom property values are treated as plain text, so you need interpolation to insert the variable's value.

Summary

  • Variables start with $, can hold any Sass value, and are replaced by their values at compile time.
  • Hyphens and underscores are interchangeable in names; prefer meaningful names like $color-danger.
  • !default marks a configurable setting; !global changes a global from inside a block.
  • Inner assignments create shadowing local variables unless the name already exists in an enclosing local scope.
  • Expose Sass tokens as CSS custom properties with #{ } when values must change at run time.

Next: Data types and operators