CSS / 参考 / white-space:控制空格与换行
CSS 的 white-space 决定是否保留连续空格和原文中的换行,以及文字能否按可用宽度自动换行。普通段落用 normal,需要保留缩进和换行的日志用 pre-wrap。只需保留换行时,可以用 pre-line。
选择空格与自动换行的处理方式
初始值为 normal,属性会继承。这里的原文换行是 HTML 文本中的换行字符。即使使用 nowrap,显式的 <br> 也会产生新的一行。各值的定义见 CSS white-space 规范。
| 值 | 连续空格和制表符 | 原文换行 | 自动换行 |
|---|---|---|---|
normal |
折叠 | 折叠 | 允许 |
nowrap |
折叠 | 折叠 | 禁用 |
pre |
保留 | 保留 | 禁用 |
pre-wrap |
保留 | 保留 | 允许 |
pre-line |
折叠 | 保留 | 允许 |
break-spaces |
保留,行末也占宽度 | 保留 | 每个保留的空格后也可换行 |
.copy { white-space: normal; }
.log { white-space: pre-wrap; }
.code-line { white-space: pre; overflow-x: auto; }
要让简短的按钮标签保持一行,可以只给这个标签设置 nowrap,并检查窄屏上是否放得下。代码需要保留原来的行长时,可像 .code-line 一样允许代码区横向滚动。
用同一段文字比较六个值
将完整代码以 UTF-8 保存为 white-space-example.html,然后在浏览器中打开。第一组的 A 和 B 之间有四个空格,B 后有换行,C 和 D 之间有制表符。三组示例均无须外部 CSS 或 JavaScript。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>比较 CSS white-space</title>
<style>
body { margin: 1rem; color: #20242b; background: #fff;
font: 1rem/1.55 system-ui, sans-serif; }
main { max-width: 58rem; margin-inline: auto; }
.samples { display: grid; gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 15rem), 1fr)); }
figure { margin: 0; min-width: 0; }
figcaption { margin-bottom: .35rem; overflow-wrap: anywhere; }
.sample { width: 22ch; max-width: 100%; overflow: auto;
font: 1rem/24px ui-monospace, monospace; tab-size: 4;
background: #edf4f8; outline: 1px solid #60727b; }
.sample:focus-visible { outline: 3px solid #005fcc; }
.narrow { width: 4ch; }
.anywhere { overflow-wrap: anywhere; }
.normal { white-space: normal; }
.nowrap { white-space: nowrap; }
.pre { white-space: pre; }
.pre-wrap { white-space: pre-wrap; }
.pre-line { white-space: pre-line; }
.break-spaces { white-space: break-spaces; }
</style>
</head>
<body>
<main>
<h1>比较 CSS white-space</h1>
<h2>同一段原文的六种值</h2>
<div class="samples">
<figure>
<figcaption id="mode-normal-label"><code>normal</code></figcaption>
<div id="mode-normal" class="sample normal">A B
C D E F G H I J K L M N O P Q</div>
</figure>
<figure>
<figcaption id="mode-nowrap-label"><code>nowrap</code></figcaption>
<div id="mode-nowrap" class="sample nowrap" tabindex="0" role="region" aria-labelledby="mode-nowrap-label">A B
C D E F G H I J K L M N O P Q</div>
</figure>
<figure>
<figcaption id="mode-pre-label"><code>pre</code></figcaption>
<div id="mode-pre" class="sample pre" tabindex="0" role="region" aria-labelledby="mode-pre-label">A B
C D E F G H I J K L M N O P Q</div>
</figure>
<figure>
<figcaption id="mode-pre-wrap-label"><code>pre-wrap</code></figcaption>
<div id="mode-pre-wrap" class="sample pre-wrap">A B
C D E F G H I J K L M N O P Q</div>
</figure>
<figure>
<figcaption id="mode-pre-line-label"><code>pre-line</code></figcaption>
<div id="mode-pre-line" class="sample pre-line">A B
C D E F G H I J K L M N O P Q</div>
</figure>
<figure>
<figcaption id="mode-break-spaces-label"><code>break-spaces</code></figcaption>
<div id="mode-break-spaces" class="sample break-spaces">A B
C D E F G H I J K L M N O P Q</div>
</figure>
</div>
<h2>六个行末空格</h2>
<div class="samples">
<figure>
<figcaption id="end-pre-wrap-label"><code>pre-wrap</code></figcaption>
<div id="end-pre-wrap" class="sample narrow pre-wrap">AB      </div>
</figure>
<figure>
<figcaption id="end-break-spaces-label"><code>break-spaces</code></figcaption>
<div id="end-break-spaces" class="sample narrow break-spaces">AB      </div>
</figure>
</div>
<h2>不含空格的长字符串</h2>
<div class="samples">
<figure>
<figcaption id="token-0-label"><code>overflow-wrap: normal</code></figcaption>
<div id="token-0" class="sample normal" tabindex="0" role="region" aria-labelledby="token-0-label">ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</div>
</figure>
<figure>
<figcaption id="token-1-label"><code>overflow-wrap: anywhere</code></figcaption>
<div id="token-1" class="sample normal anywhere">ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</div>
</figure>
<figure>
<figcaption id="token-2-label"><code>nowrap + anywhere</code></figcaption>
<div id="token-2" class="sample nowrap anywhere" tabindex="0" role="region" aria-labelledby="token-2-label">ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</div>
</figure>
</div>
</main>
</body>
</html>
使用 normal 时,间距变小,原文换行不会强制 C 另起一行。nowrap 还会禁用自动换行。pre-line 保留 C 前的换行,但折叠空格和制表符。pre 和 pre-wrap 都保留空格和原文换行,其中 pre-wrap 允许长行折行。
比较 pre-wrap 和 break-spaces 的行末空格
使用 pre-wrap 时,保留的行末空格可以伸出可用宽度。使用 break-spaces 时,这些空格仍占用宽度,而且每个空格后都可换行。细节见 CSS 空白处理规则。
第二组的盒子约四个字符宽,AB 后有六个空格。每个   都是表示一个空格的 HTML 字符引用。pre-wrap 的盒子高一行,break-spaces 的盒子因行末空格变成两行。每次删掉一两个空格,观察行数何时变化,再恢复原样。
| 值 | 行数 | 预览 |
|---|---|---|
pre-wrap |
1 | |
break-spaces |
2 |
处理超出容器的长字符串
即使允许换行,缺少断点的长标识符也可能溢出。在第三组中,overflow-wrap: normal 将长字符串留在一行,anywhere 则可以按需从字符串内部换行。与 nowrap 搭配时,anywhere 不会重新启用自动换行。word-wrap 是 overflow-wrap 的旧名。要改变普通的词内断行规则,请参阅 word-break。
.message {
white-space: pre-wrap;
overflow-wrap: anywhere;
min-width: 0;
}
把上面的 .message 规则应用到含文本的元素。如果 flex 或 grid 项目不能缩小,可以给该项目设置 min-width: 0。容器宽度和滚动见 内容溢出和长文本指南,旧属性名见 word-wrap。
检查原始文本和计算样式
先在开发者工具中确认文本节点仍含有空格和换行。这个属性只影响显示,无法恢复编辑器已删除的字符。然后检查 white-space 和 overflow-wrap 的计算值,看看是否继承了父元素的 nowrap。
在支持双关键字语法的浏览器中,white-space: preserve wrap 与 pre-wrap 等效。preserve 是 white-space-collapse 的值,wrap 是 text-wrap-mode 的值。如果需要支持较旧的浏览器,可以先用单关键字形式。



