CSS Handbook · 基础与层叠

第一章 / 1.3 长度与颜色单位

第一章 · 1.3 节

长度与颜色单位


几乎每一条 CSS 声明都离不开单位——尺寸用长度单位,颜色用颜色单位。本节梳理最常用的几种,为后面 10.2 calc / min / max / clamp 和响应式布局打好地基。

绝对单位:px

px(像素)是最直观的绝对单位,不受父元素或根元素字号影响。适合边框、阴影偏移等不需要跟随字号缩放的场景。

.card {
  border: 1px solid #ddd;
  box-shadow: 0 2px 6px rgba(0,0,0,.1);
}

相对单位:em 与 rem

em 相对"当前元素的字号"计算,且会随着嵌套层层累积;rem(root em)则始终相对根元素(<html>)的字号,不会因为嵌套而累积,是目前更推荐的排版单位。

em 的累积效应
外层 16px
中层 1.5em = 24px
内层 1.5em = 36px(相对 24px 计算)
html { font-size: 16px; } /* 1rem 的基准 */
.title { font-size: 1.5rem; }  /* 恒等于 24px,不受嵌套影响 */
.title .icon { margin-right: 0.5em; } /* 相对 .icon 自身字号,常用于图标间距 */
经验法则字号本身用 rem,避免嵌套累积导致的意外缩放;而 padding/gap 这类希望"跟随当前元素字号等比缩放"的间距,用 em 反而更符合直觉。

视口单位

单位含义
vw视口宽度的 1%
vh视口高度的 1%
vminvw 与 vh 中较小的一个
vmaxvw 与 vh 中较大的一个
svh / lvh / dvh分别对应移动端浏览器地址栏影响下的最小 / 最大 / 动态视口高度
.hero {
  min-height: 100vh; /* 移动端浏览器地址栏可能导致抖动 */
  min-height: 100dvh; /* 优先用动态视口高度修正 */
}

百分比

百分比总是相对某个"参照物"计算,且不同属性的参照物不同,这也是初学者最容易困惑的地方:

  • width / height 百分比——相对包含块(通常是父元素)的对应尺寸。
  • margin / padding 的百分比(包括上下方向)——统一相对父元素的宽度,这也是"padding-top: 100% 做正方形占位"技巧的原理。
  • font-size 百分比——相对父元素的字号,效果等价于 em。
  • line-height 百分比——相对自身字号。

颜色单位

写法示例说明
十六进制#3f5d54 / #3f5d54cc最常见,末两位可选表示透明度
rgb()rgb(63 93 84 / 80%)现代语法支持空格分隔 + 斜杠透明度
hsl()hsl(160 20% 30%)色相/饱和度/明度,调整明暗更直观
关键字currentColor / transparentcurrentColor 引用元素自身的 color 值,常用于边框、SVG 填充跟随文字色

更多颜色系统、透明度与现代色彩函数(如 oklch())见 7.1 颜色系统与透明度。

如何选择

  • 页面整体字号、排版尺寸——优先 rem,可通过根字号统一缩放全站(如无障碍需求下用户放大字体)。
  • 边框、阴影等不需要跟随字号缩放的细节——用 px。
  • 需要与视口尺寸联动的大尺寸区块(如首屏高度)——用 vh/vw,注意移动端优先 dvh。
  • 组件内部与自身字号联动的间距(图标、徽标)——用 em。

Chapter 1 · Section 1.3

Length and Color Units


Almost every CSS declaration depends on units — lengths for sizing, colors for color. This section reviews the most common ones, laying the groundwork for 10.2 calc / min / max / clamp and responsive layouts.

Absolute Units: px

px (pixels) is the most intuitive absolute unit; it is not affected by the font size of the parent or the root element. It suits borders, shadow offsets, and other details that do not need to scale with the font size.

.card {
  border: 1px solid #ddd;
  box-shadow: 0 2px 6px rgba(0,0,0,.1);
}

Relative Units: em and rem

em is computed against "the font size of the current element" and accumulates as it nests deeper; rem (root em) is always relative to the root element's (<html>) font size and does not accumulate through nesting, which makes it the currently recommended typographic unit.

The compounding effect of em
Outer 16px
Middle 1.5em = 24px
Inner 1.5em = 36px (computed against 24px)
html { font-size: 16px; } /* the baseline for 1rem */
.title { font-size: 1.5rem; }  /* always equals 24px, unaffected by nesting */
.title .icon { margin-right: 0.5em; } /* relative to .icon's own font size, often used for icon spacing */
Rule of thumbUse rem for font sizes to avoid unexpected scaling from nested accumulation; for spacing such as padding/gap that you want to "scale proportionally with the current element's font size", em is actually more intuitive.

Viewport Units

UnitMeaning
vw1% of the viewport width
vh1% of the viewport height
vminThe smaller of vw and vh
vmaxThe larger of vw and vh
svh / lvh / dvhThe small / large / dynamic viewport heights under the influence of the mobile browser's address bar, respectively
.hero {
  min-height: 100vh; /* the mobile address bar may cause jumping */
  min-height: 100dvh; /* prefer the dynamic viewport height to fix it */
}

Percentages

A percentage is always computed against some "reference", and different properties have different references — this is where beginners get most confused:

  • width / height percentages — relative to the corresponding dimension of the containing block (usually the parent element).
  • margin / padding percentages (including the top and bottom) — always relative to the width of the parent; this is the principle behind the "padding-top: 100% to reserve a square" trick.
  • font-size percentages — relative to the parent's font size, equivalent to em.
  • line-height percentages — relative to the element's own font size.

Color Units

SyntaxExampleDescription
Hexadecimal#3f5d54 / #3f5d54ccThe most common; the optional last two digits express alpha
rgb()rgb(63 93 84 / 80%)Modern syntax supports space-separated values plus a slash alpha
hsl()hsl(160 20% 30%)Hue / saturation / lightness; adjusting lightness is more intuitive
KeywordscurrentColor / transparentcurrentColor references the element's own color value, often used so borders and SVG fills follow the text color

For more color systems, alpha, and modern color functions (such as oklch()), see 7.1 Color Systems and Transparency.

How to Choose

  • Overall page font sizes and typographic dimensions — prefer rem; you can scale the whole site through the root font size (e.g., users enlarging fonts for accessibility).
  • Details that should not scale with the font size, such as borders and shadows — use px.
  • Large blocks that need to track the viewport size (such as the first-screen height) — use vh/vw, and prefer dvh on mobile.
  • Spacing inside components that should track the element's own font size (icons, badges) — use em.