Skip to content
BENIBIX Learn

CSS Custom Properties

Development🌐 Web DevelopmentBrowser Code

Also known as CSS Variables, CSS Variable, var(), --custom-property

Named values in CSS, like --brand, that you set once and reuse all over the page.

Think of it like

It is like a paint pot with a name. Change the paint in the pot, and every wall changes.

Example

Emma's site uses one teal in 40 places. She makes it --brand, so a new brand colour is now a one-line change.

Why it matters

Themes like dark mode become easy. You swap a few values, and the whole page follows.

Definition

CSS custom properties are values you name once and reuse across a stylesheet. The name starts with two dashes, like --brand. You read it back with var(--brand), and you can give a backup value after a comma. Most people set them on :root, so the whole page can use them. Change one line, and every place that uses it changes too.

Example code

css
:root { --brand: teal; }
.button { background: var(--brand); }
.link { color: var(--brand, blue); }
Set it once on :root, then read it with var(). The link falls back to blue if it is missing.
One custom property on :root feeds the button, the link and the heading.

Common mistake

Writing the name without var(), like color: --brand. That does nothing; you need var(--brand).

Key takeaways

  1. 1
    Two dashes. Names start with --.
  2. 2
    Read with var(). Add a backup after a comma.
  3. 3
    They pass down. Children get the value of their parent.

Compare

Learn it properly