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).
.code { white-space: pre; } .message { white-space: pre-wrap; } .badge { white-space: nowrap; }
Try it
Syntax
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-collapseandtext-wrap-mode. For example,preispreserveplusnowrap. - Defaults in HTML.
<pre>usespre, and<textarea>usespre-wrap. Settingpre-wrapon a<pre>lets long code lines wrap. - Forced breaks stay. A
<br>element always breaks the line, even withnowrap. - Ellipsis. Truncating text needs
white-space: nowraptogether withoverflowandtext-overflow. - User text. To show multi-line text from a database or form,
white-space: pre-lineorpre-wrapkeeps the line breaks without needing<br>tags. - Tabs. Preserved tab characters are 8 spaces wide by default;
tab-sizechanges that. - Mobile and zoom.
nowrapandprecan 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.
Related
white-space-collapseโ whitespace collapsingtext-wrap-modeโ wrap or nowraptext-overflowโ ellipsistab-sizeโ width of a tab- Lesson: Wrapping, Overflow and Hyphens