Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Typography

The white-space property

Reference for the CSS white-space property: how spaces, tabs and line breaks are collapsed or preserved and whether text wraps, with a keyword table and live examples.

The white-space property controls two things at once: what happens to the spaces, tabs and line breaks in your source, and whether long lines wrap. By default, runs of whitespace collapse into one space and lines wrap when they run out of room.

Its keywords cover the cases you need most often: keep text on one line (nowrap), show text exactly as typed (pre), or keep line breaks while still wrapping (pre-wrap, pre-line).

CSS
.code { white-space: pre; }
.message { white-space: pre-wrap; }
.badge { white-space: nowrap; }

Try it

Comparing white-space values

Syntax

CSS
white-space: normal | pre | pre-wrap | pre-line | nowrap | break-spaces

/* newer longhand form */
white-space: <'white-space-collapse'> || <'text-wrap-mode'>
  • Initial value: normal
  • Applies to: text and the elements that contain it
  • Inherited: yes
  • Animatable: no
  • Computed value: as specified

Values

Value Spaces and tabs Line breaks in source Wraps
normal collapse collapse yes
nowrap collapse collapse no
pre preserved preserved no
pre-wrap preserved preserved yes
pre-line collapse preserved yes
break-spaces preserved preserved yes

break-spaces is like pre-wrap, except that trailing preserved spaces take up room and may wrap to the next line, instead of hanging past the edge.

Usage notes

  • Shorthand. Each keyword stands for a combination of white-space-collapse and text-wrap-mode. For example, pre is preserve plus nowrap.
  • Defaults in HTML. <pre> uses pre, and <textarea> uses pre-wrap. Setting pre-wrap on a <pre> lets long code lines wrap.
  • Forced breaks stay. A <br> element always breaks the line, even with nowrap.
  • Ellipsis. Truncating text needs white-space: nowrap together with overflow and text-overflow.
  • User text. To show multi-line text from a database or form, white-space: pre-line or pre-wrap keeps the line breaks without needing <br> tags.
  • Tabs. Preserved tab characters are 8 spaces wide by default; tab-size changes that.
  • Mobile and zoom. nowrap and pre can cause horizontal scrolling on narrow screens. Use them for short pieces of text.

Browser support

The keyword values are supported in every browser. The longhand form with white-space-collapse and text-wrap-mode is newer, so check a compatibility table before using it.