CSS Variable (Custom Property)

A CSS variable, officially called a custom property, is a reusable value you define once and reference throughout your stylesheet. They are written with two dashes and read with var().

Web

Defining values like brand colors or spacing as variables lets you change them in one place and update the whole site. They are commonly declared on :root so they're available everywhere.

Unlike preprocessor variables, CSS custom properties are live in the browser and can even be changed at runtime with JavaScript, making them great for theming.

Examples

CSS

:root {
  --brand: #6c5ce7;
}
.button {
  background: var(--brand);
}

Output:

Sets a reusable brand color and applies it to the button's background.

Learn it in a lesson

See also

Back to the Dictionary