Skip to content

自定义属性与变量

css
:root {
  --global--color-primary: #28303d;
}
a {
  color: var(--global--color-primary);
}
.social-icons a {
  background: var(--global--color-primary);
}

Javascript 设置与访问 CSS 变量

javascript
const element = document.getElementById("my-element");
element.style.setProperty("--variable-name", "a-value");

const styles = getComputedStyle(document.documentElement);
const value = String(styles.getPropertyValue("--variable-name")).trim();