
Property in CSS
The @property
rule in CSS is part of the CSS Houdini APIs, and it allows developers to define custom properties (CSS variables) with type, inheritance, and initial value. This brings structure and performance benefits when animating or interacting with custom properties.
? Syntax:
@property --custom-name { syntax: '<data-type>'; inherits: true | false; initial-value: value;}
? Example: Animating a Custom Property
@property --rotate-angle { syntax: '<angle>'; inherits: false; initial-value: 0deg;}.box { --rotate-angle: 0deg; transform: rotate(var(--rotate-angle)); transition: --rotate-angle 1s;}.box:hover { --rotate-angle: 360deg;}
This allows smooth animation of a custom angle variable, which wasn't possible before @property
.
? Why Use @property
?
Enables transitions/animations on CSS custom properties.
Adds type checking and default values.
Improves performance by allowing the browser to optimize changes.
? Supported Syntax Types:
<length>
<color>
<number>
<percentage>
<angle>
<image>
<transform-function>
and more…
You can also use the generic <custom-ident>
if none of the above applies.
?? Browser Support:
Supported in Chrome, Edge, Opera.
Not supported in Firefox or Safari (as of 2024).
Use with caution or feature-detection for cross-browser compatibility.
? Good Use Cases:
Theme variables (e.g., colors, spacing)
Animated effects (e.g., rotation, blur, opacity)
Dynamic styling in web components or JavaScript-heavy apps
Would you like a working example with animation or a fallback for unsupported browsers?