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

`white-space-collapse` CSS property

· 52CE 我爱测

The **`white-space-collapse`** CSS property controls how whitespace inside an element is collapsed.

The white-space-collapse CSS property controls how whitespace inside an element is collapsed.

[!NOTE] The white-space-collapse and text-wrap-mode properties can be declared together using the white-space shorthand property.

Syntax

/* Keyword values */
white-space-collapse: collapse;
white-space-collapse: preserve;
white-space-collapse: preserve-breaks;
white-space-collapse: preserve-spaces;
white-space-collapse: break-spaces;

/* Global values */
white-space-collapse: inherit;
white-space-collapse: initial;
white-space-collapse: revert;
white-space-collapse: revert-layer;
white-space-collapse: unset;

The white-space-collapse property is specified as a single keyword chosen from the list of values below.

Values

  • collapse
  • preserve
    • : White space sequences and segment break characters are preserved.
  • preserve-breaks
    • : White space sequences are collapsed, while segment break characters are preserved.
  • preserve-spaces
    • : White space sequences are preserved, while tabs and segment break characters are converted to spaces.
  • break-spaces
    • : The behavior is identical to preserve, except that:
      • Any sequence of preserved white space always takes up space, including at the end of the line.
      • A line-breaking opportunity exists after every preserved white space character, including between white space characters.
      • Preserved spaces take up space and do not hang, thus affecting the box's intrinsic sizes (min-content size and max-content size).

[!NOTE] Segment break characters are characters such as line feeds that cause text to break onto new lines.

[!NOTE] The CSS text module defines a discard value for the white-space-collapse property to discard all white space in the element, however, this is not supported in any browsers.

Formal definition

Formal syntax

Examples

HTML

<h2 class="collapse">Default behavior;
  all   whitespace   is   collapsed
  in    the          heading       .</h2>

<h2 class="preserve">In this case
  all   whitespace   is   preserved
  in    the          heading       .</h2>

<h2 class="preserve-breaks">In this case only
  the   line breaks  are  preserved
  in    the          heading       .</h2>

<h2 class="preserve-spaces">In this case only
  the   spaces       are  preserved
  in    the          heading       .</h2>

CSS

.collapse {
  white-space-collapse: collapse;
}

.preserve {
  white-space-collapse: preserve;
}

.preserve-breaks {
  white-space-collapse: preserve-breaks;
}

.preserve-spaces {
  white-space-collapse: preserve-spaces;
}

h2 {
  font-size: 1.6rem;
  font-family: monospace;
  border-bottom: 1px dotted #cccccc;
}

Result

Examples

Specifications

Browser compatibility

See also

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

资料来源与适用范围

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

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

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