Web Development

CSS Custom Properties: Variables You Can Change in One Place

Flat illustration of CSS variables feeding colours and spacing into a page layout

A CSS custom property is a variable you define in CSS and reuse in other rules. You define it with a name that starts with two hyphens, such as --main-colour, and you use it with the var() function, such as color: var(--main-colour). When you change the definition, every rule that uses that variable picks up the new value.

That single change point is the whole point. A site with one main colour written in many rules needs many edits when the colour changes, and one missed rule leaves an odd colour behind. A site that defines the colour once needs one edit, and the change reaches headings, links, buttons and borders together. This guide shows how to define variables, where to put them, and how to override them for one section.

How custom properties work

A custom property behaves like any other CSS property in one important way: it follows the cascade and it is inherited. A value defined on an element is available to that element and to its descendants, unless a closer rule overrides it. That is why a definition on :root, which represents the top of the document, reaches the whole page.

The value itself is stored as text until it is used. A custom property can hold a colour, a length, a font stack, or a longer value, and the browser checks that value only when a rule calls it with var(). This makes variables flexible, and it also means a spelling mistake in a variable name can stay quiet until you look at the element that should have used it.

:root {
  --main-colour: #1d4ed8;
  --text-colour: #111827;
  --spacing: 16px;
}

h1 {
  color: var(--main-colour);
}

p {
  color: var(--text-colour);
  margin-bottom: var(--spacing);
}

In this example, the three values live in one block at the top of the stylesheet. The heading rule and the paragraph rule only refer to the variable names, so a later change to --main-colour changes every heading that uses it. The guide to the CSS box model explains how spacing values like the one above affect layout, and the same variable can feed padding and margins across a page.

Define variables in one place

Start with a small set of variables for values that repeat. Main and secondary colours, text colour, background colour, border colour, main spacing and a heading font are common choices. A small set is easier to remember than a long list, and it keeps the stylesheet readable when you return to it later.

Name variables by their job, not by their current value. A name like --main-colour still makes sense if the colour changes from blue to green, while a name like --blue becomes misleading after the change.

:root {
  --main-colour: #1d4ed8;
  --page-background: #ffffff;
  --section-gap: 32px;
}

section {
  background: var(--page-background);
  padding: var(--section-gap);
}

Put the definitions near the top of your stylesheet, in one block, so a reader can see the full set at a glance. If variables are scattered through the file, the benefit of one change point is lost, because a reader has to search the file to find which definition is in force. The guide to CSS Flexbox and Grid uses shared spacing in a similar way, and a variable set makes that spacing consistent across layout sections.

Change a value for one section

Override a variable inside a narrower selector when one section needs a different look. A dark callout box, for example, can set its own background and text variables, and every element inside that box will use the new values. The rest of the page keeps the original values, because the override only applies inside that box.

This works because inheritance follows the document structure. A child element looks for the closest definition of a variable, starting from itself and moving up through its ancestors. The callout definition wins inside the callout, and the :root definition wins everywhere else. You do not need to rewrite the rules for headings or paragraphs inside the callout.

.callout {
  --page-background: #111827;
  --text-colour: #f9fafb;
  background: var(--page-background);
  color: var(--text-colour);
  padding: var(--section-gap);
}

This pattern is useful for a footer, a banner, or a highlighted card. It also keeps the markup clean, because the section only needs one class, and the variable change does the rest of the work. The guide to CSS animations and transitions shows another place where a shared value helps, since a transition duration stored in a variable can stay consistent across hover effects.

Custom properties compared with plain values

ApproachHow a change is madeWhere it helps
Plain repeated valueEvery rule that uses the value is edited by hand.A value used once, in one rule.
Custom property on :rootOne definition is edited, and every use follows.Colours and spacing shared across a page.
Overridden custom propertyOne definition inside a section is edited.A section with its own colour scheme.
Fallback in var()The fallback is used when the variable is missing.A component reused in different pages.

The table shows a simple rule for choosing. Use a plain value when the value appears once, use a shared variable when it appears in several places, and use an override when one section breaks from the shared look. A fallback belongs in the second case as well when a component might be used without the shared definitions in place.

Common mistakes

The most common mistake is a spelling mismatch between the definition and the use. CSS variable names are case sensitive, so --Main-Colour and --main-colour are different variables. When a style does not apply, check the spelling in both places before changing anything else, because the browser will not warn you about a missing variable unless you inspect the element.

