Sub topic 10
Typed and animatable tokens with @property
This chapter assumes you can declare custom properties, read them with var(), and explain how they inherit. It also assumes you know the basic cascade, including how a value can be invalid at computed-value time. It builds on the spacing and scale chapter, where calc() derived tokens from a base value.
Why untyped tokens fall short
A normal custom property has no type. The browser stores whatever tokens you wrote and checks them only when a property uses them through var(). This causes two problems.
- A wrong value, such as
--space-4: banana, is accepted without complaint. It fails later, inside the property that consumes it. - The browser cannot animate the token. It does not know that
220is a number, so a transition switches from one value to the other with no in-between steps.
The @property at-rule fixes both. It registers a custom property with a syntax, an inheritance setting, and an initial value.
Registering a token
@property --accent-hue {
syntax: "<number>";
inherits: false;
initial-value: 220;
}A registration has three descriptors.
| Descriptor | Meaning |
|---|---|
| `syntax` | The type the value must match, such as `"<number>"`, `"<length>"`, `"<color>"`, or `"<percentage>"`. `"*"` accepts anything. |
| `inherits` | Whether child elements inherit the value. This is required. |
| `initial-value` | The value used when nothing sets the property. It is required unless the syntax is `"*"`. |
If syntax or inherits is missing, the whole rule is ignored. The property then stays an ordinary untyped custom property, and no error appears.
Animating a token
Once --accent-hue is a <number>, the browser can interpolate it.
@property --accent-hue {
syntax: "<number>";
inherits: false;
initial-value: 220;
}
.button {
background: hsl(var(--accent-hue) 80% 50%);
transition: --accent-hue 0.3s ease;
}
.button:hover {
--accent-hue: 260;
}On hover, the hue moves from 220 to 260 over 0.3 seconds, and the background color shifts smoothly through the hues between them. Without the registration, the color would jump at once.
This works because the transition is on the token, not on background. Many properties can read one animated token, so a single transition can drive several colors together.
Validation and the initial value
For a registered property, an invalid value is rejected when the declaration is parsed. The declaration is dropped, and the cascade continues with the next valid value or the initial value.
@property --space-unit {
syntax: "<length>";
inherits: true;
initial-value: 0.25rem;
}
:root {
--space-unit: 4;
}
.card {
padding: calc(var(--space-unit) * 4);
}Here 4 is a number, not a length, so the --space-unit: 4 declaration is discarded. The token keeps its initial value of 0.25rem, and .card gets 1rem of padding. This beats the untyped case, where calc(4 * 4) would make padding invalid at computed-value time, which resets it to its default.
The initial value must be computationally independent. That means 0.25rem is fine, but var(--other) or 2em is not, because em depends on context. A bad initial value makes the whole rule invalid.
Choosing `inherits`
Design tokens usually need inherits: true, so a theme set on :root or [data-theme] reaches components. Use inherits: false for values that belong to one element, such as a hover hue. This also makes the browser cheaper to run, because a change on one element does not trigger style recalculation in all its descendants.
Common errors
- Missing
inherits. The registration is silently ignored, and animation does not work. - Registering inside a selector.
@propertyis a top-level rule. It cannot sit inside a rule block or be scoped. - Wrong syntax type. A color token written as
"<number>"rejects#2563eb. - Registering twice. If the same name is registered more than once, the last rule wins.
- Browser support. Older browsers ignore
@property. Because your tokens still work as untyped properties there, tokens should be usable without animation.
Exercise
A token is registered with syntax: "<length>" and initial-value: 1rem. A stylesheet sets --gap: 12;. What value does --gap have on that element, and why?
Show answer
It is 1rem. The value 12 is a number, not a length, so it does not match the registered syntax. The declaration is dropped at parse time, and the property falls back to its initial value (or to an earlier valid declaration, if one exists in the cascade).
Exercise
A hover transition on --accent-hue makes the button color jump instead of fading. The @property rule has syntax: "<number>" and initial-value: 220. What is the most likely cause?
Show answer
The inherits descriptor is missing. Without it, the whole @property rule is ignored, so --accent-hue stays an untyped property and cannot be interpolated. Adding inherits: false; fixes it.