CSS Handbook · 文字与排印

第四章 / 4.2 文本属性

第四章 · 4.2 节

文本属性


字体属性决定字符本身的样子,文本属性则决定字符在行与段落中如何排布。行高、对齐、字间距这几个属性组合起来,直接决定了一段文字读起来"舒不舒服"。

line-height 行高

line-height 是排版中最容易被低估的属性——正文行高通常建议在 1.5–1.8 之间,行高过小会让多行文字显得拥挤,过大则打断阅读的连贯性。

p { line-height: 1.6; } /* 无单位数值,相对自身 font-size 计算,推荐写法 */
line-height 对比

line-height: 1.1 —— 多行文字挤在一起,阅读时容易串行,压迫感强。

line-height: 1.7 —— 行间留白充足,多行文字阅读起来更轻松从容。

提示无单位数值(如 1.6)优于带单位的值(如 24px)——前者是比例,子元素继承时会按各自字号重新计算;后者是固定值,子元素字号变化时行高不会跟着调整,容易导致嵌套结构里的行高错乱。

text-align 与 text-indent

属性常用取值说明
text-alignleft / center / right / justifyjustify 两端对齐,中文排版中较少使用,容易产生不均匀的字间距
text-indent如 2em首行缩进,中文排版常用 2em 模拟两个汉字的缩进

letter-spacing 与 word-spacing

.kicker { letter-spacing: 0.12em; text-transform: uppercase; }
letter-spacing 效果
wide letter spacing

字母间距加宽常见于小号大写字母(如标签、导航),能提升可读性;正文中一般不建议调整,浏览器的默认字距经过字体设计者精心调校。

text-decoration

现代写法把下划线拆成三个可独立控制的子属性,可以精细调整下划线的样式与颜色,而不必依赖字体自带的下划线粗细。

a {
  text-decoration-line: underline;
  text-decoration-color: var(--accent2);
  text-decoration-thickness: 2px;
  text-underline-offset: 3px; /* 下划线与文字基线的距离 */
}

text-transform

取值效果
uppercase全部转为大写
lowercase全部转为小写
capitalize每个单词首字母大写
提示text-transform 只影响显示效果,不改变 HTML 中的原始文本内容,复制粘贴时得到的仍是原文,这一点优于在数据层直接转换大小写。

文本溢出处理

单行文本超出容器时截断并显示省略号,是列表、卡片标题的高频需求:

.title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

多行截断需要借助 WebKit 私有的行数裁剪属性(目前主流浏览器均已支持):

.desc {
  display: -webkit-box;
  -webkit-line-clamp: 2; /* 最多显示 2 行,超出部分省略 */
  -webkit-box-orient: vertical;
  overflow: hidden;
}

常见误区

  • 单行省略只写了 text-overflow: ellipsis,却忘记同时设置 overflow: hidden 与 white-space: nowrap——三者缺一不可。
  • 行高用固定像素值,导致组件内字号调整(如响应式缩放)后行高比例失调。
  • 中文正文使用 text-align: justify,可能因为浏览器实现差异产生忽宽忽窄的字间距,中文排版通常保持左对齐即可。

Chapter 4 · Section 4.2

Text Properties


Font properties decide what the characters themselves look like, while text properties decide how characters are laid out within lines and paragraphs. Line-height, alignment, and letter-spacing combine to determine whether a passage of text "feels comfortable" to read.

line-height

line-height is one of the most underestimated properties in typography — body text is usually set between 1.5 and 1.8. Too small a line-height makes multi-line text feel cramped; too large breaks the continuity of reading.

p { line-height: 1.6; } /* unitless number, relative to its own font-size; the recommended way */
line-height comparison

line-height: 1.1 — multi-line text is squeezed together, lines are easy to mix up, and it feels oppressive.

line-height: 1.7 — generous spacing between lines makes multi-line text easier and calmer to read.

TipUnitless values (e.g. 1.6) are better than values with units (e.g. 24px) — the former is a ratio that child elements recalculate against their own font-size when inheriting; the latter is a fixed value, so the line-height does not follow when a child's font-size changes, which easily breaks line-height in nested structures.

text-align and text-indent

PropertyCommon valuesDescription
text-alignleft / center / right / justifyjustify aligns both edges; rarely used in Chinese layout because it can produce uneven letter spacing
text-indente.g. 2emfirst-line indent; Chinese layout commonly uses 2em to mimic a two-character indent

letter-spacing and word-spacing

.kicker { letter-spacing: 0.12em; text-transform: uppercase; }
letter-spacing effect
wide letter spacing

Widened letter spacing is common in small caps (such as labels and navigation) and improves legibility; it is generally not recommended in body text, since the browser's default spacing has been carefully tuned by type designers.

text-decoration

The modern approach splits the underline into three independently controllable sub-properties, letting you fine-tune the underline's style and color without relying on the weight of the font's built-in underline.

a {
  text-decoration-line: underline;
  text-decoration-color: var(--accent2);
  text-decoration-thickness: 2px;
  text-underline-offset: 3px; /* distance between the underline and the text baseline */
}

text-transform

ValueEffect
uppercaseConverts everything to uppercase
lowercaseConverts everything to lowercase
capitalizeCapitalizes the first letter of each word
Tiptext-transform only affects the displayed rendering and does not change the original text in the HTML; copying and pasting still yields the original text, which is an advantage over converting case in the data layer.

Handling Text Overflow

Truncating a single line of text that overflows its container and showing an ellipsis is a common need for lists and card titles:

.title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

Multi-line truncation relies on WebKit's private line-clamping property (now supported by all major browsers):

.desc {
  display: -webkit-box;
  -webkit-line-clamp: 2; /* show at most 2 lines, truncating the rest */
  -webkit-box-orient: vertical;
  overflow: hidden;
}

Common Pitfalls

  • For single-line ellipsis you write only text-overflow: ellipsis but forget to also set overflow: hidden and white-space: nowrap — all three are required.
  • Using a fixed pixel value for line-height, so that when a component's font-size changes (e.g. responsive scaling) the line-height ratio goes out of proportion.
  • Using text-align: justify for Chinese body text: browser implementation differences can produce unevenly spaced characters; Chinese typography usually stays left-aligned.