Sub topic 10
Typed and animatable tokens with @property
This chapter assumes you can declare custom properties and read them with var(). It also assumes you know how they inherit and how fallback values work, as covered in earlier chapters. By the end you will register a custom property with a type. You will use that type to validate values and to animate tokens that a plain custom property cannot animate.
Why untyped tokens fall short
A normal custom property such as --accent-hue: 220 has no type. The browser stores it as a sequence of tokens and does not check what it means. It only interprets the value after var() substitutes it into a real property.
This causes two limits:
- No validation at the token.
--space: bananais accepted. The problem only shows up wherepadding: var(--space)becomes invalid. - No interpolation. To animate a value, the browser must know its type. It cannot tell whether
220and260are numbers, lengths, or something else. Atransitionon an unregistered custom property switches instantly at the start or end of the duration.
The @property at-rule fixes both. It registers a custom property with a syntax, an inheritance behavior, and an initial value.
The @property rule
A registration has three descriptors:
@property --accent-hue {
syntax: "<number>";
inherits: false;
initial-value: 220;
}| Descriptor | Meaning | Required |
|---|---|---|
| `syntax` | The type the value must match, written as a quoted string | Yes |
| `inherits` | Whether children inherit the value, `true` or `false` | Yes |
| `initial-value` | The value used when nothing is set | Yes, unless `syntax` is `"*"` |
If a required descriptor is missing or invalid, the browser ignores the whole rule. The property then stays an ordinary untyped custom property, and you get no error message.
Syntax strings
The syntax string uses CSS value type names in angle brackets. Common ones:
| Syntax | Accepts | Example value |
|---|---|---|
| `<number>` | Unitless numbers | `220` |
| `<integer>` | Whole numbers | `3` |
| `<length>` | Lengths | `1rem`, `12px` |
| `<percentage>` | Percentages | `50%` |
| `<length-percentage>` | A length or a percentage | `2rem`, `30%` |
| `<color>` | Any color | `#2563eb`, `hsl(220 80% 50%)` |
| `<angle>` | Angles | `45deg` |
| `<time>` | Durations | `300ms` |
| `*` | Anything (the untyped default) | any token stream |
You can combine types and keywords:
"<length> | auto"accepts either a length or the keywordauto."<length>+"accepts a space-separated list of lengths."<color>#"accepts a comma-separated list of colors."compact | comfortable"accepts only those two keywords.
What a valid initial-value must be
The initial-value must be computationally independent. It cannot contain var(). It also cannot use units that depend on context, such as em, % of something unknown, or currentColor. initial-value: 1em on a <length> property makes the rule invalid. Write 16px or 1rem instead. (rem is allowed because it depends only on the root element, but check support in your target browsers.)
What the type changes
Registration alters how the browser handles the value. These effects matter in practice.
Validation and fallback to the initial value
Take a registered padding token:
@property --card-padding {
syntax: "<length>";
inherits: false;
initial-value: 1rem;
}
.card {
padding: var(--card-padding);
}
.card--wide {
--card-padding: 2rem;
}
.card--typo {
--card-padding: big;
}For .card--typo, the declaration --card-padding: big does not match <length>. The browser rejects it at parse time, exactly like an invalid width: big. The declaration is dropped. The token keeps its initial value, 1rem, and the card is still padded correctly.
If the bad value arrives through var(), the check happens later:
.card--indirect {
--size: big;
--card-padding: var(--size);
}The browser can only test var(--size) after substitution. It finds a mismatch, and the declaration is invalid at computed-value time. The registered property then behaves as if it were unset. Because inherits is false here, that means the initial value, 1rem. With inherits: true it would take the parent's value instead.
An untyped token has no such safety net. The same mistake would make the whole padding declaration invalid, and the card would get no padding.
Values are resolved where they are declared
Registered values are computed at the element that declares them. Relative units become absolute lengths at that point:
@property --gap {
syntax: "<length>";
inherits: true;
initial-value: 0px;
}
.parent {
font-size: 20px;
--gap: 1em;
}
.child {
font-size: 10px;
margin-left: var(--gap);
}Here .child gets a left margin of 20px. The 1em was resolved to 20px on .parent, and the child inherits the computed length. If --gap were unregistered, the child would inherit the text 1em and resolve it against its own font size. The margin would be 10px. This is a frequent source of surprise when you register an existing token. If your design relies on late resolution of em, do not register that token as <length>.
Control over inheritance
Unregistered custom properties always inherit. A registered one can set inherits: false. This stops a value set on a container from leaking into every descendant. It also lowers style recalculation cost on large trees, because the browser has nothing to propagate. Use false for tokens that only apply to a single element, such as an animation progress value. Use true for tokens meant to flow down, such as a theme color.
Animating a token
Once the browser knows --accent-hue is a <number>, it can interpolate between two values. A transition on the property now produces intermediate steps:
@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, so the background color slides through the intermediate hues. The transition names the custom property itself, not background. The background is only a consumer that is recomputed on every frame.
Animating things that cannot normally animate
A common use is the angle of a gradient. The browser does not interpolate gradient images directly, but it can interpolate an angle:
@property --angle {
syntax: "<angle>";
inherits: false;
initial-value: 0deg;
}
.panel {
background: linear-gradient(var(--angle), #2563eb, #9333ea);
transition: --angle 0.5s ease;
}
.panel:hover {
--angle: 180deg;
}The gradient rotates smoothly from 0 to 180 degrees. The same pattern works with @keyframes: animate --angle from 0deg to 360deg for a continuous spin.
What does not animate
Interpolation requires a type that has a defined midpoint. Keyword syntaxes such as "compact | comfortable" are discrete. They flip at 50% of the transition. A token with syntax * never interpolates. If you need a smooth change, pick the narrowest numeric or length type that fits.
The following diagram shows how the browser handles a declaration for a registered property.
Registering from JavaScript
The same registration is available through the CSS Object Model:
CSS.registerProperty({
name: '--accent-hue',
syntax: '<number>',
inherits: false,
initialValue: 220,
});Registering the same name twice throws an InvalidModificationError. This is useful when a component library registers its tokens at load time. Wrap the call in try/catch if the script might run more than once. A CSS @property rule and a JavaScript registration for the same name also conflict, and the last registration takes effect for the page. Pick one method per token.
Practical guidance
- Register selectively. You do not need to register every token. Color and spacing scales usually work well untyped. Register the ones you animate, or the ones where a typo would be costly.
- Keep the registration near the token. Put
@propertyrules at the top level of your token stylesheet, next to the:rootblock that sets values. They are global to the document and cannot be scoped to a selector. - Choose the initial value deliberately. It is the fallback for every invalid assignment. A neutral, safe value such as
0or the base color keeps a broken override from wrecking the layout. - Match
inheritsto the tier. Primitive and semantic tokens usually inherit. Component-level animation values usually do not. - Plan for missing support. Current versions of the major browsers support
@property, but older ones do not. In a browser without support, the rule is ignored, the token stays untyped, and the transition jumps instead of animating. The page still works, so treat the animation as an enhancement. If you need to detect support, use@supports at-rule(@property)where available, or checktypeof CSS.registerProperty === 'function'in JavaScript.
Exercise
A stylesheet registers --size with syntax: "<length>", inherits: false and initial-value: 10px. A rule then sets --size: 2 on an element that uses width: var(--size). What width does the element get, and why?
Show answer
The element gets a width of 10px. The value 2 is a number, not a length, so it does not match the syntax. The browser drops the declaration at parse time, and the token keeps its initial value of 10px. Without registration, width: 2 would be invalid and the width would fall back to auto.
Exercise
You want a card's border color to fade smoothly from #2563eb to #dc2626 on hover using a token named --border. What must you add to make the transition work, and what does the transition declaration name?
Show answer
Register --border with @property, using syntax: "<color>", inherits: false and a valid initial-value such as #2563eb. Then set transition: --border 0.3s ease; on the card, naming the custom property rather than border-color. The hover rule sets --border: #dc2626, and border-color: var(--border) follows the interpolated value. Without registration the color would switch instantly.