惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
MyScale Blog
MyScale Blog
雷峰网
雷峰网
量子位
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
T
Tailwind CSS Blog
月光博客
月光博客
博客园 - 【当耐特】
博客园_首页
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
IT之家
IT之家
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
WordPress大学
WordPress大学
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
人人都是产品经理
人人都是产品经理
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell

Matthias Ott

Hello Again, World This, Still Not for Everyone The Shape of Friction WeissKlang L1 – Punching Above Its Weight Continvoucly Morged Value Webspace Invaders To Affinity and Beyond The Mystery of Storytelling Amateurs! Echoes of Connection Linear() Is Not (That) Linear View Transitions: The Smooth Parts Adding AVIF and WebP Support to My Craft CMS Site Challenge Acoustic Room Treatment and Building Sound Panels, Part 1: Planning Play On Overshoot The HTML Output Element Listening Closely Compressed Fluid Typography The Lifeblood of the Web What Could Go Wrong? That’s My Rank Making Space CSS :is() :where() the Magic Happens Visual Regression Testing for External URLs With Playwright Jane Goodall’s Famous Last Words European Tech Alternatives 🇪🇺 Independent Type Foundry Advent Calendar – Day 24: NaN Independent Type Foundry Advent Calendar – Day 23: Typotheque
CSS Custom Properties With @property
Matthias Ott · 2020-09-22 · via Matthias Ott

Una Kravets has written an excellent article about a feature that has been released with Chrome 85: The @property syntax of the Properties and Values API. The Properties and Values API is part of CSS Houdini, the next generation of additions to CSS allowing developers to extend the language directly at the engine level. With the @property syntax, you can declare a variable including a type, an initial value, and even control inheritance. This is how it looks like:

@property --colorPrimary {
  syntax: "<color>";
  initial-value: magenta;
  inherits: false;
}

The initial value ensures that if someone tries to give the --colorPrimary an invalid value that doesn’t match the type specified by syntax, like “20px” or “border-box”, there is still a fallback.

But there is more. The new @property syntax fixes a fundamental problem of custom properties: You could not animate them. While it was possible to animate a property that used a custom property, it wasn’t possible to animate the custom property itself because the custom property was read as a string.

Animating a background property, for example, would work:

button {
  --color: green;
  background: var(--color);
  transition: background .4s ease;
}

button:hover {
	--color: blue;
}

But not if you wanted to apply the transition to the custom property, --color:

button {
  --color: green;
  background: var(--color);
  transition: --color .4s ease;
}

button:hover {
	--color: blue;
}

With the new @property syntax, this now changes, because if you define a type (like length, number, percentage, color, and many more) for the custom property, the browser now knows how to transition it. The prime example that Una shows is that it is now possible to animate something that couldn’t be animated before: Gradients.

See the Pen Gradient Transitions with @property by Matthias Ott (@matthiasott) on CodePen.

When I played around with the feature myself, however, I noticed that using relative units like rems with a type of length did not work properly when I tried to calculate a font-size in Chrome 85. It worked well when I used an absolute length in px, though. Maybe I’m also overlooking something here – and if I am, please tell me – but it seems as if the new feature still needs a few more iterations until it fully works as expected.

See the Pen @property Custom Property Test: rem font-size by Matthias Ott (@matthiasott) on CodePen.

Once browser support has gotten better, the @property syntax might make many things around custom properties much more convenient. In particular when creating animations, the possibility to transition the computed value of the custom property will come in handy.

-

This is the 59th post of my 100 days of writing series. You can find a list of all posts here.

~

5 Webmentions

1 Repost

4 Likes

ⓘ Webmentions are a way to notify other websites when you link to them, and to receive notifications when others link to you. Learn more about Webmentions.