liveBook · Manning

@property

As explained in CSS in Depth, Second Edition

The CSS @property at-rule registers a custom property by defining its syntax, inheritance behavior, and initial value.

The CSS @property at-rule registers a custom property so the browser understands its data type, inheritance behavior, and initial value. That information enables type checking and smooth transitions of the custom property itself. section 15.5 section 15.6

A definition must specify three characteristics:

  • syntax: the permitted data type, such as <angle>, <length>, <number>, <percentage>, or <length-percentage>; section 15.5
  • inherits: whether the custom property inherits from parent elements by default; section 15.5
  • initial-value: the value used when no value is declared or when a declaration has an invalid type. section 15.5

Transition a custom hue

Define --hue as an angle, prevent inheritance, and give it a 0deg initial value. A button can then transition --hue from 314deg to 250deg on hover while using it in an OKLCH background color. Without @property, the custom-property transition does not work. section 15.5

For example, --hue: 15% is invalid when syntax is "<angle>"; the browser ignores that declaration and uses the initial value of 0deg. Firefox did not support @property as of mid-2024, so check current support before making it essential. section 15.5

How CSS in Depth, Second Edition covers it

How deep it goes
Explains required descriptors, invalid-value fallback, and a worked hover transition, but does not detail broader browser support beyond Firefox.
Examples
Uses an angle-typed --hue property to animate a button’s OKLCH background from purple toward blue on hover.
What it assumes
Assumes familiarity with CSS custom properties and transitions while defining the registration details directly.
What you can do after
Build it from scratch by registering a custom property and animating its validated value with CSS transitions.
How it connects
Connects custom properties with inheritance, transitions, interpolation, data types, and OKLCH colors.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.