The white-space-collapse
CSS property controls how white space inside an element is collapsed.
Note: The white-space-collapse
and text-wrap
properties can be declared together using the white-space
shorthand property.
Since March 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
The white-space-collapse
CSS property controls how white space inside an element is collapsed.
Note: The white-space-collapse
and text-wrap
properties can be declared together using the white-space
shorthand property.
/* Keyword values */ white-space-collapse: collapse; white-space-collapse: preserve; white-space-collapse: preserve-breaks; white-space-collapse: preserve-spaces; white-space-collapse: break-spaces; /* Global values */ white-space-collapse: inherit; white-space-collapse: initial; white-space-collapse: revert; white-space-collapse: revert-layer; white-space-collapse: unset;
The white-space-collapse
property is specified as a single keyword chosen from the list of values below.
collapse
White space sequences are collapsed.
preserve
White space sequences and segment break characters are preserved.
preserve-breaks
White space sequences are collapsed, while segment break characters are preserved.
preserve-spaces
White space sequences are preserved, while tabs and segment break characters are converted to spaces.
break-spaces
The behavior is identical to preserve
, except that:
min-content
size and max-content
size).Note: Segment break characters are characters such as line feeds that cause text to break onto new lines.
User agents handle white space collapsing as follows:
break-spaces
value however, a soft wrap could potentially occur after each space, so the next line may start with one or more spaces.Initial value | collapse |
---|---|
Applies to | all elements |
Inherited | yes |
Computed value | as specified |
Animation type | discrete |
<h2 class="collapse">Default behavior; all whitespace is collapsed in the heading .</h2> <h2 class="preserve">In this case all whitespace is preserved in the heading .</h2> <h2 class="preserve-breaks">In this case only the line breaks are preserved in the heading .</h2>
.collapse { white-space-collapse: collapse; } .preserve { white-space-collapse: preserve; } .preserve-breaks { white-space-collapse: preserve-breaks; } h2 { font-size: 1.6rem; font-family: monospace; }
Specification |
---|
CSS Text Module Level 4 # white-space-collapsing |
Desktop | Mobile | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | |
white-space-collapse |
114 | 114 | 124 | 100 | 17.4 | 114 | 124 | 76 | 17.4 | 23.0 | 114 |
break-spaces |
114 | 114 | 124 | 100 | 17.4 | 114 | 124 | 76 | 17.4 | 23.0 | 114 |
collapse |
114 | 114 | 124 | 100 | 17.4 | 114 | 124 | 76 | 17.4 | 23.0 | 114 |
preserve |
114 | 114 | 124 | 100 | 17.4 | 114 | 124 | 76 | 17.4 | 23.0 | 114 |
preserve-breaks |
114 | 114 | 124 | 100 | 17.4 | 114 | 124 | 76 | 17.4 | 23.0 | 114 |
preserve-spaces |
No | No | 124 | No | No | No | 124 | No | No | No | No |
white-space-collapse
and text-wrap
: The white-space
property.
© 2005–2024 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/white-space-collapse