A second mistake is forgetting the fallback. Without a fallback, a missing variable makes the whole declaration invalid, so the element falls back to whatever value it inherited or to the browser default. That can look like a broken layout when the real problem is one missing definition. Adding a fallback such as var(--main-colour, #1d4ed8) keeps the element styled while you track down the missing definition.

A third mistake is creating a variable for every value in the stylesheet. A stylesheet full of variables used once is harder to read than the plain values it replaced, because a reader has to jump between the definition and the use to know what is happening. Keep the set small, name variables by purpose, and leave single-use values as plain values.

Limits of custom properties

Custom properties are a CSS feature, so they only affect styling. They cannot store data for JavaScript logic on their own, and they cannot replace a value that comes from content or from a server. A variable can hold a colour or a length, and it cannot hold a paragraph of text that should appear on the page as content.

Custom properties also depend on the cascade, which means an unexpected override can change a value in a place you did not intend. A definition on a parent element affects its descendants, so a variable set for one component can leak into nested content if that content sits inside the same element. Keep overrides close to the section they belong to, and check nested elements when a value looks wrong.

Worked example: a theme built from variables

Custom properties let a stylesheet name its design decisions once and reuse them everywhere. Define colours, spacing and font choices on the root element, then refer to them with the var function wherever they are needed. Changing a brand colour then means editing one line, and every button, heading and link that uses the variable updates together. That single point of change is the main reason to adopt custom properties, even on a small site.

:root {
  --brand: #2563eb;
  --text: #1f2937;
  --spacing: 16px;
}
.button {
  background-color: var(--brand);
  color: #fff;
  padding: var(--spacing);
}

Custom property names start with two hyphens, and they are case sensitive, so --brand and --Brand are different variables. That sensitivity catches beginners who rename a variable in one place and miss the capitalisation in another. Pick a naming habit early, such as lowercase words joined by hyphens, and apply it consistently, so a variable can be found by searching for its exact name.

Fallback values make variables safer to use. Writing var(--brand, #2563eb) means the browser uses the fallback colour when the variable is not defined, instead of leaving the property unset. Fallbacks are most useful for variables that might be defined only in some themes or some sections, where an undefined variable would otherwise produce a missing style with no warning.

Scoped variables: change a value for one section

A custom property defined on the root applies everywhere, but a property redefined on a narrower element applies only inside that element and its descendants. That scoping is what makes them more flexible than a preprocessor variable, which is fixed when the stylesheet is built. A dark section, for example, can redefine the text and background variables for its own subtree, and every component inside that section adapts without any change to the component rules themselves.

.dark-section {
  --text: #f9fafb;
  --surface: #111827;
}
.dark-section .card {
  color: var(--text);
  background-color: var(--surface);
}

Read that example as a theme applied by scope. The card rules never mention dark or light; they only refer to variables, and the section supplies the values. Adding a new theme later means adding a new scope with new values, not rewriting every component rule. That separation between component rules and theme values is the design benefit that keeps a growing stylesheet manageable.

Scoping also means a variable can be shadowed by accident. A rule that redefines a variable for a broad element, such as a section that wraps most of the page, changes the value for everything inside it, including components that expected the root value. When a component suddenly shows the wrong colour, check whether an ancestor redefined the variable before assuming the component rule itself is wrong.

Variables that change with JavaScript

Because custom properties live in the rendered stylesheet, JavaScript can read and change them at runtime, which opens effects that a static stylesheet cannot express. A theme toggle, for example, can switch a set of variables on the root element, and the whole page restyles at once, because every component already refers to those variables. The script only changes values; it does not need to know which elements use them.

const root = document.documentElement;
root.style.setProperty("--brand", "#059669");

That single line changes the brand colour everywhere it is used, which is far simpler than selecting every branded element and setting its style individually. The same technique supports user preferences, such as a font size setting or a spacing choice, where the preference is stored as a variable value and the page layout follows it automatically.

Keep the JavaScript side minimal. The script should set values, not rebuild styles, and the stylesheet should hold the rules that use those values. When the logic of which value applies lives in the script and the presentation lives in the stylesheet, each side stays readable, and a change to the design does not require touching the script.

Limits and mistakes to watch for

Custom properties are not a replacement for every stylesheet decision. A variable holding a single value used in exactly one place adds indirection without benefit, because a reader must now look in two places to understand one rule. Reserve variables for values that repeat, values that change by theme or scope, or values that JavaScript needs to change. A value used once can stay as a plain value in the rule that uses it.

Variables also cannot be used everywhere a stylesheet value appears. A property name cannot be a variable, and a variable cannot supply only part of a value in every situation, though it can supply a whole value or a whole component of a shorthand in many cases. When a variable-based rule does not work, check whether the variable is being asked to do something the syntax does not support, rather than assuming the variable itself is undefined.

Finally, custom properties inherit like other properties, which is usually what you want, but it means a variable set on a parent flows into children whether or not the child rule mentions it. That inheritance is the mechanism that makes scoping work, and it is also the mechanism behind accidental shadowing. Reading the computed values in developer tools shows the effective value for an element, which settles most variable mysteries quickly.

Frequently asked questions

Can a custom property hold any kind of CSS value?

Yes, a custom property can hold a colour, a length, a font name, or a longer value such as a shadow list. The value is only checked when it is used with var(), so a mistake in the definition may stay hidden until that property is applied.

What happens if a custom property is missing?

If the property is missing and no fallback is given, the declaration that uses it becomes invalid at that point. Give a fallback inside var() for values that must always work, and define the property in a scope that covers every use.

Should every repeated value become a custom property?

No. Turn a value into a custom property when it is shared across a page or when it is likely to change as a group. A value used once, in one rule, is usually clearer left as a plain value.

Are CSS custom properties the same as variables in a preprocessor?

No. Preprocessor variables are resolved when the stylesheet is built, and the result is fixed. Custom properties live in the rendered page, so they can be scoped to a section, changed by JavaScript, and updated at runtime without rebuilding anything.

What happens when a custom property is not defined?

The var function can supply a fallback value, which is used when the variable is missing. Without a fallback, the property that used the variable is left unset, and the browser falls back to its inherited or initial value for that property.

Sources