$color: #ff0066;
@color: #ff0066;
$color: #ff0066;
@color: #ff0066;
--color, --corner, inline style, fallback
inheritance, cancel inheritance, inherit keyword
|
CSS limitation |
|
CSS bug |
|
Works! |
background: red;
background: var(--accent-color, orange);
var(--color1, var(--color2, var(--color3, red)))
background: var(--nonexistent, none, yellowgreen);
/* Equivalent to: */
background: none, yellowgreen;
--accent-color: 42deg;
--accent-color: var(--accent-color);
background: red;
background: var(--accent-color, orange);
CSS Variables | 49 | 31 | 15 | 9.1 |
---|
CSS Variables | 49 | 31 | 15 | 9.1 |
---|---|---|---|---|
@supports | 28 | 22 | 13 | 9 |
Show division not working
Number → unit: | calc(var(--foo) * 1px) |
Unit → number: |
Try to animate --color | show that var() is ok
[CSS variables] can even be transitioned or animated, but since the UA has no way to interpret their contents, they always use the "flips at 50%" behavior that is used for any other pair of values that can’t be intelligently interpolated.
— CSS Custom Properties for Cascading Variables Module Level 1
CSS.registerProperty() | ⛳️ |
---|
Theming by class, by style, box-shadow transition | default default values
--colorD: var(--color, black);
/* use --colorD instead of --color internally */
No inherit from box-shadow, invalid at computed value time
inline style too
// Get variable from inline style
element.style.getPropertyValue("--foo");
// Get variable from wherever
getComputedStyle(element).getPropertyValue("--foo");
// Set variable on inline style
element.style.setProperty("--foo", 38 + 4);
var root = document.documentElement;
document.addEventListener("mousemove", evt => {
let x = evt.clientX / innerWidth;
let y = evt.clientY / innerHeight;
root.style.setProperty("--mouse-x", x);
root.style.setProperty("--mouse-y", y);
});
<html mv-app
style="--mouse-x: [$mouse.x / innerWidth];
--mouse-y: [$mouse.y / innerHeight];">
for (input of document.querySelectorAll("input")) {
input.style.setProperty("--value", input.value);
}
document.addEventListener("input", evt => {
var input = evt.target;
input.style.setProperty("--value", input.value);
});
Show value indicator
CSS variables are awesome
for (let element of document.querySelectorAll(".typing")) {
let length = element.textContent.length;
element.style.setProperty("--length", length);
}
CSS variables are awesome
for (let el of document.querySelectorAll(".scrolling")) {
el.addEventListener("scroll", evt => {
let maxScroll = el.scrollHeight - el.offsetHeight;
let scroll = el.scrollTop / maxScroll;
el.style.setProperty("--scroll", scroll);
});
}