Paragraphs & wrapping

white-space

white-space decides how the spaces and line breaks written in the HTML are handled, and whether lines wrap.

Syntax

normal | pre | pre-wrap | pre-line | <'white-space-collapse'> || <'text-wrap-mode'>

Preview

The quick brown fox jumps over the lazy dog. 素早い茶色の狐がのろまな犬を飛び越える。
Presets
Drag the bottom-right corner to resize

Code

.box {
  white-space: pre-wrap;
}

Controls

white-spacepre-wrap
white-space
The quick brown fox jumps over the lazy dog. 素早い茶色の狐がのろまな犬を飛び越える。

About

In HTML, runs of spaces and line breaks normally collapse into a single space, and lines wrap at the edge of the box. white-space can keep spaces and line breaks as written, or stop lines from wrapping.

Writing the value

  • normal: collapses spaces and line breaks, and wraps at the box width. This is the initial value.
  • nowrap: collapses spaces and line breaks, and never wraps. Long lines overflow the box.
  • pre: keeps spaces and line breaks as written, and never wraps. This is the default for the <pre> element.
  • pre-wrap: keeps spaces and line breaks, and also wraps at the box width.
  • pre-line: keeps only line breaks, collapses runs of spaces, and wraps at the box width.
  • break-spaces: almost the same as pre-wrap, but spaces at the end of a line take up room and can wrap too.

The newer syntax

white-space is now a shorthand for white-space-collapse, which handles spaces and line breaks, and text-wrap-mode, which decides whether lines wrap. For example, pre means preserve nowrap and pre-line means preserve-breaks wrap. The buttons on this page only list the long-standing keywords. A value such as preserve nowrap is read as the keyword with the same meaning; combinations with no matching keyword, such as preserve-spaces, can't be loaded.

Comments