Sub topic 9
Registering typed properties with @property
This chapter assumes you can declare and read custom properties with var(), and that you know how inheritance and the cascade work. It also assumes you have seen calc() with custom properties. You do not need JavaScript for most of it. The last section uses one short script.
Why plain custom properties have no type
A custom property declared with --name: value is not validated when you write it. The browser stores the value as a token stream, which is a sequence of raw pieces of text. It does not know whether --angle: 45deg is an angle, whether --size: 12px is a length, or whether --x: banana is a mistake.
This has useful consequences. You can store almost anything, such as a partial value, a list, or a fragment like 1px solid. It also causes three problems:
- No validation. A typo such as
--space: 8pis accepted. The error only shows up when a property that uses it throughvar()fails. - No interpolation. Transitions and animations need to know how to blend two values. For an untyped property, the browser cannot do this, so the value flips from start to end at once.
- Forced inheritance. Every plain custom property inherits. You cannot make one that applies only to the element where it is set.
The @property at-rule fixes all three. It registers a custom property with a syntax, an inheritance setting, and an initial value.
The @property rule
You write @property at the top level of a stylesheet. The name of the property follows it, then a block of descriptors.
@property --angle {
syntax: '<angle>';
inherits: false;
initial-value: 0deg;
}There are three descriptors:
| Descriptor | Purpose | Required |
|---|---|---|
| `syntax` | The type the value must match, as a quoted string | Yes |
| `inherits` | Whether children inherit the value (`true` or `false`) | Yes |
| `initial-value` | The value used when nothing sets the property | Only if `syntax` is not `'*'` |
If a required descriptor is missing, or the rule is otherwise invalid, the browser ignores the whole @property rule. The property then behaves as an ordinary untyped custom property. No error appears, so check the rule carefully when a registration seems to have no effect.
The syntax descriptor
The syntax string lists the types the value may take. Each type is written in angle brackets.
| Syntax string | Accepts |
|---|---|
| `'<length>'` | `12px`, `2rem`, `calc(1px + 2px)` |
| `'<number>'` | `0`, `1.5`, `-3` |
| `'<percentage>'` | `50%` |
| `'<length-percentage>'` | a length or a percentage |
| `'<color>'` | `red`, `#0b5fff`, `rgb(0 0 0 / 50%)` |
| `'<angle>'` | `45deg`, `0.5turn` |
| `'<time>'` | `200ms`, `1s` |
| `'<integer>'` | `1`, `-4` |
| `'<image>'` | `url(...)`, gradients |
| `'<url>'` | `url(...)` |
| `'*'` | any value, same as an unregistered property |
You can combine types in three ways:
- A pipe means "either":
'<length> | <percentage>'. - A literal keyword is allowed alongside types:
'auto | <length>'. - A trailing
+accepts a space-separated list:'<length>+'. A trailing#accepts a comma-separated list:'<color>#'.
Initial values must be computationally independent
The initial-value must be a value the browser can compute without looking at anything else. That means 0deg, 12px, #fff, and 2em are fine in most cases, but var(--other) is not. A relative unit such as em is allowed in some contexts, but a value that depends on the element, such as currentColor for a <color> property, is rejected. When you are unsure, use an absolute value.
Validation in practice
Once registered, the browser checks every value written to the property. A value that does not match the syntax is invalid at parse time. The declaration is dropped, as with any normal CSS property, and the property keeps the value from lower in the cascade or the initial value.
@property --gap {
syntax: '<length>';
inherits: true;
initial-value: 8px;
}
.row {
--gap: 16px;
}
.row.broken {
--gap: banana;
}
.row > * {
margin-right: var(--gap);
}For .row.broken, the banana declaration is thrown away. The value stays 16px from the .row rule. With an unregistered --gap, the value banana would be stored, and margin-right: banana would then fail when var() is substituted. In that case the margin falls back to its own initial value instead of 16px. Registration moves the failure closer to its cause, and it keeps the last good value.
Controlling inheritance
Plain custom properties always inherit. With inherits: false, a child only sees the value if a rule sets it on that child directly. Otherwise the child gets the initial-value.
This matters for components that nest. Suppose a card sets --accent and contains another card that does not.
@property --accent {
syntax: '<color>';
inherits: false;
initial-value: gray;
}
.card {
border: 2px solid var(--accent);
}
.card.promo {
--accent: crimson;
}A plain .card inside a .card.promo shows a gray border, because --accent does not flow from the parent. With an unregistered property, the inner card would inherit crimson and look promoted by accident. Setting inherits: false is a good default for properties that belong to a single component. Use inherits: true for design tokens that you set once on :root, such as a spacing unit.
Non-inheriting properties also cost less. When a value changes on an element, the browser does not need to check the whole subtree for new computed values.
Typed values and computed results
A registered property is computed at its declared type. This changes what var() returns in two ways.
First, relative units resolve early. If you register --size as <length> and set --size: 2em, the computed value is the pixel result in the context of the element where it is declared. A child with a different font size inherits the resolved length, not the text 2em. For an unregistered property, the child would re-resolve 2em against its own font size.
Second, calc() is evaluated. A registered <number> property set to calc(1 + 2) computes to 3.
@property --scale {
syntax: '<number>';
inherits: true;
initial-value: 1;
}
.panel {
--scale: calc(1 + 0.5);
transform: scale(var(--scale));
}Because --scale is typed as a number, you can use it where a number is required, and the browser can animate it. These are the same values, but they are now data with a known type, not text.
Making a property animatable
The most common reason to register a property is animation. A transition between two values needs an interpolation rule. A registered type supplies it: lengths blend as lengths, colors as colors, angles as angles.
@property --angle {
syntax: '<angle>';
inherits: false;
initial-value: 0deg;
}
.spinner {
transform: rotate(var(--angle));
transition: --angle 0.4s ease;
}
.spinner:hover {
--angle: 180deg;
}You name the custom property in transition, as you would name opacity. When the pointer enters, the browser computes the in-between angles and re-evaluates rotate(var(--angle)) on every frame. Without the @property rule, the spinner would jump straight to 180 degrees.
The next chapter covers animation in more depth, including gradients and @keyframes. For now, remember the rule: a custom property can only animate smoothly if it is registered with a type that can be interpolated.
Registering from JavaScript
You can also register a property in a script with CSS.registerProperty(). It takes an object with the same three descriptors.
if (CSS.registerProperty) {
try {
CSS.registerProperty({
name: '--angle',
syntax: '<angle>',
inherits: false,
initialValue: '0deg',
});
} catch (error) {
console.warn('Could not register --angle:', error.message);
}
}Unlike the at-rule, the method throws an error when the descriptors are invalid. It also throws if the name is already registered. A property can only be registered once for a document. A later @property rule for the same name, or a second call, does not replace the first. Use the script form when the syntax or initial value depends on something known only at run time.
Common errors
- Missing
inherits. The rule is dropped. This is the most frequent mistake, becauseinheritshas no default. - Initial value does not match the syntax. For example,
syntax: '<length>'withinitial-value: 0is invalid. A length needs a unit, even for zero, in this context. Use0px. - Wrong type for the job. Registering
--was<number>and then writingwidth: var(--w)fails, because a width needs a length. Use<length>or multiply withcalc(var(--w) * 1px). - Expecting registration to change old values. Registration applies to the whole document, so a
--gapthat was already in use as a free-form value may now be dropped when it does not match. - Registering twice. The first registration wins. Later ones are ignored in CSS and throw in JavaScript.
- Browser support. Current versions of Chrome, Edge, Safari, and Firefox support
@property. Check your target browsers, and plan a fallback, since an unsupported browser treats the property as untyped.
When to register and when not to
Registration is not free. Each rule adds a small amount of parsing, and it makes the property stricter. Use it when you need one of its three benefits.
| Need | Register? |
|---|---|
| Animate or transition the value | Yes |
| Stop a property from inheriting into nested components | Yes |
| Reject bad values and keep a safe default | Yes |
| Store a token such as a color you only read with `var()` | Usually no |
| Hold a free-form fragment such as `1px solid red` | No, use `'*'` or leave it unregistered |
Check your understanding
Exercise
A learner writes @property --offset { syntax: '<length>'; initial-value: 0px; } and then adds transition: --offset 0.3s to an element. The offset still jumps with no animation. What is wrong, and how do you fix it?
Show answer
The rule has no inherits descriptor, which is required. The browser drops the whole @property rule, so --offset stays an untyped custom property and cannot be interpolated. Add inherits: false (or true) to the rule, so it reads: @property --offset { syntax: '<length>'; inherits: false; initial-value: 0px; }.
Exercise
A registered property has syntax '<length>' and inherits: false, with initial-value: 10px. A parent sets --pad: 30px. What value does a child element see for --pad, and what would it see if the property were not registered?
Show answer
The child sees 10px, the initial value, because inherits: false stops the parent value from flowing down. Without registration, custom properties always inherit, so the child would see 30px.
Exercise
Why does the declaration --gap: banana behave differently for a registered <length> property and an unregistered one?
Show answer
For the registered property, the value does not match the syntax, so the declaration is dropped at parse time and the previous valid value or the initial value stays in effect. For the unregistered property, the text is stored, and the problem appears later when var(--gap) is substituted into a property such as margin-right. That declaration becomes invalid at computed-value time and the property falls back to its own default.