Skip to content

CSS 变量

Use CSS 自定义属性 (变量) for 主题, reuse, and 动态样式.

你将学到

  • 什么 CSS 自定义属性是 and 如何 declare 它们
  • Using 变量 for 颜色, spacing, and 设计令牌
  • 如何 cascade and 继承 apply 到自定义属性
  • 更改变量使用 JavaScript for 动态主题

概念讲解

CSS Variables

CSS Custom Properties (commonly called CSS variables) let you store values in a named reference and reuse them throughout your stylesheet. Declare one with a double dash, reference it with var():

:root {
  --primary: #2563eb;
  --spacing: 16px;
}
.button {
  background: var(--primary);
  padding: var(--spacing);
}

Change the variable in one place, and every reference updates. This makes themes, spacing systems, and consistent design far easier to maintain.

Why Not Preprocessor Variables?

Sass and Less have variables too, but CSS variables have a superpower: they are live in the browser. They follow the cascade and can be overridden at any element, not just at compile time. You can change them with media queries (responsive themes), with JavaScript (dark mode toggle), and they inherit through the DOM tree. Preprocessor variables are static — once compiled, they are gone. CSS variables are dynamic.

Design Tokens

Variables are the foundation of a design token system. Define your brand colors, spacing scale, font sizes, border radii, and shadows as variables on :根, then use only those variables in your components. This ensures consistency (every button uses the same primary color) and makes rebranding a one-line change. A well-structured token system is the difference between a maintainable and an unmaintainable stylesheet.

Theming with Variables

Dark mode becomes trivial with variables. Define light values on :根 and dark values under a [数据-主题="dark"] selector:

:root { --bg: #ffffff; --text: #1f2937; }
[data-theme="dark"] { --bg: #1f2937; --text: #ffffff; }
body { background: var(--bg); color: var(--text); }

Toggle the 数据-主题 attribute with JavaScript, and the entire page re-themes instantly — no reload, no duplication.

Fallback Values

var() accepts a fallback: var(--主要, #2563eb). If the variable is not defined, the fallback is used. This is useful for optional customization and for safety when a variable might not be set in a particular context.

示例代码

              /* Design tokens defined on :root */
:root {
  --color-primary: #2563eb;
  --color-bg: #ffffff;
  --color-text: #1f2937;
  --radius: 8px;
  --space: 16px;
}

/* Dark theme overrides */
[data-theme="dark"] {
  --color-bg: #0f172a;
  --color-text: #e2e8f0;
}

/* Components use only variables, never raw values */
.card {
  background: var(--color-bg);
  color: var(--color-text);
  border-radius: var(--radius);
  padding: var(--space);
  border: 1px solid var(--color-primary);
}

/* Responsive spacing with a media query override */
@media (min-width: 768px) {
  :root { --space: 24px; }
}
            

设计令牌是 定义一次 on :根. dark 主题 overrides 仅变量那个更改. 组件引用变量 exclusively, 所以更改令牌更新每个组件. media 查询 increases spacing on larger screens 通过 overriding one 变量.

动手试一试

常见错误

错误写法

定义变量 on 特定元素相反的 :根, limiting 它们的作用域.

正确做法

定义全局设计令牌 on :根所以它们是 available everywhere. 定义组件-特定变量 on 组件它自己. Use :根 for shared 值 and 作用域 for 局部 overrides.

错误写法

Using var() 不使用 fallback for 关键值, risking unstyled 内容.

正确做法

Provide fallback in var(--name, fallback) for 重要属性. 如果变量失败到 加载 (e.g., stylesheet 做不 加载), fallback ensures 元素是 still 样式, 不 broken.

相关速查表

练习 CSS 变量

2 个练习

练习 CSS 变量

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误