Chepter 4: Sass Variables

Sass Variables — the single most loved feature that makes almost everyone switch from plain CSS to Sass.

I’m going to explain this like we’re building a real mini-project together — very detailed, step-by-step, with lots of live examples, gotchas, modern 2026 best practices, and why variables still beat native CSS custom properties in many real-world cases.

1. What Are Sass Variables? (Super Simple First)

A Sass variable is just a named box where you store a value once, and then reuse that name everywhere instead of repeating the value.

  • You write: $primary: #6c5ce7;
  • Then everywhere you want that purple color → just write $primary

Magic moment: Change the value in one place → entire project updates automatically after recompile.

This solves the classic pain: “Wait… our brand blue is used 147 times… now marketing wants a new shade… kill me.”

2. Basic Declaration Syntax (SCSS – the one we use)

SCSS
  • Name must start with $
  • Value can be almost anything Sass understands (colors, numbers, strings, lists, maps, functions…)
  • No !important or CSS-specific junk — just pure values

3. Using Variables (Everyday Examples)

SCSS

See? Clean, readable, maintainable.

4. Scope – Global vs Local (Very Important in 2026)

  • Global: Declared outside any {} block → available everywhere in that module/file (and to files that @use it)
  • Local: Declared inside a block (rule, mixin, function) → only lives inside that block

Example:

SCSS

Pro tip: Avoid shadowing unless you really mean it — can confuse you later.

5. !default Flag – The Library Hero (Modern Sass loves this)

Used in libraries/frameworks so users can override defaults without editing core files.

SCSS

Now in your main file:

SCSS

→ You customized the library without touching its code. Beautiful!

6. Variable Interpolation (Where You Can & Cannot Use Variables)

You can put variables almost anywhere:

  • In values: color: $primary;
  • In calculations: padding: $spacing * 2;
  • In selectors (with #{}):
    SCSS

But you cannot interpolate inside the variable name itself:

SCSS

7. Sass vs CSS Variables (Custom Properties) – 2026 Honest Comparison

Feature Sass Variables $ CSS Variables –var Winner for large projects?
Value set once, compile-time Yes No – runtime Sass
Can use in selectors/math fully Yes Limited (calc() only in some places) Sass
Color functions (darken etc.) Built-in rich set color-mix(), relative color syntax Sass (easier still)
Live change per element No Yes (great for themes) CSS
No build step needed No (needs compiler) Yes CSS
Scoped to component easily With modules (@use) With :root or shadow DOM Tie

Verdict in 2026: Use Sass variables for design tokens, colors, spacing scales, typography in big projects/libraries. Use CSS custom properties for dynamic runtime theming (dark mode per-user, etc.). Many teams use both together!

8. Modern Best Practices (2026 Edition)

  • Put all variables in _variables.scss or _tokens.scss
  • Use @use ‘variables’; in other files (never old @import)
  • Prefer maps over tons of separate variables
  • Use kebab-case or snake_case — Sass treats $font-size and $font_size the same
  • Add units to numbers when needed ($spacing: 16px !default;)
  • Use !default everywhere in shared/library code
  • Avoid re-assigning globals without !global (and even then — sparingly)

Quick Challenge for You

Try this in your editor:

  1. Create _colors.scss
SCSS
  1. In style.scss
SCSS

Compile → see the override in action!

You’ve now mastered Sass variables — the foundation of everything else (mixins, maps, themes…).

Next class — want to go into Sass Maps + Lists (super useful for themes/buttons)? Or color functions with real examples? Or how to structure variables in a medium-sized project?

Just say the word — I’m here! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *