designwithfocus

CSS

CSS Custom Properties vs Variables: Two Names, One Feature

By Design With Focus Team 5 min read
A stylesheet snippet with a dashed custom property name beside a color swatch

Short answer

CSS custom properties and CSS variables are two names for the same feature. You declare a value under a name that starts with two dashes, then read it with the var() function. MDN also calls them cascading variables. A property written with two dashes inherits from its parent unless you register it with @property and turn inheritance off.

Last verified against the sources listed below.

Key takeaways

  • MDN treats custom properties, CSS variables, and cascading variables as one feature you declare and reuse.
  • A custom property name starts with two dashes. var() reads it. Names are case sensitive.
  • Two-dash properties inherit. @property can set a type, an initial value, and whether the property inherits.
  • var() belongs in property values. MDN says it does not work in media queries, container queries, selectors, or property names.
  • The W3C snapshot of this module is a Candidate Recommendation dated 16 June 2022. The editor's draft is still a working copy.

CSS custom properties vs variables is a naming question, not a feature fight. MDN’s guide says custom properties are sometimes called CSS variables or cascading variables: values an author defines once and reuses.

The function that reads them is var(). The declaration is either a name that starts with -- or the @property at-rule. The rest of this page is how those pieces differ in syntax, inheritance, and where a value is legal.

Are the two names the same feature?

Yes. MDN uses both names for one author-defined value. Custom properties are the entities. “CSS variables” is the other label on the same guide.

The W3C snapshot, a Candidate Recommendation dated 16 June 2022, introduces cascading variables as a value type that CSS properties accept, and custom properties as the way you define them. The editor’s draft says the same thing and warns that the draft is a working copy.

Name you will see What it refers to What you write
Custom property The author-defined property --main-bg-color: brown;
CSS variable MDN’s other name for that property var(--main-bg-color)
Cascading variable The spec’s name for the value type A value any property can accept
@property A registered custom property syntax, inherits, initial-value

MDN’s own sample sets --main-bg-color to the named color brown, then reads it as a background. The name is the reusable slot. The color is just one legal value.

How do you declare a custom property?

You declare it on a selector, with two dashes before the name, or you register it with @property. A two-dash name holds any valid CSS value.

section {
  --main-bg-color: brown;
}

@property is stricter. MDN’s example registers a color, turns inheritance off, and sets an initial value:

@property --logo-color {
  syntax: "<color>";
  inherits: false;
  initial-value: #c0ffee;
}

That registration is what lets you name a type, a default, and an inheritance choice. The two-dash form does not ask for those three fields. It just stores the value you wrote.

How does var() read the value?

var() substitutes the custom property where a property value would go. MDN’s sample uses it as a background color.

details {
  background-color: var(--main-bg-color);
}

You can pass a fallback as the second argument when the variable is missing or invalid. MDN says that fallback is not a way to support browsers that have never heard of custom properties. It only helps when the feature exists and this particular name does not have a usable value yet. Several fallbacks can be listed, which is useful when a custom element has not set the name.

Do custom properties inherit?

A property declared with two dashes inherits from its parent and takes part in the cascade. Case matters: MDN says --my-color and --My-color are different properties.

@property can turn that inheritance off. MDN’s --logo-color sample sets inherits: false, so a child does not pick up the parent’s value unless you set the property again. The same at-rule is also where an initial value lives, which is the value you get when nothing else has set the property.

The draft notes that this module focuses on var() as the way to make “variables,” and that custom properties can also be used as properties in their own right. Inheritance is the default only for the two-dash form. Registration is the opt-out.

The same draft says the CSS-wide keywords are legal inside a custom property. It also says the draft is a public copy of the editor’s work, provided for discussion, and that you should not cite it as anything but work in progress. The 16 June 2022 Candidate Recommendation Snapshot is the W3C document with a fixed date. When the two disagree, the snapshot is the stable citation and the draft is the newer working text.

Where can you not use var()?

var() is legal inside a property value, including as part of a longer value. MDN says you cannot use it for property names, selectors, or anything that is not a property value.

That ban covers media queries and container queries. A breakpoint cannot be stored in a custom property and then dropped into @media. If the query needs a length, write the length in the query, and keep the custom property for properties on elements.

The editor’s draft also says ordinary custom properties cannot be transitioned, while registered ones can. If a color or a length should animate, register it with @property so the type is known. An unregistered value is not something the transition machinery can interpolate.

Where does a fluid size fit?

A clamp() length can be stored in a custom property and read with var() anywhere a length is legal. The Fluid Typography calculator builds that clamp() size so you can paste it into a property such as --title.

The editor’s draft links a web-platform-tests dashboard for this feature. That page is a test list, not a support date, so this guide does not invent a year when browsers shipped the feature. How these declarations sit inside a stylesheet is part of what CSS is used for.

Frequently asked questions

Can two custom properties differ only by letter case?

Yes. MDN says custom property names are case sensitive, so --my-color and --My-color are different properties. A stylesheet that sets one of them does not fill the other. var() will not treat them as aliases. If a lookup misses, the function uses a fallback you wrote, or the property is invalid when no fallback exists. Pick one spelling and keep it, especially when several people edit the same file.

Do fallbacks fix old browsers that lack custom properties?

No. MDN says fallback values are not a compatibility patch for browsers that do not support custom properties. A fallback applies when the browser understands the feature and the variable is not defined yet or holds an invalid value. You can list more than one fallback inside var(), which helps when a custom element has not set the name. Support itself is a separate question from a missing value.

Can a custom property be animated?

The CSS variables draft says ordinary custom properties cannot be transitioned. Registered custom properties can. Registration is the @property at-rule, where you set a syntax, an inherits flag, and an initial value. Without that registration, the value is stored as text the cascade passes along, and a transition has no numeric type to interpolate. If you need a color or length to ease, register it first.

Is there a JavaScript way to define one?

MDN says you can define or work with custom properties from JavaScript through a matching API, and it points that work at the CSS Properties and Values API rather than spelling the methods on the custom-properties guide. In CSS itself, the two paths are the two-dash declaration and @property. The two-dash form is what inherits by default. The at-rule is what adds a type and an initial value.

Sources