← Back to HTML & CSS, page 5

html-css-0098 · Hard senior

What does @property add to CSS custom properties, and how can it be used to animate a gradient?

@property registers a custom property with a syntax type, an inherits flag and an initial value. The browser then validates values and can interpolate them, so a registered angle can animate a gradient. It is newer, so check support and keep a static fallback.

Full answer

A normal custom property, such as --angle, is just text to the browser. It does not know that 45deg is an angle. This has two results. The value is not checked, and it cannot be animated smoothly: a transition from 0deg to 90deg on a plain custom property jumps at the halfway point, because there is nothing to calculate between two strings.

@property registers a custom property with a type. You give it three descriptors:

  • syntax: the type, for example "<angle>", "<color>", "<length>" or "<number>".
  • inherits: true or false. Setting false can help performance when the value does not need to pass down.
  • initial-value: the default, and it is required for most types.

After that, the browser parses the value, rejects invalid ones (falls back to the initial value), and can interpolate between two values. The classic use is animating a gradient angle or a colour stop. A gradient itself cannot be animated, but a registered angle that the gradient reads can.

The same registration is available in JavaScript with CSS.registerProperty().

It is a newer feature, so check the support table. Without support, the page should still look fine. With the example below, the gradient is simply static if the property is not registered.

When the pointer enters the box, the gradient turns smoothly from 0 to 90 degrees over half a second. In the second box, --bad: red is not a length, so it is thrown away and the padding uses the initial value of 4px.

Without the registration, the hover would flip the gradient angle at once and not animate.

html
<style>
  @property --angle {
    syntax: "<angle>";
    inherits: false;
    initial-value: 0deg;
  }
  .box {
    width: 160px; height: 80px;
    background: linear-gradient(var(--angle), #0a6cff, #ff6a00);
    transition: --angle 0.5s;
  }
  .box:hover { --angle: 90deg; }
  @property --bad { syntax: "<length>"; inherits: false; initial-value: 4px; }
  .check { --bad: red; padding: var(--bad); }
</style>
<div class="box" id="box"></div>
<div class="check" id="check">Invalid value falls back</div>

Checked in Chrome 156 only. The check read the value part way through the transition to confirm that it is interpolated. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Plain custom properties are untyped text.
  • @property sets syntax, inherits and initial-value.
  • Registered properties are validated and interpolated.
  • Animating a registered angle animates a gradient.
  • Check the support table and keep a fallback.

Common mistakes

  • Expecting plain custom properties to transition smoothly.
  • Registering a property with no initial-value for a type that requires one, which makes the rule invalid.
  • Relying on @property for essential behaviour without a fallback.

Follow-ups

  • Why can a plain custom property not be animated smoothly?
  • What does inherits: false do?

Tips

  • Say what registration adds: type checking, a default and interpolation.
  • Give the gradient angle example, because it is the most common real use.