CSS Reference ยท Typography
The white-space-collapse property
Reference for the CSS white-space-collapse property: control how spaces, tabs and line breaks collapse or are preserved, with values, examples and how it relates to white-space.
The white-space-collapse property decides what happens to sequences of spaces, tabs and line breaks in the text. By default they collapse: any run of whitespace becomes a single space, and a line break in your source is treated like a space.
It is one of the two longhands behind white-space (the other is text-wrap-mode). Use it when you want to change only the collapsing and leave wrapping alone.
.poem { white-space-collapse: preserve-breaks; }
Try it
If your browser does not yet support the longhand, all four boxes look like the first one.
Syntax
white-space-collapse: collapse | preserve | preserve-breaks | preserve-spaces | break-spaces
- Initial value:
collapse - Applies to: text and the elements that contain it
- Inherited: yes
- Animatable: no
- Computed value: as specified
Values
| Value | Description |
|---|---|
collapse |
Runs of spaces and tabs collapse into one space; source line breaks become spaces. Spaces at the start and end of a line are removed. |
preserve |
Spaces, tabs and line breaks are kept exactly. |
preserve-breaks |
Line breaks are kept, while runs of spaces and tabs collapse. |
preserve-spaces |
Spaces and tabs are kept, while line breaks are turned into spaces. |
break-spaces |
Like preserve, but trailing spaces take up room and can wrap to the next line instead of hanging past the edge. |
Usage notes
- Pair it with wrapping. Alone, this property does not stop lines from wrapping. For a
<pre>-like block, combinepreservewithtext-wrap-mode: nowrap, which is the same aswhite-space: pre. - Common mappings.
white-space: pre-wrapispreservewith wrapping, andpre-lineispreserve-breakswith wrapping. - Shorthand resets it. Declaring
white-space: normalafter this property will reset it, because the shorthand covers it. Mind the order in your CSS. - Source formatting. Indentation inside an element with
preserveshows up on the page, so avoid indenting template markup inside such elements. - Not for layout. Do not use preserved spaces to line things up; use real layout tools such as flexbox, grid or tables.
- Accessibility. Screen readers generally read the text rather than the extra spaces, so spacing alone cannot carry meaning.
Browser support
A newer longhand. The shorthand white-space is supported everywhere, but the standalone property and some values are more recent, so check a compatibility table before using it.
Related
white-spaceโ the shorthandtext-wrap-modeโ wrap or nowraptab-sizeโ width of preserved tabs- Lesson: Wrapping, Overflow and Hyphens