排版最容易暴露问题的地方,往往不是字体选得够不够特别,而是一段文字落进布局之后,断行是否自然。

标题在最后一个词前突然折行,段落末尾孤零零剩下一个词,数字从 9 跳到 10 时整列内容跟着抖,按钮被挤压成两行,长文本直接撑破容器;中文里还会遇到词被拆开、行首出现半角空隙、中英文之间没有呼吸感。

这些都不需要换字体,也不需要重写文案。给文本换行、间距、对齐和截断建立一组明确的 CSS 开关,就能解决大部分问题。

先处理换行,不手动插换行符

标题和正文的目标不同:标题要让几行长度接近,正文则要尽量避免最后一行只剩一个词。

对 h1 到 h3、卡片标题和首屏主标题使用:

1
text-wrap: balance;

浏览器会重新计算断行位置,让每一行的长度更平均。它适合短文本;Chrome 目前最多平衡 6 行,所以不要把这套规则无差别套到长正文上。

对段落、描述和列表项使用:

1
text-wrap: pretty;

它会在合适的断点提前换行,减少段落末尾的孤立单词。对于破折号和后面的名字、数字和单位这类不能拆开的组合,再用不换行空格 U+00A0 连起来。

这两种方法都比在文案里手写 <br> 稳定。固定换行符只对某一套字号和容器宽度负责,布局一响应,原本漂亮的断行就会立刻失效。

数字、按钮和截断各自需要边界

会原地变化的计数器、价格、计时器和表格数字,如果使用比例数字,宽度会随着字形的变化而跳动:

1
font-variant-numeric: tabular-nums;

等宽数字能让整列保持稳定,但运行中的正文仍应使用比例数字,否则阅读节奏会变得机械。还要确认字体真的包含 tnum 特性,不能只看 CSS 是否生效。

按钮和 Flex 行里的标签不应该被挤成两行:

1
white-space: nowrap;

不过,不换行并不等于可以无限变长。空间不足时,需要调整布局、缩短文案或允许截断,而不是让按钮溢出屏幕。

长文本截断经常失效,原因不在 text-overflow,而在 Flex 项默认不会缩小到内容宽度以下。父级 Flex 项必须先允许收缩:

1
2
3
4
5
6
7
8
9
.truncate-item {
min-width: 0;
}

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

只有实际发生截断时,才在悬停或键盘聚焦时显示完整文本。判断方式是 scrollWidth > clientWidth,不是为每一段文本都挂上 tooltip。触摸设备不能只依赖悬停,还需要可聚焦的替代方式。

中文排版先尊重语言规则

中文规则应该通过 :lang(zh) 限定,并确保根元素或文本元素上真的设置了 lang。否则英文文本会被中文规则误伤。

长段中文可以使用两端对齐:

1
2
3
:lang(zh) p {
text-align: justify;
}

但只适合足够宽的长正文。行宽最好落在整数个汉字上,例如段落字号下使用 max-width: 26em,这样两端对齐只需要吸收英文单词和标点造成的微小空隙。窄栏、短文案、标题、按钮和以英文为主的内容不要使用,否则字间空隙会忽大忽小。

中英文混排时,字与字母、数字之间的细微间隔应该交给浏览器:

1
2
3
:lang(zh) {
text-autospace: normal;
}

不要在文案或翻译文件里手动输入空格。代码、输入框和等宽文本应跳过这条规则,避免破坏本就需要精确对齐的内容。

行首开括号也有类似的细节。没有首行缩进的中文界面文本可以使用:

1
text-spacing-trim: trim-start;

