CSS Studio detects the CSS variables available on an element.
令人惊讶的是:这个工具能够智能识别元素上可用的CSS变量,并允许设计师编辑这些变量,同时看到变化在整个网站上的传播效果。这种变量管理功能对于维护大型设计系统的一致性非常有价值,但很多开发者可能不知道有这样的工具存在。
CSS Studio detects the CSS variables available on an element.
令人惊讶的是:这个工具能够智能识别元素上可用的CSS变量,并允许设计师编辑这些变量,同时看到变化在整个网站上的传播效果。这种变量管理功能对于维护大型设计系统的一致性非常有价值,但很多开发者可能不知道有这样的工具存在。
We also need at least something in our CSS that can be set from outside. CSS custom properties are a great fit for this!
With JavaScript, you can actually calculate the width of the scrollbar and whether it’s visible by comparing two properties—window.innerWidth and document.body.clientWidth. If these are equal, the scrollbar isn’t visible. If these are different, we can subtract the body width from the window width to get the width of the scrollbar:const scrollbarWidth = window.innerWidth - document.body.clientWidthWe’ll want to perform this both on page load and on resize, in case someone resizes the window vertically and changes the overflow. Then, once we have the scrollbar width, we can assign it as a CSS variable:document.body.setProperty("--scrollbarWidth", `${scrollbarWidth}px`)
missing feature: vw/vh can't be used "directly" because doesn't account for scrollbars
let root = document.documentElement; root.addEventListener("mousemove", e => { root.style.setProperty('--mouse-x', e.clientX + "px"); root.style.setProperty('--mouse-y', e.clientY + "px"); });
Here’s a CSS variable (formally called a “CSS custom property“)
A great thing about CSS variables is their reactive nature. As soon as we update them, whatever property has the value of the CSS variable gets updated as well.
The standard cascade rules also apply to the CSS Variables.So, if a custom property is declared multiple times, the lowermost definition in the css file overwrites the ones above it.
You can style a component by overriding the available CSS variables.
style.css now uses native CSS variables