CSS @property: typed custom properties that can be animated
A normal custom property is a string to the browser. --angle: 135deg is just the text “135deg” until it is substituted somewhere. That is why a transition from one gradient angle to another jumps instead of moving: the browser cannot interpolate text. @property registers a custom property with a type, and from then on the browser knows it is an angle, a color or a percentage and can animate it.
Problem: A block theme has a gradient Cover block whose angle and colors come from custom properties. A transition on hover and a slow @keyframes rotation both do nothing: the gradient switches at the end instead of moving.
Solution: Register the properties with @property and a syntax such as "<angle>" or "<color>", an initial-value and inherits, then animate the property itself in @keyframes or with transition, and keep a static fallback for browsers without support.
Register and animate
@property --ha-angle {
syntax: "<angle>";
inherits: false;
initial-value: 135deg;
}
@property --ha-from {
syntax: "<color>";
inherits: false;
initial-value: #1e40af;
}
.is-style-ha-animated-gradient {
background: linear-gradient(var(--ha-angle), var(--ha-from), #9333ea);
transition: --ha-from .4s ease;
}
.is-style-ha-animated-gradient:hover {
--ha-from: #0f766e;
}
@media (prefers-reduced-motion: no-preference) {
.is-style-ha-animated-gradient {
animation: ha-rotate 12s linear infinite;
}
}
@keyframes ha-rotate {
to { --ha-angle: 495deg; }
}
The browser cannot animate background-image between two gradients, but it can animate the registered --ha-angle from 135 to 495 degrees and recompute the gradient on each frame. The hover changes --ha-from with a transition, which only works because the property is a registered <color>.
The three descriptors are all required: syntax (the type), inherits and initial-value (which may be omitted only for the universal syntax "*"). If one is missing or invalid, the whole rule is ignored and the property behaves like a normal custom property.
A block style for the Cover block
The class is-style-ha-animated-gradient becomes a style variation of the Cover block, selectable in the editor:
add_action( 'init', function (): void {
register_block_style( 'core/cover', array(
'name' => 'ha-animated-gradient',
'label' => __( 'Animated gradient', 'textdomain' ),
) );
} );
How block styles work in general: register_block_style.
A progress ring with a number
@property --ha-progress {
syntax: "<percentage>";
inherits: false;
initial-value: 0%;
}
.ha-ring {
inline-size: 6rem;
aspect-ratio: 1;
border-radius: 50%;
background: conic-gradient(var(--wp--preset--color--primary) var(--ha-progress), #e5e7eb 0);
transition: --ha-progress 1s ease-out;
}
<div class="ha-ring" style="--ha-progress: 72%" role="img" aria-label="72% complete"></div>
When a script or a server-side value changes --ha-progress, the ring fills smoothly. The aria-label is needed because the value is only visual.
inherits: false and performance
inherits: false means a child element does not take the value from its parent but starts with the initial-value. For properties that are only used on one element, that is the right choice: the browser does not have to recompute the whole subtree when the value changes on every animation frame.
Register from JavaScript
if ( window.CSS?.registerProperty ) {
CSS.registerProperty( { name: '--ha-angle', syntax: '<angle>', inherits: false, initialValue: '135deg' } );
}
Registering the same name twice throws an error, so use either the CSS rule or the JavaScript call, not both.
NOTE: @property works in Chrome and Edge since version 85, in Safari since 16.4 (March 2023) and in Firefox since 128 (July 2024). Without support, the properties stay unregistered: the gradient is shown with the values given, it is only not animated. Custom properties in general and the --wp--preset--* variables from theme.json: CSS custom properties in WordPress themes.