行首的 「、“、( 会丢掉多余的半角空白,与上方文字对齐。带两字符首行缩进的长文则改用 space-first。这条属性目前主要作为 Chrome 的渐进增强,不支持它的浏览器直接忽略即可,不需要补丁库。

中文标题只在词与词之间换行

中文没有空格。标题如果只是随便允许断行,可能把「排版」拆成「排」和「版」,语义会被切断;如果直接使用 word-break: keep-all,没有标记断点的标题又会变成一整个不可换行的词。

正确做法是先用 BudouX 的中文模型找出词组边界,在边界插入 <wbr>:

1
<h2>让文字<wbr>排版<wbr>更顺眼</h2>

再让浏览器在这些候选断点之间平衡:

1
2
3
4
5
.zh-heading {
word-break: keep-all;
overflow-wrap: anywhere;
text-wrap: balance;
}

静态文案可以在构建时插入,动态文案可以在渲染时处理。不要把这套规则应用到正文,也不要用 Intl.Segmenter 代替中文分词模型——它没有中文词典,常见的双字词很容易被切错。

overflow-wrap: anywhere 只是极端情况下的兜底;真正决定正常换行位置的,是 <wbr>。

避头尾交给浏览器,不要重新造轮子

浏览器已经会避免句号、逗号等标点出现在行首,并压缩行尾标点占用的空间。用 JavaScript 逐字测量、手动插入换行,通常会比原生排版更慢,也更不稳定。

真正需要审查的,是那些为了阻止溢出而写下的:

1
2
line-break: anywhere;
word-break: break-all;

line-break: anywhere 会放开本应受避头尾限制的断点。想要让超长 URL 或连续字符串在必要时断开,应改用 overflow-wrap: anywhere,它只在没有其他合理断点时兜底。

如果文本画在 Canvas、SVG 上,或者由 JavaScript 手动测量换行,浏览器提供的这些规则都不会自动生效。这类内容需要单独处理,不能只依赖页面上的全局 CSS。

国际化的文案长度也是布局的一部分

同一个界面翻译成不同语言后,词的数量、词的边界和可读行宽都会改变。

给 Agent 做国际化时,不能只要求“逐字翻译”,还要让它结合当前布局选择长度合适的表达:这里需要短按钮,还是完整句子;一个词能否留在当前行;换成另一种语言后,是否会制造孤字或过大的两端对齐空隙。

断行规则负责在空间里排得自然,文案长度负责让空间本身合理。两者一起决定最终排版是否需要退一步调整。

检查清单

  • 标题是否使用 text-wrap: balance,长正文是否没有被误用;
  • 段落末尾是否不再孤立一个词;
  • 会变化的数字是否使用 tabular-nums;
  • 按钮标签是否保持单行,同时没有溢出;
  • Flex 项是否设置了 min-width: 0,截断是否只在需要时出现;
  • 中文长正文是否按整数汉字宽度排版,窄栏是否避免了强制两端对齐;
  • 中英文与数字之间是否由 text-autospace 处理,而不是手动输入空格;
  • 中文标题是否只在词边界换行;
  • 是否移除了会关闭避头尾的 line-break: anywhere 和 word-break: break-all;
  • 是否在中英文、数字变化、截断和行首标点的真实页面上检查过前后差异。

文字排版示例 Prompt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Prompt:

Polish the typography in my product with a set of CSS switches. Change only how text wraps, spaces, aligns and truncates. No copy, font, colour or layout changes beyond what each rule says.

0. Start from what exists
- Put the rules in the global text styles (base CSS, typography tokens, Tailwind config or shared text components) so every page inherits them. Add per-component rules only where a component overrides the base.
- Every rule below is progressive enhancement: browsers that don't know a property ignore it. Add no polyfills.
- Scope the Chinese rules with :lang(zh), and make sure the lang attribute is set on the html element or on the element itself, so English text keeps its own rules.

1. Headings break into even lines
- On h1 to h3, card titles and hero lines: text-wrap: balance;
- Lines come out about the same length, so a heading never ends on one lonely word. Short text only: Chrome balances up to 6 lines.

2. No lone word at the end of a paragraph
- On paragraphs, descriptions and list items: text-wrap: pretty;
- Also join pairs that must not split with a non-breaking space (U+00A0): an em dash and the name after it, a number and its unit.

3. Numbers that change keep their width
- On anything that updates in place or sits in a column (counters, prices, timers, stats, table cells): font-variant-numeric: tabular-nums;
- The font must have tabular figures (the tnum feature); check that the width actually stops changing. Leave running text proportional.

4. Button labels stay on one line
- On buttons and other labels in flex rows: white-space: nowrap; (flex-shrink: 0 works too)
- A crowded row must not squeeze a label onto two lines.

5. Long text truncates instead of spilling out
- A flex item won't shrink below its content, so text inside it can't truncate. On the flex item: min-width: 0;
- On the text itself: overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
- When the text really is cut (scrollWidth > clientWidth), show the full text in a tooltip on hover.

6. Chinese body text: justified, on a whole number of characters
- On Chinese paragraphs only: text-align: justify;
- Set the measure to a whole number of characters (for example max-width: 26em at the paragraph's font size), so a justified line only stretches to absorb Latin words and squeezed punctuation.
- Not for narrow columns (under about 15 characters), short text, captions, buttons or mostly-English text: the gaps get uneven.

7. Space between Chinese and Latin text or numbers
- On Chinese text: text-autospace: normal;
- The browser adds a thin gap between Han characters and Latin letters or digits. Don't also hand-type spaces in copy or translation files. Skip code, inputs and monospaced text.

8. No half-width gap before an opening bracket at a line start
- On Chinese UI text without a first-line indent: text-spacing-trim: trim-start;
- An opening 「 “ ( at the start of a line drops its blank half and lines up with the text above. Long-form text with a two-character indent should use space-first instead. Chrome only for now.

9. Chinese headings break between words
- Chinese has no spaces, so a heading can split a word across lines. Mark the break points between words with BudouX (its Simplified or Traditional Chinese model), inserting a wbr element at each one, at build time for static copy or at render time for dynamic copy. Then: word-break: keep-all; overflow-wrap: anywhere; text-wrap: balance;
- Never use keep-all on Chinese without break points: the heading becomes one unbreakable word. Don't use Intl.Segmenter for this (it splits common words wrongly), and don't apply it to body text.

10. Line-start and line-end rules (避头尾)
- Browsers already keep punctuation off the start of a line and squeeze line-end punctuation. Don't rebuild this.
- Find and remove any line-break: anywhere used to stop overflow: it turns these rules off. Use overflow-wrap: anywhere instead.
- Text drawn on canvas or SVG, or wrapped by hand in JS, gets none of this: apply the rules there yourself.

11. When done
- List every file and selector you changed.
- List every line-break: anywhere and word-break: break-all you removed or kept, and why.
- Show before and after on my own pages: a wrapped heading, a paragraph ending, a changing number, a truncated label and a mixed Chinese-English paragraph.

质感来自可解释的细节

排版不是把每个字都塞进容器,而是让每一行都知道自己为什么在这里结束。

标题在平衡的位置换行,正文不让最后一个词掉队,数字保持稳定宽度,中文只在词组之间断开,标点遵守避头尾。这些规则单独看都很小,合在一起,文字才会从“能读”变成“愿意继续读”。

站内搜索

没有找到内容!