Sub topic 9
Registering Typed Properties with @property
This chapter assumes you can declare custom properties and read them with var(). It also assumes you know how inheritance and the cascade work, and that you understand how var() substitution behaves. Those topics are covered in earlier chapters.
Why plain custom properties are untyped
A normal custom property such as --angle: 45deg is stored as a token stream. The browser does not know it is an angle. It keeps the text and substitutes it wherever var(--angle) appears.
That has three consequences:
- The browser cannot validate the value when you declare it. Any text is accepted.
- The browser cannot interpolate between two values. A transition on
--anglejumps from one value to the other. - Every custom property inherits by default, and the only default is "guaranteed-invalid" unless you supply a fallback.
The @property at-rule fixes these by registering a property with a type, an inheritance rule, and a default value.
The @property rule
@property --angle {
syntax: '<angle>';
inherits: false;
initial-value: 0deg;
}The rule has three descriptors:
| Descriptor | Meaning |
|---|---|
| `syntax` | The type the value must match, written as a string. Examples: `'<angle>'`, `'<length>'`, `'<color>'`, `'<number>'`, `'<percentage>'`, `'*'`. |
| `inherits` | `true` or `false`. Controls whether child elements inherit the value. |
| `initial-value` | The value used when nothing else sets the property. |
syntax and inherits are required. initial-value is required unless the syntax is '*'. If the descriptors are invalid, the browser ignores the whole rule and the property stays unregistered.
The initial-value must be computationally independent. That means it cannot depend on other values, so 1em is rejected for <length> and 10px is accepted.
You can combine types with | and allow lists:
@property --size {
syntax: '<length> | <percentage>';
inherits: false;
initial-value: 0px;
}
@property --shadow-colors {
syntax: '<color>+';
inherits: false;
initial-value: black;
}What registration changes
Registration changes three behaviors.
- Validation. If you assign a value that does not match the syntax, the declaration is invalid at parse time and is dropped. The property then falls back to inherited or initial value instead of carrying bad text.
- Computed values. Relative units resolve early. A registered
<length>set to2emcomputes to an absolute length on the element where it is declared, so children inherit the resolved pixels and not the2emtext. - Interpolation. The browser knows how to blend two values of the type, so transitions and animations work.
@property --gap {
syntax: '<length>';
inherits: true;
initial-value: 8px;
}
.row {
--gap: 2em; /* resolves against .row's font-size */
}
.row .item {
margin-right: var(--gap); /* uses the resolved pixel value */
}.row {
--gap: red; /* invalid for <length>: dropped */
}In the second block the declaration is discarded at parse time. --gap keeps its inherited or initial value.
Registering from JavaScript
You can also register a property in script. This is useful in libraries.
if (window.CSS && CSS.registerProperty) {
try {
CSS.registerProperty({
name: '--angle',
syntax: '<angle>',
inherits: false,
initialValue: '0deg',
});
} catch (error) {
console.warn('Property already registered or invalid:', error.message);
}
}A property can be registered only once. A second call with the same name throws an InvalidModificationError, so wrap it as shown. Note the JavaScript option is initialValue, in camel case.
Choosing inherits
Set inherits: false unless children need the value. Non-inheriting properties are cheaper for the browser to recompute, and they stop a value set on an ancestor from leaking into a nested component. Use inherits: true for design tokens that should flow down the tree, such as a spacing unit.
A worked example: a gradient you can animate
A gradient is not interpolated directly. Its color stops are part of an image value. If the stop position is a registered <percentage>, you can animate it.
@property --stop {
syntax: '<percentage>';
inherits: false;
initial-value: 0%;
}
.bar {
height: 1rem;
background: linear-gradient(90deg, #0b5fff var(--stop), #dde3f0 var(--stop));
transition: --stop 0.6s ease;
}
.bar:hover {
--stop: 100%;
}Without the @property rule, the gradient changes in one step on hover. With it, the fill grows smoothly. The next chapter covers animation in more detail.
Common errors
- Using
emininitial-value. Relative units are rejected. Use absolute units. - Forgetting
inherits. The rule is ignored without it. - Registering after use. Registration is global to the document. A property that was already declared untyped is typed once the rule is parsed, which can change how existing declarations compute.
- Expecting
var()fallbacks to fix type errors. A mismatched value is dropped at parse time, so the fallback invar()is not involved.
Exercise
You register --size with syntax: '<length>', inherits: false, and initial-value: 10px. A rule then sets --size: large;. What is the computed value of --size on that element, and why?
Show answer
It is 10px. The value large does not match <length>, so the declaration is dropped at parse time. Because inherits is false and nothing else sets it, the property uses its initial-value.
Exercise
Write an @property rule for --hue that holds a number, does not inherit, and starts at 200. Then say what you need to add to a rule for it to animate smoothly.
Show answer
@property --hue { syntax: '<number>'; inherits: false; initial-value: 200; }. To animate it, add transition: --hue 0.3s; (or a keyframe animation) to the element and change --hue in a state such as :hover.