【发布时间】:2018-11-07 03:51:32
【问题描述】:
我对 CSS 变量有一个非常简单的问题。我想交换两个 CSS 变量,基本上是 ES6 中 [a, b] = [b, a] 的 CSS 等价物。这是一个简单的例子:
<p>White background</p>
<button>Black background</button>
<div>
<p>Black background</p>
<button>White background</button>
</div>
:root {
--primary-color: #fff;
--secondary-color: #000;
}
body {
background-color: var(--primary-color);
}
button {
background-color: var(--secondary-color);
}
div {
/* i'd like to do the following: */
--primary-color: var(--secondary-color);
--secondary-color: var(--primary-color);
/* so here, `--primary-color` would be `--secondary-color` from `:root`
* and any children have these colors swapped as well
*/
background-color: var(--primary-color);
}
但是,这会失败,因为 CSS var()s 是实时绑定。我在这里错过了什么吗?或者这是规范当前的工作方式?
【问题讨论】:
标签: css swap css-variables