大网数据广告活动
Web 标准与前端开发

white-space

· 52CE 我爱测

**`white-space`** CSS 属性用于设置如何处理元素内的whitespace。

white-space CSS 属性用于设置如何处理元素内的whitespace。

CSS Demo: white-space

white-space: normal;
white-space: pre;
white-space: pre-wrap;
white-space: pre-line;
white-space: wrap;
white-space: collapse;
white-space: preserve nowrap;
<section class="default-example" id="default-example">
  <div id="example-element">
    <p>
      But ere she from the church-door stepped She smiled and told us why: 'It
      was a wicked woman's curse,' Quoth she, 'and what care I?' She smiled, and
      smiled, and passed it off Ere from the door she stept—
    </p>
  </div>
</section>
#example-element {
  width: 16rem;
}

#example-element p {
  border: 1px solid #c5c5c5;
  padding: 0.75rem;
  text-align: left;
}

这个属性指定了两件事:

  • 空白字符是否合并,以及如何合并。
  • 是否换行,以及如何换行。

[!NOTE] 要使单词可以在其内部被截断,请使用 overflow-wrap、word-break 或 hyphens 代替。

语法

/* 单个关键字值 */
white-space: normal;
white-space: nowrap;
white-space: pre;
white-space: pre-wrap;
white-space: pre-line;
white-space: break-spaces;

/* white-space-collapse 和 text-wrap 简写值 */
white-space: collapse balance;
white-space: preserve nowrap;

/* 全局值 */
white-space: inherit;
white-space: initial;
white-space: revert;
white-space: revert-layer;
white-space: unset;

值

white-space 属性可以被指定为从下面的值列表中选择的单个关键字,或者是表示 white-space-collapse 和 text-wrap 属性的简写的两个值。

  • normal
    • : 连续的空白符会被合并。源码中的换行符会被当作空白符来处理。并根据填充行框盒子的需要来换行。
  • nowrap
    • : 和 normal 一样合并空白符,但阻止源码中的文本换行。
  • pre
    • : 连续的空白符会被保留。仅在遇到换行符或 br 元素时才会换行。
  • pre-wrap
    • : 连续的空白符会被保留。在遇到换行符或 br 元素时,或者根据填充行框盒子的需要换行。
  • pre-line
    • : 连续的空白符会被合并。在遇到换行符或 br 元素时,或者根据填充行框盒子的需要换行。
  • break-spaces
    • : 与 pre-wrap 的行为相同,除了:
      • 任何保留的空白序列总是占用空间,包括行末的。
      • 每个保留的空白字符后(包括空白字符之间)都可以被截断。
      • 这样保留的空间占用空间而不会挂起,从而影响盒子的固有尺寸(min-content 尺寸和 max-content 尺寸)。

下面的表格总结了各种 white-space 关键字值的行为:

<table class="standard-table"> <thead> <tr> <th></th> <th>换行符</th> <th>空格和制表符</th> <th>文本换行</th> <th>行末空格</th> <th>行末的其他空白分隔符</th> </tr> </thead> <tbody> <tr> <th><code>normal</code></th> <td>合并</td> <td>合并</td> <td>换行</td> <td>移除</td> <td>挂起</td> </tr> <tr> <th><code>nowrap</code></th> <td>合并</td> <td>合并</td> <td>不换行</td> <td>移除</td> <td>挂起</td> </tr> <tr> <th><code>pre</code></th> <td>保留</td> <td>保留</td> <td>不换行</td> <td>保留</td> <td>不换行</td> </tr> <tr> <th><code>pre-wrap</code></th> <td>保留</td> <td>保留</td> <td>换行</td> <td>挂起</td> <td>挂起</td> </tr> <tr> <th><code>pre-line</code></th> <td>保留</td> <td>合并</td> <td>换行</td> <td>移除</td> <td>挂起</td> </tr> <tr> <th><code>break-spaces</code></th> <td>保留</td> <td>保留</td> <td>换行</td> <td>换行</td> <td>换行</td> </tr> </tbody> </table>

默认情况下,一个制表符等于 8 个空格,且可以使用 tab-size 属性。对于 normal、nowrap 和 pre-line 值,每个制表符都会被转化为一个空格(U+0020)字符。

[!NOTE] 空格和其他空白分隔符之间存在区别。定义如下:

  • 空格
    • : 空格(U+0020)、制表符(U+0009)和分段符(例如换行)
  • 其他空白分隔符
    • : Unicode 中定义的所有其他空格分隔符(已定义为空格的分隔符除外)。

如果空白字符被挂起,那么它可能会影响框的固有尺寸的测量结果。

合并空白字符

white-space-collapse 属性的页面解释了浏览器合并空白字符的算法。

形式定义

形式语法

示例

基础示例

code {
  white-space: pre;
}

<pre> 元素内的换行

pre {
  white-space: pre-wrap;
}

试试看

<div id="css-code" class="box">
  p { white-space:
  <select>
    <option>normal</option>
    <option>nowrap</option>
    <option>pre</option>
    <option>pre-wrap</option>
    <option>pre-line</option>
    <option>break-spaces</option>
  </select>
  }
</div>
<div id="results" class="box">
  <p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
    non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </p>
</div>
.box {
  width: 350px;
  padding: 16px;
}

#css-code {
  background-color: rgb(220, 220, 220);
  font-size: 16px;
  font-family: monospace;
}

#css-code select {
  font-family: inherit;
  width: 100px;
}

#results {
  background-color: rgb(230, 230, 230);
  overflow-x: scroll;
  white-space: normal;
  font-size: 14px;
}
const select = document.querySelector("#css-code select");
const results = document.querySelector("#results p");
select.addEventListener("change", (e) => {
  results.style.setProperty("white-space", e.target.value);
});

试试看

控制表格中的换行

HTML

<table>
  <tr>
    <td></td>
    <td>拆分后非常长的内容</td>
    <td class="nw">未拆分非常长的内容</td>
  </tr>
  <tr>
    <td class="nw">white-space:</td>
    <td>normal</td>
    <td>nowrap</td>
  </tr>
</table>

CSS

table {
  border-collapse: collapse;
  border: solid black 1px;
  width: 250px;
  height: 150px;
}
td {
  border: solid 1px black;
  text-align: center;
}
.nw {
  white-space: nowrap;
}

结果

控制表格中的换行

SVG 文本元素的多行文本

white-space CSS 属性可用于在 text 元素中创建多行文本,该元素默认情况下不会换行。

HTML

<text> 元素内部的文本需要拆分成多行以便检测新行。从第二行开始,其余行的空白需要移除。

<svg viewBox="0 0 320 150">
  <text y="20" x="10">Here is an English paragraph
that is broken into multiple lines
in the source code so that it can
be more easily read and edited
in a text editor.
  </text>
</svg>

CSS

text {
  white-space: break-spaces;
}

结果

SVG 文本元素的多行文本

规范

浏览器兼容性

参见

  • 定义单词如何在其内部被截断的属性:overflow-wrap、word-break、hyphens
  • tab-size

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。