--- url: "https://web.dev/articles/at-property" titulek: "@property: giving superpowers to CSS variables | Articles | web.dev" h1: "@property: giving superpowers to CSS variables Stay organized with collections Save and categorize content based on your preferences." popis: "Learn how to implement CSS custom properties with semantic typing, a fallback value, and more, directly in your CSS file." jazyk: "en" kanonicka: "https://web.dev/articles/at-property" drobecky: ["Articles", "@property: giving superpowers to CSS variables"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:04:36" znaku: 5619 slov: 832 kodovani: "iso-8859-2" sha256: "c4004179c5859dd96b3720e205c90bd1f0e4ecc63b52f4257a1ad816267a439b" --- @property: giving superpowers to CSS variables Stay organized with collections Save and categorize content based on your preferences. Una Kravets Published: July 21, 2020 [CSS Houdini](https://ishoudinireadyyet.com) is an umbrella term that covers a set of low-level APIs that expose parts of the CSS rendering engine, and give developers access to the CSS Object Model. This is a huge change for the CSS ecosystem, as it enables developers to tell the browser how to read and parse custom CSS without waiting for browser vendors to provide built-in support for these features. So exciting! One of the most exciting additions to CSS within the Houdini umbrella is the [Properties and Values API](https://developer.mozilla.org/docs/Web/API/CSS_Properties_and_Values_API). This API supercharges your CSS custom properties (also commonly referred to as CSS variables) by giving them semantic meaning (defined by a syntax) and even fallback values, enabling CSS testing. Write custom properties Here's an example of setting a custom property (think: CSS variable), but now with a syntax (type), initial value (fallback), and inheritance boolean (does it inherit the value from its parent or not?). The current way to do this is through `CSS.registerProperty()` in JavaScript, but in supporting browsers, you can use `@property`: Separate JavaScript file (Chromium 78) CSS.registerProperty({ name: '--colorPrimary', syntax: '', initialValue: 'magenta', inherits: false }); Included in CSS file (Chromium 85) @property --colorPrimary { syntax: ''; initial-value: magenta; inherits: false; } Now you can access `--colorPrimary` like any other CSS custom property, with `var(--colorPrimary)`. However, the difference here is that `--colorPrimary` isn't just read as a string. It has data! Fallback values As with any other custom property, you can get (using `var`) or set (write/rewrite) values, but with Houdini custom properties, if you set a falsey value when overriding it, the CSS rendering engine sends the initial value (its fallback value) instead of ignoring the line. Consider the example. The `--colorPrimary` variable has an `initial-value` of `magenta`. But the developer has given it the invalid value "23". Without `@property`, the CSS parser would ignore the invalid code. Now, the parser falls back to `magenta`. This allows for true fallbacks and testing within CSS. Neat! `.card { background-color: var(--colorPrimary); /* magenta */ } .highlight-card { --colorPrimary: yellow; background-color: var(--colorPrimary); /* yellow */ } .another-card { --colorPrimary: 23; background-color: var(--colorPrimary); /* magenta */ }` Syntax With the syntax feature, you can now write semantic CSS by specifying a type. The current types that are allowed include: `length` `number` `percentage` `length-percentage` `color` `image` `url` `integer` `angle` `time` `resolution` `transform-list` `transform-function` `custom-ident` (a custom identifier string) Setting a syntax enables the browser to type-check custom properties. This has many benefits. To illustrate this point, I'll show you how to animate a gradient. There's no way to smoothly animate (or interpolate) between gradient values, as each gradient declaration is parsed as a string. Using a custom property with a "number" syntax, the gradient on the left shows a smooth transition between stop values. The gradient on the right uses a default custom property (no syntax defined) and shows an abrupt transition. In this example, the gradient stop percentage is being animated from a starting value of 40% to an ending value of 100% with a hover interaction. You should see a smooth transition of that top gradient color downward. The browser on the left supports the Houdini Properties and Values API, enabling a smooth gradient stop transition. The browser on the right does not. The non-supporting browser is only able to understand this change as a string going from point A to point B. There is no opportunity to interpolate the values, and thus you don't see that smooth transition. However, if you declare syntax type when writing custom properties, and then use those custom properties to enable the animation, you'll see the transition. You can instantiate the custom property `--gradPoint` like so: `@property --gradPoint { syntax: ''; inherits: false; initial-value: 40%; }` And then when it comes time to animate it, you can update the value from the initial `40%` to `100%`: `.post:hover, .post:focus { --gradPoint: 100%; }` This enables that smooth gradient transition. Smoothly transitioning gradient borders. [See Demo on CodePen](https://codepen.io/web-dot-dev/pen/RNPzxPo) Conclusion The `@property` rule makes an exciting technology even more accessible by allowing you to write semantically meaningful CSS within CSS itself. To learn more about CSS Houdini and the Properties and Values API, check out these resources: [Is Houdini Ready Yet?](http://ishoudinireadyyet.com/) [MDN Houdini Reference](https://developer.mozilla.org/docs/Web/Houdini) [Smarter custom properties with Houdini's new API](/articles/css-props-and-vals) [Houdini CSSWG Issue Queue](https://github.com/w3c/css-houdini-drafts/issues) Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2020-07-21 UTC.