CSS propertyTypography

white-space

Decides how to treat spaces, line breaks, and line wrapping in text.

Initial value
normal
Inherited
Yes
Animatable
no
Applies to
all elements
JavaScript
element.style.whiteSpace = "nowrap"
On this page

Example

#
CSS
.message {
  white-space: pre-line;
}
Preview

Definition and usage

#

By default, the browser collapses multiple spaces and newline characters in the code into a single space, and text wraps automatically. The white-space property lets you change that.

nowrap turns off wrapping, so the text stays on one line. pre preserves every space and newline exactly as written in the code, with no wrapping. pre-wrap also preserves them, but allows long lines to wrap, and pre-line preserves only the newlines.

white-space: nowrap together with overflow: hidden and text-overflow: ellipsis is the classic recipe for truncating text with an ellipsis.

Syntax

#
Syntax
white-space: normal | nowrap | pre | pre-wrap | pre-line | break-spaces;

Values

#
  • normal

    Spaces collapse, text wraps automatically.
  • nowrap

    A single line with no wrapping, text overflows the container.
  • pre

    Spaces and newlines exactly as in the code, with no wrapping.
  • pre-wrap

    Spaces and newlines exactly as in the code, with wrapping.
  • pre-line

    Newlines preserved, spaces collapsed.

Values in practice

#

normal

Spaces collapse, text wraps automatically.
.demo { white-space: normal; }

nowrap

A single line with no wrapping, text overflows the container.
.demo { white-space: nowrap; }

pre

Spaces and newlines exactly as in the code, with no wrapping.
.demo { white-space: pre; }

pre-wrap

Spaces and newlines exactly as in the code, with wrapping.
.demo { white-space: pre-wrap; }

pre-line

Newlines preserved, spaces collapsed.
.demo { white-space: pre-line; }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Display user-entered text, like a comment with line breaks, with white-space: pre-line.
  • Protect buttons and labels that shouldn’t break onto two lines with nowrap.

Related entries

#

Spotted a mistake or missing an example? Write to us.