@property and typed custom properties

Advanced · 11 min

How @property gives a custom property a type, an initial value and an inheritance rule, so the browser can animate it and reject bad values.

Before this:is(), :where() and :has()Custom properties

Web Dev Simplified · 11 min

@property and typed custom properties

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Web Dev SimplifiedReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • An ordinary custom property is an untyped string of tokens. The browser substitutes it but cannot interpolate it, validate it or know whether it inherits.
  • `@property --name { syntax: '<length>'; inherits: false; initial-value: 0px; }` registers it. `syntax` and `inherits` are required, and `initial-value` is required unless the syntax is `*`.
  • Once typed, a property can be transitioned and animated: a gradient angle, a colour stop position, a number that drives a `counter()`.
  • A registered property gives the browser something to check a value against. A wrong-typed value no longer passes through as if it were fine, though in Chrome it does not quietly fall back to the `initial-value` either, so it is a debugging aid, not a safety net.
  • `inherits: false` scopes a property to the element it is set on, which is both a cleaner default for component internals and cheaper for the browser.

Variables with no type

A plain custom property is only a bag of tokens. When you write --angle: 90deg, the browser stores the text 90deg and nothing else. It does not know that this is an angle, so it cannot do the things CSS does for angles: interpolate between two of them, reject red as nonsense, or decide whether children should receive it.

That is usually fine. It is the flexibility that lets a custom property hold 0 0 10px or half a selector. The limitation shows up when you want the property to behave like a real CSS value.

Registering a type

@property --angle {
  syntax: "<angle>";
  inherits: false;
  initial-value: 0deg;
}

Three descriptors:

  • syntax: the type, written like "<length>", "<number>", "<color>", "<percentage>", "<angle>", a union like "<length> | <percentage>", a custom keyword list like "small | large", or "*" for anything.
  • inherits: whether children inherit it. This is required, with no default.
  • initial-value: the value when nothing sets it. Required, unless syntax is "*". It must be computationally independent, so 0px is fine but 1em or var(--x) is not.

An invalid @property is dropped as a whole, so a missing inherits quietly leaves the property unregistered and nothing animates, which is a frustrating thing to debug.

What it unlocks

Animation and transitions

.card {
  --glow: 0deg;
  background: conic-gradient(from var(--glow), tomato, gold, tomato);
  transition: --glow 600ms;
}
.card:hover { --glow: 180deg; }

(with @property --glow registered as <angle>). Gradients cannot be animated directly because the browser does not interpolate between two gradient images, but an angle inside one can be animated if the browser knows it is an angle. I checked in Chrome that a registered <angle> property under a 1 second transition from 0 to 90deg showed 45deg at the halfway point, while the same transition on an unregistered property produced no animation at all.

The same trick animates a gradient’s colour stop position (<percentage>) or a ring’s progress (<number> or <percentage>). It also feeds the counter() trick: animate an <integer> and display it with counter-reset and content: counter(n).

Validation

@property --gap {
  syntax: "<length>";
  inherits: true;
  initial-value: 1rem;
}

The initial-value is a real default: with nothing setting --gap, padding-left: var(--gap) is 1rem without writing var(--gap, 1rem) at every use. A wrong-typed value behaves differently from what you might hope. I set --gap: red on a registered <length> property in Chrome 157, and padding-left: var(--gap) computed to 0px: the declaration was invalid at computed-value time, which is the same trap custom properties describes for untyped ones, and it did not use the 1rem initial value. So registration gives a type to check against, and tools can use that, but you still have to supply the right kind of value.

Scoping and cost

inherits: false keeps a property local to the element where it is set. For internals of a component (a hover progress, a spin angle), that stops children from picking it up by accident, and the browser has less to recompute on change. Use inherits: true for theme-level values like an accent colour.

From JavaScript

CSS.registerProperty({
  name: '--angle',
  syntax: '<angle>',
  inherits: false,
  initialValue: '0deg',
});

This registers the same thing. The name can only be registered once per document, and a second attempt throws. Prefer the stylesheet rule so the information lives with the CSS.

Support and fallbacks

@property is in Chrome 85, Safari 16.4 and Firefox 128 per MDN’s compatibility data. In a browser without it, the property is simply a plain custom property. An animated gradient would then jump instead of rotate, which is an acceptable degradation if the page still works. If it is not acceptable, test for the feature before relying on it. The supports lesson goes through the options.

What the video covers

Web Dev Simplified’s “@property Is One Of The Coolest New CSS Features” (September 2024, about 11 minutes) calls @property one of the most important recent additions, with the potential to be as significant as grid and flexbox, but says some tooling changes need to happen first, and links a VS Code issue about editor support. I could not read a transcript and the description lists no timestamps, so which demos it builds are not described. Tooling support has probably moved since September 2024. The scoping details here are from the specification and MDN. The animation and bad-value checks were run in Chrome 157, and I confirmed there that a rule with no inherits descriptor is dropped (the property then behaved as an ordinary inheriting one), while inherits: false gave a child the initial value.

Where this fits

Typed properties extend custom properties and give keyframe animations something new to animate. The next lesson covers two newer layout features: anchor positioning and popovers.

Check yourself

  1. 01Why can't you animate `--angle` in `@keyframes` unless it is registered?

    The browser treats an unregistered custom property as an untyped token string. To interpolate between two values it must know their type (angle, length, number, colour). Without that, the value just flips at the halfway point. `@property` provides the `<angle>` type, so intermediate values can be computed.

  2. 02You register `--gap` as `<length>` with `initial-value: 1rem`, then set `--gap: red` and use `padding-left: var(--gap)`. What should you expect?

    `red` is not a length, so the substitution is invalid. In my test in Chrome 157 the padding came out as `0px`, the initial value of `padding-left`, not the `1rem` from the registration. The registration does not repair bad values. It makes the mistake show up consistently, and the `initial-value` applies when nothing sets the property at all.

  3. 03What is the difference between `@property` and `CSS.registerProperty()`?

    They register the same thing. `@property` is the CSS rule form and lives in your stylesheet. `CSS.registerProperty({ name, syntax, inherits, initialValue })` does it from JavaScript, once per name for the document. Use the CSS rule unless registration has to be computed at runtime.

  4. 04When would you choose `inherits: true`?

    When the property is meant to flow down as part of a theme, such as a global `--accent` that nested components read. For internal values of one component, like a transition's progress, `inherits: false` stops them leaking into children and is cheaper.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark