CSS white-space-collapse Property
The CSS white-space-collapse property controls how spaces, tabs, and line breaks are collapsed, preserved, or converted when text is displayed.
By default, browsers combine consecutive whitespace characters into a single space and collapse most source line breaks. The white-space-collapse property provides more precise control over this behavior. It is a constituent property of the white-space shorthand and works alongside text wrapping properties to determine how text appears within an element.
white-space-collapse Syntax
The white-space-collapse property accepts six keyword values that determine how whitespace characters are processed.
Basic Syntax
.element {
white-space-collapse: collapse;
}
Available Values
.element {
white-space-collapse: collapse;
}
.element {
white-space-collapse: preserve;
}
.element {
white-space-collapse: preserve-breaks;
}
.element {
white-space-collapse: preserve-spaces;
}
.element {
white-space-collapse: break-spaces;
}
.element {
white-space-collapse: discard;
}
The discard value is defined in CSS Text Level 4 but is not currently implemented in mainstream browsers. Use the other values for practical examples.
CSS-Wide Keywords
.element {
white-space-collapse: initial;
}
.element {
white-space-collapse: inherit;
}
.element {
white-space-collapse: unset;
}
.element {
white-space-collapse: revert;
}
.element {
white-space-collapse: revert-layer;
}
Invalid Values
.element {
white-space-collapse: normal;
}
.element {
white-space-collapse: nowrap;
}
.element {
white-space-collapse: pre-wrap;
}
These values belong to the white-space shorthand rather than the white-space-collapse property.
Property Values
| Value | Description |
|---|---|
collapse |
Collapses consecutive whitespace characters and handles source line breaks according to normal whitespace processing rules. This is the initial value. |
preserve |
Preserves spaces, tabs, and line breaks from the source content. |
preserve-breaks |
Preserves source line breaks while collapsing other collapsible whitespace. |
preserve-spaces |
Preserves spaces and tabs while converting segment breaks into spaces. |
break-spaces |
Preserves whitespace and provides wrapping opportunities after preserved spaces, including spaces at line endings. |
discard |
Discards whitespace according to the specification. This value is experimental and lacks mainstream browser support. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | collapse |
| Inherited | Yes |
| Applies to | Text |
| Computed value | As specified |
| Animation type | Discrete |
| Percentages | Not applicable |
| Shorthand | white-space |
| Specification | CSS Text Module Level 4 |
How It Works
Whitespace processing determines how spaces, tabs, and source line breaks are represented during text layout.
In ordinary HTML content, browsers typically collapse repeated spaces and treat most source line breaks as spaces. The white-space-collapse property changes these rules without changing the underlying text characters.
HTML Example
<p class="example">CSS controls
how whitespace appears.</p>
Default Behavior
.example {
white-space-collapse: collapse;
}
The repeated spaces are collapsed, and the source line break is processed as ordinary collapsible whitespace.
Preserving Whitespace
.example {
white-space-collapse: preserve;
}
The browser preserves the repeated spaces and source line break.
Preserving Only Line Breaks
.example {
white-space-collapse: preserve-breaks;
}
The source line break remains, but repeated spaces are collapsed.
Wrapping Is Controlled Separately
Preserving whitespace does not necessarily disable automatic wrapping. The text-wrap-mode property controls whether text wraps when it reaches the available width.
Important Distinction
The white-space-collapse property controls whitespace processing. The white-space shorthand can control both whitespace processing and wrapping.
The collapse Value
The collapse value combines consecutive collapsible whitespace characters and processes source line breaks according to normal text layout rules.
Syntax
.element {
white-space-collapse: collapse;
}
Repeated Spaces
Multiple consecutive spaces are generally displayed as a single space.
Tabs
Tabs are processed as collapsible whitespace rather than preserving their original tab spacing.
Source Line Breaks
Source line breaks are normally collapsed or transformed according to the applicable text processing rules.
HTML Example
<p class="collapsed-text">First word
Second word</p>
CSS Example
.collapsed-text {
white-space-collapse: collapse;
}
Result
The text appears with ordinary word spacing rather than preserving the repeated spaces and source line break.
When to Use It
Use collapse for ordinary paragraphs, navigation text, labels, and other content where repeated source whitespace should not affect presentation.
The preserve Value
The preserve value retains spaces, tabs, and source line breaks instead of collapsing them.
Syntax
.element {
white-space-collapse: preserve;
}
HTML Example
<div class="formatted-text">First line
Indented line
Third line</div>
CSS Example
.formatted-text {
white-space-collapse: preserve;
}
The indentation and source line breaks remain visible.
Preserving Tabs
Tabs remain significant and are rendered using tab spacing behavior. The tab-size property can control their displayed width.
.formatted-text {
white-space-collapse: preserve;
tab-size: 4;
}
Automatic Wrapping
Unlike white-space: pre, setting white-space-collapse: preserve alone does not disable automatic wrapping.
Disable Wrapping
.formatted-text {
white-space-collapse: preserve;
text-wrap-mode: nowrap;
}
When to Use It
Use preserve when source spacing and line breaks should remain visible, such as formatted text, code examples, or user-entered content.
The preserve-breaks Value
The preserve-breaks value preserves source line breaks while collapsing repeated spaces and tabs.
Syntax
.element {
white-space-collapse: preserve-breaks;
}
HTML Example
<div class="formatted-text">First line
Second line
Third line</div>
CSS Example
.formatted-text {
white-space-collapse: preserve-breaks;
}
Result
Each source line remains separate, but repeated spaces within those lines are collapsed.
Relationship to pre-line
.formatted-text {
white-space: pre-line;
}
The pre-line shorthand corresponds to white-space-collapse: preserve-breaks with automatic wrapping enabled.
Addresses and Structured Text
.address {
white-space-collapse: preserve-breaks;
}
This is useful for displaying plain-text addresses or instructions where line breaks matter but extra spacing does not.
When to Use It
Use preserve-breaks when you want to retain source line breaks without preserving repeated spaces.
The preserve-spaces Value
The preserve-spaces value preserves spaces and tabs while converting source segment breaks into spaces.
Syntax
.element {
white-space-collapse: preserve-spaces;
}
HTML Example
<div class="spacing-example">First Second
Third Fourth</div>
CSS Example
.spacing-example {
white-space-collapse: preserve-spaces;
}
Result
The repeated spaces remain significant. The source line break is converted into a space rather than creating a forced line break.
Difference From preserve
The preserve value retains source line breaks, while preserve-spaces converts them into spaces.
Difference From collapse
The collapse value combines repeated whitespace, while preserve-spaces retains repeated spaces.
When to Use It
Use preserve-spaces when repeated spaces matter but source line breaks should not create separate lines.
The break-spaces Value
The break-spaces value preserves whitespace and allows additional wrapping opportunities after preserved spaces.
Syntax
.element {
white-space-collapse: break-spaces;
}
HTML Example
<div class="spacing-example">First Second Third</div>
CSS Example
.spacing-example {
white-space-collapse: break-spaces;
}
Preserved Spaces
Repeated spaces remain visible rather than collapsing into a single space.
Wrapping Opportunities
When wrapping is enabled, a wrapping opportunity exists after every preserved space.
Trailing Spaces
Preserved spaces at the ends of lines occupy space and contribute to line sizing rather than hanging outside the line's measured width.
Relationship to white-space: break-spaces
.spacing-example {
white-space: break-spaces;
}
The shorthand combines white-space-collapse: break-spaces with automatic wrapping.
Difference From preserve
Both values preserve whitespace, but break-spaces introduces additional wrapping opportunities and treats trailing spaces differently.
When to Use It
Use break-spaces when the exact amount of whitespace should affect both wrapping and layout dimensions.
The discard Value
The discard value is defined to discard whitespace characters rather than collapsing or preserving them.
Syntax
.element {
white-space-collapse: discard;
}
Intended Behavior
The value is intended to remove whitespace during text processing, producing a different result from collapsing multiple spaces into one.
Difference From collapse
The collapse value normally retains a single space where collapsible whitespace separates words. The discard value is designed to discard that whitespace.
Browser Support
The discard value is not implemented in mainstream browsers and should not be relied upon for production layouts.
Practical Alternative
If whitespace must be removed entirely, modify the source text or use an appropriate text-processing method rather than relying on this unsupported value.
Important Consideration
Browsers that do not recognize discard ignore the declaration and continue using the applicable supported value.
Relationship to white-space
The white-space-collapse property is one component of the white-space shorthand.
The shorthand also includes text-wrap-mode and the newer white-space-trim property.
Shorthand Equivalents
| white-space Value | white-space-collapse | text-wrap-mode |
|---|---|---|
normal |
collapse |
wrap |
nowrap |
collapse |
nowrap |
pre |
preserve |
nowrap |
pre-wrap |
preserve |
wrap |
pre-line |
preserve-breaks |
wrap |
break-spaces |
break-spaces |
wrap |
Using the Longhand
.element {
white-space-collapse: preserve;
text-wrap-mode: wrap;
}
Using the Shorthand
.element {
white-space: pre-wrap;
}
These declarations provide equivalent whitespace collapsing and wrapping behavior.
Prevent Wrapping
.element {
white-space-collapse: preserve;
text-wrap-mode: nowrap;
}
This corresponds to the established white-space: pre behavior.
Shorthand Reset Behavior
Declaring white-space resets its constituent properties, including any omitted components, to the values implied by the shorthand.
Declaration Order
.element {
white-space: normal;
white-space-collapse: preserve;
}
The later longhand declaration overrides the collapsing behavior established by the shorthand while retaining the shorthand's wrapping behavior.
Browser Compatibility
The established white-space keywords have broader historical support than the newer longhand properties.
Whitespace and Text Wrapping
Whitespace collapsing and automatic wrapping are related but separate parts of text layout.
Collapse Whitespace and Allow Wrapping
.element {
white-space-collapse: collapse;
text-wrap-mode: wrap;
}
This produces ordinary paragraph behavior.
Preserve Whitespace and Allow Wrapping
.element {
white-space-collapse: preserve;
text-wrap-mode: wrap;
}
Source whitespace is preserved, and text can wrap when necessary.
Preserve Whitespace Without Wrapping
.element {
white-space-collapse: preserve;
text-wrap-mode: nowrap;
}
Source whitespace remains visible, but automatic wrapping is disabled.
Long Unbroken Text
.element {
white-space-collapse: collapse;
overflow-wrap: anywhere;
}
The overflow-wrap property allows long unbroken strings to wrap when they would otherwise overflow.
Horizontal Scrolling
.code-example {
white-space: pre;
overflow-x: auto;
}
This preserves code formatting while allowing users to scroll horizontally when lines are too long.
Important Consideration
Use white-space-collapse to control whitespace preservation and text-wrap-mode to control automatic wrapping. The white-space shorthand can configure both.
Inheritance
The white-space-collapse property is inherited. Descendant text normally uses the parent's computed value unless another declaration overrides it.
Parent Declaration
.container {
white-space-collapse: preserve;
}
Descendant text normally preserves whitespace because the value is inherited.
Override on a Child
.container {
white-space-collapse: preserve;
}
.container .description {
white-space-collapse: collapse;
}
The description uses normal whitespace collapsing while other descendant text continues to inherit preserve.
Explicit Inheritance
.child {
white-space-collapse: inherit;
}
Restore the Initial Value
.element {
white-space-collapse: initial;
}
This restores the initial value of collapse.
Using unset
.element {
white-space-collapse: unset;
}
Because the property is inherited, unset behaves like inherit.
Important Consideration
Applying whitespace preservation to a large container can affect all descendant text. Use targeted selectors when only specific content needs preserved formatting.
Practical Examples
Collapse Repeated Spaces
.paragraph {
white-space-collapse: collapse;
}
Preserve Source Formatting
.formatted-content {
white-space-collapse: preserve;
}
Preserve Line Breaks in a Message
.message {
white-space-collapse: preserve-breaks;
}
Preserve Spaces Without Source Line Breaks
.spacing-example {
white-space-collapse: preserve-spaces;
}
Preserve Spaces With Additional Wrapping Opportunities
.spacing-sensitive {
white-space-collapse: break-spaces;
}
Preserve Code Indentation
.code-content {
white-space-collapse: preserve;
text-wrap-mode: nowrap;
overflow-x: auto;
}
Display Plain-Text Instructions
.instructions {
white-space-collapse: preserve-breaks;
}
Override an Inherited Value
.container {
white-space-collapse: preserve;
}
.container p {
white-space-collapse: collapse;
}
Preserve User-Entered Text
.user-content {
white-space-collapse: preserve;
text-wrap-mode: wrap;
overflow-wrap: anywhere;
}
Use a Widely Supported Shorthand
.user-content {
white-space: pre-wrap;
overflow-wrap: anywhere;
}
The shorthand provides similar behavior and is a practical fallback for browsers without the newer longhand support.
white-space-collapse Example
This interactive example demonstrates how different white-space-collapse values affect repeated spaces and source line breaks.
Select a value to compare how the same text is displayed. The example uses text-wrap-mode: wrap so that automatic wrapping remains enabled.
HTML Example
<div class="collapse-demo">
<label for="collapse-select">Choose a white-space-collapse value:</label>
<select id="collapse-select">
<option value="collapse">collapse</option>
<option value="preserve">preserve</option>
<option value="preserve-breaks">preserve-breaks</option>
<option value="preserve-spaces">preserve-spaces</option>
<option value="break-spaces">break-spaces</option>
</select>
<div class="collapse-output">CSS controls whitespace.
This line contains extra spaces.
Preserved spacing can affect how text wraps.</div>
</div>
CSS Example
.collapse-demo label {
display: block;
margin-bottom: 8px;
font-weight: bold;
}
.collapse-demo select {
margin-bottom: 15px;
padding: 8px;
}
.collapse-output {
max-width: 320px;
min-height: 130px;
padding: 15px;
background-color: #e5eef7;
border: 2px solid #0066cc;
border-radius: 6px;
white-space-collapse: collapse;
text-wrap-mode: wrap;
}
JavaScript Example
const select = document.querySelector("#collapse-select");
const output = document.querySelector(".collapse-output");
select.addEventListener("change", () => {
output.style.whiteSpaceCollapse = select.value;
});
Understanding the Results
The example contains repeated spaces and source line breaks. Each value changes how those characters are processed.
Selecting collapse
Repeated spaces are collapsed, and source line breaks are processed as ordinary whitespace.
Selecting preserve
Spaces and source line breaks are preserved.
Selecting preserve-breaks
Source line breaks remain visible, but repeated spaces are collapsed.
Selecting preserve-spaces
Repeated spaces remain significant, while source line breaks are converted into spaces.
Selecting break-spaces
Whitespace is preserved, with additional wrapping opportunities after preserved spaces.
Experiment With Wrapping
.collapse-output {
text-wrap-mode: nowrap;
}
Disabling automatic wrapping makes the differences between whitespace preservation and line breaking easier to observe.
Experiment With the Shorthand
.collapse-output {
white-space: pre-wrap;
}
This shorthand preserves whitespace while allowing automatic wrapping.
Accessibility Considerations
The white-space-collapse property affects visual whitespace processing but does not replace semantic HTML or change the underlying source text.
Preserve Meaningful Line Breaks
Use an appropriate value when source line breaks convey useful structure, such as instructions or plain-text messages.
Avoid Using Spaces for Layout
Repeated spaces are not a reliable substitute for CSS Grid, Flexbox, margins, or padding.
Support Text Reflow
Preserving whitespace can increase line width. Test content at narrow viewport sizes and with enlarged text.
Code Examples
When preserving code indentation, allow horizontal scrolling or appropriate wrapping so users can access the entire example.
Long Unbroken Strings
.user-content {
white-space-collapse: preserve;
overflow-wrap: anywhere;
}
This can help prevent long strings from overflowing narrow containers when automatic wrapping is enabled.
Semantic Structure
Use paragraphs, lists, headings, and other appropriate HTML elements rather than relying on whitespace alone to communicate document structure.
Screen Reader Considerations
Assistive technologies may interpret whitespace differently. Do not depend on repeated spaces or visual indentation to convey essential meaning.
Common Mistakes
Using white-space Values as Longhand Values
.element {
white-space-collapse: pre-wrap;
}
The value pre-wrap belongs to the white-space shorthand. Use preserve for the corresponding collapsing behavior.
Expecting preserve to Disable Wrapping
The preserve value controls whitespace processing, not automatic wrapping. Use text-wrap-mode: nowrap when wrapping must be disabled.
Confusing preserve and preserve-breaks
The preserve value retains spaces and line breaks, while preserve-breaks collapses repeated spaces but retains line breaks.
Confusing preserve and preserve-spaces
The preserve-spaces value converts source segment breaks into spaces instead of preserving them as forced line breaks.
Ignoring Trailing Spaces
The break-spaces value can cause trailing spaces to affect line sizing and wrapping.
Using discard in Production
The discard value is not implemented in mainstream browsers and should not be relied upon.
Ignoring Inheritance
A parent declaration can change whitespace processing throughout descendant content.
Overriding a Longhand With the Shorthand
.element {
white-space-collapse: preserve;
white-space: normal;
}
The later shorthand resets the collapsing behavior to collapse.
Assuming Longhand Support Matches Shorthand Support
The established white-space keywords have broader historical support than the newer longhand property.
Using Preserved Spaces for Alignment
Use CSS layout properties rather than relying on repeated spaces to align elements.
Important Considerations
Keep the following points in mind when using white-space-collapse:
- The property controls how whitespace characters are collapsed, preserved, or converted.
- Its initial value is
collapse. - The property is inherited.
- It is a constituent property of the
white-spaceshorthand. - The
collapsevalue provides ordinary whitespace collapsing. - The
preservevalue retains spaces, tabs, and source line breaks. - The
preserve-breaksvalue retains line breaks while collapsing repeated spaces. - The
preserve-spacesvalue retains spaces while converting source segment breaks into spaces. - The
break-spacesvalue preserves whitespace and introduces additional wrapping opportunities. - The
discardvalue is experimental and lacks mainstream browser support. - The property does not remove characters from the underlying HTML source.
- Whitespace preservation can affect layout dimensions.
- Automatic wrapping is controlled separately by
text-wrap-mode. - The
white-spaceshorthand can control both collapsing and wrapping. - Declaring the shorthand can reset previously specified longhand values.
- The
white-space: normaldeclaration corresponds to collapsing whitespace with wrapping enabled. - The
white-space: predeclaration corresponds to preserving whitespace with wrapping disabled. - The
white-space: pre-wrapdeclaration corresponds to preserving whitespace with wrapping enabled. - The
white-space: pre-linedeclaration corresponds to preserving line breaks with wrapping enabled. - The
white-space: break-spacesdeclaration corresponds to thebreak-spacescollapsing behavior with wrapping enabled. - Preserved whitespace can contribute to overflow in narrow containers.
- Use
overflow-wrapwhen long unbroken strings need additional wrapping opportunities. - Use
tab-sizewhen preserved tab spacing needs adjustment. - Use semantic HTML rather than whitespace to communicate structure.
- Test text reflow and accessibility at different viewport widths.
- Use established
white-spaceshorthand values as fallbacks when necessary. - Check browser compatibility before relying on newer longhand values.
Browser Support
The CSS white-space-collapse property has limited availability compared with the established white-space shorthand.
Modern browser support has expanded, but compatibility varies by browser version and individual value. The discard value is not supported in mainstream browsers.
For current compatibility information, visit Can I Use? and search for white-space-collapse.
Standard Declaration
.element {
white-space-collapse: preserve;
}
CSS Feature Detection
@supports (white-space-collapse: preserve) {
.element {
white-space-collapse: preserve;
}
}
Fallback Example
.element {
white-space: pre-wrap;
}
@supports (white-space-collapse: preserve) {
.element {
white-space-collapse: preserve;
}
}
The established shorthand provides a fallback while the longhand declaration applies in browsers that support it.
Testing Individual Values
@supports (white-space-collapse: break-spaces) {
.element {
white-space-collapse: break-spaces;
}
}
Feature detection can help determine whether a browser recognizes a specific value, but visual testing remains important for verifying its behavior.
Vendor Prefixes
The standard white-space-collapse property does not require vendor prefixes in supporting browsers.
Testing Recommendations
Test repeated spaces, source line breaks, preserved tabs, trailing spaces, and wrapping behavior in the browsers you intend to support.
Specifications
The white-space-collapse property is defined in CSS Text Module Level 4.
CSS Text Module Level 4 - White Space Collapsing
The specification defines the available values and the rules used to process collapsible and preserved whitespace.
For additional documentation and examples, consult MDN Web Docs.
