# Custom Properties (CSS Variables)

> Store reusable values like colors and spacing in variables to build flexible, scalable themes.

Subject: CSS | Lesson 14 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/css/custom-properties
Previous: [Color Formats & OKLCH](https://codewithanimation.in/css/color-formats-and-oklch.md) | Next: [Backgrounds & Gradients](https://codewithanimation.in/css/backgrounds-and-gradients.md)

## What it is

Custom properties (commonly called CSS variables) allow you to define a specific value once—like a brand color or a standard padding size—and reuse it everywhere in your CSS.

They always begin with two dashes (like `--brand-color`) and are accessed using the `var()` function.

## Why it matters

Without variables, updating a design is a nightmare. If your brand color changes, you have to use 'Find and Replace' across hundreds of files, hoping you don't accidentally replace the wrong thing.

With custom properties, you update the value in exactly one place (usually the `:root` selector), and every button, link, and border in your entire application updates instantly.

## How it works

You define a custom property inside a selector to set its scope. Usually, we put global variables in `:root` so the entire HTML document can access them.

```css
:root {
  --primary: #3b82f6;
  --spacing-lg: 2rem;
}
```

Then, you apply them using the `var()` function:

```css
.button {
  background-color: var(--primary);
  padding: var(--spacing-lg);
}
```

## Common mistake: The find-and-replace disaster

Before CSS variables, developers used standard text search to update colors. If you wanted to change your grey text (`#333`) to slightly darker grey (`#222`), you might accidentally overwrite a `#333` border that was supposed to remain unchanged.

Custom properties give your values semantic meaning. `--text-main` and `--border-light` might both equal `#333` today, but they can be changed independently tomorrow.

## Tip: They are alive in the browser

Unlike older tools like Sass or LESS variables (which get compiled away into static CSS before the browser ever sees them), CSS custom properties are alive in the DOM.

This means you can change them using JavaScript (`element.style.setProperty('--primary', 'red')`) to create dynamic themes or animations in real-time.

## Try it: Try it

Change the `--brand` and `--radius` variables and watch the entire component system update instantly.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/custom-properties

## Check yourself

### Question 1

How do you apply a custom property named '--highlight' to a background color?

- A. background-color: var(--highlight);
- B. background-color: --highlight;
- C. background-color: $highlight;

Correct answer: A

Native CSS requires the `var()` function to evaluate and apply the custom property's stored value.

- Why not B: You must wrap the property name inside the var() function.
- Why not C: The $ prefix is used in Sass/SCSS, not native CSS custom properties.

### Question 2

Which prefix is mandatory when defining a CSS custom property?

- A. -- (two dashes)
- B. $ (dollar sign)
- C. @ (at symbol)

Correct answer: A

All CSS custom properties must begin with two dashes (--) to ensure they never conflict with future standard CSS properties that browsers might introduce.

- Why not B: The dollar sign is used by the Sass preprocessor.
- Why not C: The at symbol is used for CSS at-rules like @media or @keyframes.

### Question 3

What is a major advantage of native CSS variables over Sass/LESS variables?

- A. They exist in the DOM and can be updated live with JavaScript
- B. They compile to smaller file sizes
- C. They don't require the var() function

Correct answer: A

Because CSS custom properties are alive in the browser, you can read and modify them at runtime using JavaScript, making dynamic theming (like Dark Mode toggles) incredibly easy.

- Why not B: They don't affect file size significantly. Sass variables actually disappear entirely from the final CSS.
- Why not C: Sass doesn't require var(), but native CSS does.

## Remember this

- Define custom properties with a `--` prefix.
- Use them by wrapping the name in the `var()` function.
- Place global variables in the `:root` selector.
- Variables are live in the DOM and inherit down the element tree.
