CSS white-space Property
The CSS white-space property controls how whitespace characters and line breaks are handled within an element, including whether text wraps onto additional lines.
Whitespace includes spaces, tabs, and line breaks in the source content. The white-space property can collapse repeated spaces, preserve formatting, prevent automatic wrapping, or allow text to wrap while retaining selected whitespace characters. It is a shorthand for white-space-collapse, text-wrap-mode, and white-space-trim, although support for the newer values and constituent properties varies.
white-space Syntax
The white-space property accepts several established keyword values and newer shorthand combinations.
Basic Syntax
.element {
white-space: normal;
}
Common Values
.element {
white-space: normal;
}
.element {
white-space: nowrap;
}
.element {
white-space: pre;
}
.element {
white-space: pre-wrap;
}
.element {
white-space: pre-line;
}
.element {
white-space: break-spaces;
}
Newer Shorthand Values
.element {
white-space: collapse;
}
.element {
white-space: preserve nowrap;
}
.element {
white-space: preserve wrap;
}
.element {
white-space: preserve-breaks wrap;
}
.element {
white-space: preserve-spaces wrap;
}
These combinations express whitespace collapsing and wrapping behavior using the shorthand's constituent values. Support for newer combinations may vary.
CSS-Wide Keywords
.element {
white-space: initial;
}
.element {
white-space: inherit;
}
.element {
white-space: unset;
}
.element {
white-space: revert;
}
.element {
white-space: revert-layer;
}
Invalid Values
.element {
white-space: none;
}
.element {
white-space: hidden;
}
.element {
white-space: true;
}
Values such as none, hidden, and true are not valid for the white-space property.
Property Values
| Value | Description |
|---|---|
normal |
Collapses repeated whitespace and allows text to wrap normally. This is the initial value. |
nowrap |
Collapses repeated whitespace but prevents automatic line wrapping. |
pre |
Preserves spaces and line breaks while preventing automatic wrapping. |
pre-wrap |
Preserves spaces and line breaks while allowing automatic wrapping. |
pre-line |
Collapses repeated spaces and tabs, preserves source line breaks, and allows automatic wrapping. |
break-spaces |
Preserves whitespace, allows wrapping, and treats preserved spaces differently from pre-wrap, including at line endings. |
collapse |
Sets whitespace collapsing behavior using the shorthand. |
preserve |
Preserves whitespace, corresponding to the white-space-collapse value. |
preserve-breaks |
Preserves line breaks while collapsing other collapsible whitespace. |
preserve-spaces |
Preserves spaces and tabs while treating segment breaks as spaces. |
wrap |
Allows automatic line wrapping when used in a valid shorthand combination. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | normal |
| Inherited | Yes |
| Applies to | Text and elements containing text |
| Computed value | Computed values of the constituent properties |
| Animation type | Discrete |
| Percentages | Not applicable |
| Shorthand for | white-space-collapse, text-wrap-mode, and white-space-trim |
| Specification | CSS Text Module Level 3 and Level 4 |
Note: The established keyword values remain useful and widely supported. Newer shorthand components allow more precise control, but not every browser supports all combinations.
How It Works
Browsers normally collapse repeated spaces and allow text to wrap when it reaches the available width of its container.
The white-space property changes these behaviors by controlling whitespace preservation and automatic line wrapping.
HTML Example
<p class="example">CSS controls
how text appears.</p>
Default Behavior
.example {
white-space: normal;
}
Repeated spaces and the source line break are collapsed into ordinary word spacing. Text wraps as needed.
Preserve Whitespace
.example {
white-space: pre;
}
The browser preserves the spaces and line break from the HTML source.
Preserve Whitespace and Allow Wrapping
.example {
white-space: pre-wrap;
}
The browser preserves the original formatting but also wraps text when necessary.
Prevent Automatic Wrapping
.example {
white-space: nowrap;
}
The browser collapses repeated whitespace but keeps the text on a single line unless another mechanism introduces a forced break.
Important Distinction
The property controls how whitespace and line wrapping are processed. It does not add or remove characters from the underlying HTML source.
The normal Value
The normal value is the default. It collapses repeated whitespace and allows automatic line wrapping.
Syntax
.element {
white-space: normal;
}
Repeated Spaces
Multiple consecutive spaces are generally displayed as a single space.
Source Line Breaks
Line breaks in ordinary HTML text are generally collapsed into spaces, subject to the text processing rules.
Automatic Wrapping
Text can wrap onto additional lines when it reaches the available width.
Example
<p class="normal-text">This text contains
extra spaces and a line break.</p>
.normal-text {
white-space: normal;
}
The paragraph displays with ordinary word spacing and wraps according to the container width.
When to Use It
Use normal for paragraphs, articles, navigation labels, and most standard page content.
The nowrap Value
The nowrap value collapses repeated whitespace but prevents automatic line wrapping.
Syntax
.element {
white-space: nowrap;
}
Single-Line Text
.label {
white-space: nowrap;
}
The label remains on one line even when its content exceeds the available width.
Button Labels
.button-label {
white-space: nowrap;
}
Navigation Links
.navigation a {
white-space: nowrap;
}
Text Overflow
Long text may overflow its container when automatic wrapping is disabled.
Ellipsis Example
.single-line-title {
width: 220px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
This combination truncates overflowing single-line text and displays an ellipsis when supported.
Important Consideration
Use nowrap selectively. Long text that cannot wrap may cause horizontal scrolling or overflow on smaller screens.
The pre Value
The pre value preserves spaces, tabs, and source line breaks while preventing automatic line wrapping.
Syntax
.element {
white-space: pre;
}
HTML Example
<div class="formatted-text">First line
Indented line
Third line</div>
CSS Example
.formatted-text {
white-space: pre;
}
The browser preserves the line breaks and indentation from the source.
Relationship to the pre Element
The HTML <pre> element commonly uses white-space: pre in browser default styles.
Long Lines
Text does not automatically wrap, so long lines can overflow the container.
Code Examples
.code-sample {
white-space: pre;
overflow-x: auto;
}
This preserves code formatting and provides horizontal scrolling when necessary.
When to Use It
Use pre when preserving exact spacing and line breaks is more important than automatic wrapping.
The pre-wrap Value
The pre-wrap value preserves whitespace and source line breaks while allowing text to wrap when needed.
Syntax
.element {
white-space: pre-wrap;
}
Preserved Formatting
<div class="formatted-text">First line
Indented line
Third line with additional text that may wrap.</div>
.formatted-text {
white-space: pre-wrap;
}
Indentation and source line breaks remain visible, but long lines can wrap within the container.
Displaying User-Entered Text
.message-content {
white-space: pre-wrap;
}
This is useful for displaying plain-text messages containing intentional line breaks.
Formatted Instructions
.instructions {
white-space: pre-wrap;
}
Trailing Spaces
Unlike break-spaces, preserved spaces at the ends of lines can hang rather than contributing to line sizing in the same way.
When to Use It
Use pre-wrap when you need to preserve source formatting while keeping text responsive to narrower containers.
The pre-line Value
The pre-line value preserves source line breaks while collapsing repeated spaces and tabs.
Syntax
.element {
white-space: pre-line;
}
HTML Example
<p class="formatted-text">First line
Second line
Third line</p>
CSS Example
.formatted-text {
white-space: pre-line;
}
The source line breaks are preserved, but repeated spaces are collapsed.
Automatic Wrapping
Text can wrap within each preserved line when it reaches the available width.
Formatted Text
.address {
white-space: pre-line;
}
This can display a multi-line address or similar content without preserving extra indentation.
Final Line Break
A final source line break does not necessarily produce an additional visible blank line under the pre-line processing rules.
When to Use It
Use pre-line when line breaks matter but repeated spaces should be normalized.
The break-spaces Value
The break-spaces value preserves whitespace and line breaks while allowing text to wrap, including at preserved spaces.
Syntax
.element {
white-space: break-spaces;
}
Preserve Repeated Spaces
<div class="spacing-example">First Second Third</div>
.spacing-example {
white-space: break-spaces;
}
The browser preserves the repeated spaces and allows wrapping opportunities after preserved spaces.
Preserve Trailing Spaces
Unlike pre-wrap, preserved spaces at line endings take up space and affect the layout.
Wrapping Behavior
Each preserved space can create an additional wrapping opportunity after that space.
Whitespace-Sensitive Content
.spacing-sensitive {
white-space: break-spaces;
}
This is useful when the exact amount of whitespace should be reflected in the layout.
Difference From pre-wrap
| Behavior | pre-wrap |
break-spaces |
|---|---|---|
| Preserves repeated spaces | Yes | Yes |
| Preserves source line breaks | Yes | Yes |
| Allows automatic wrapping | Yes | Yes |
| Trailing spaces affect line sizing | Can hang | Yes |
| Wrapping opportunity after every preserved space | No | Yes |
When to Use It
Use break-spaces when preserved spaces must affect wrapping and layout rather than hanging at line endings.
Shorthand Components
Modern CSS defines white-space as a shorthand for three properties that separately control whitespace collapsing, wrapping, and trimming.
white-space-collapse
.element {
white-space-collapse: preserve;
}
The white-space-collapse property determines how collapsible whitespace is handled.
text-wrap-mode
.element {
text-wrap-mode: nowrap;
}
The text-wrap-mode property controls whether automatic text wrapping is allowed.
white-space-trim
.element {
white-space-trim: discard-before;
}
The white-space-trim property is designed to control selected whitespace trimming behavior. It remains experimental and is not broadly supported.
Equivalent Shorthand Values
| 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 |
Shorthand Example
.element {
white-space: preserve nowrap;
}
This corresponds to preserving whitespace while disabling automatic wrapping.
Longhand Equivalent
.element {
white-space-collapse: preserve;
text-wrap-mode: nowrap;
}
Resetting Omitted Components
As with other CSS shorthands, assigning white-space also resets omitted constituent properties to their initial values.
Browser Support
The established shorthand keywords are broadly supported. Newer combinations and white-space-trim require additional compatibility testing.
Wrapping and Overflow
The white-space property affects wrapping behavior, but it does not control every type of text overflow.
Preventing Wrapping
.label {
white-space: nowrap;
}
The text remains on one line and may overflow its container.
Allowing Wrapping
.paragraph {
white-space: normal;
}
Breaking Long Words
.long-content {
white-space: normal;
overflow-wrap: anywhere;
}
The overflow-wrap property allows long unbroken strings to wrap when they would otherwise overflow.
Using word-break
.text {
word-break: break-all;
}
The word-break property controls additional word-breaking behavior. Use it carefully because it can break words at visually awkward positions.
Single-Line Ellipsis
.title {
width: 220px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
Horizontal Scrolling
.code-block {
white-space: pre;
overflow-x: auto;
}
This combination preserves formatting and allows horizontal scrolling when content is wider than the container.
Important Consideration
The white-space property controls whitespace processing and automatic wrapping. Properties such as overflow, overflow-wrap, and text-overflow handle related but different layout behaviors.
Inheritance
The white-space property is inherited. Child elements normally receive their parent's whitespace processing and wrapping behavior unless they specify another value.
Parent Declaration
.container {
white-space: nowrap;
}
Text in descendant elements normally inherits the no-wrap behavior.
Override on a Child
.container {
white-space: nowrap;
}
.container .description {
white-space: normal;
}
The description can wrap normally even though the parent uses nowrap.
Explicit Inheritance
.child {
white-space: inherit;
}
Restore the Initial Value
.element {
white-space: initial;
}
This restores the initial shorthand behavior corresponding to normal.
Using unset
.element {
white-space: unset;
}
Because the shorthand's constituent properties are inherited, unset uses their inherited values.
Important Consideration
A parent declaration can unintentionally prevent wrapping in descendants. Apply nowrap only where single-line behavior is needed.
Practical Examples
Normal Paragraph Wrapping
.paragraph {
white-space: normal;
}
Single-Line Navigation Label
.navigation-label {
white-space: nowrap;
}
Preserve Code Formatting
.code-example {
white-space: pre;
overflow-x: auto;
}
Preserve User-Entered Line Breaks
.user-message {
white-space: pre-wrap;
}
Preserve Line Breaks Without Extra Spaces
.formatted-address {
white-space: pre-line;
}
Preserve All Spaces With Wrapping
.spacing-example {
white-space: break-spaces;
}
Truncate a Long Heading
.heading {
max-width: 280px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
Responsive Preformatted Text
.preformatted-content {
white-space: pre-wrap;
overflow-wrap: anywhere;
}
Prevent Button Text From Wrapping
.button {
white-space: nowrap;
}
Allow Long URLs to Wrap
.url-text {
white-space: normal;
overflow-wrap: anywhere;
}
white-space Example
This interactive example demonstrates how different white-space values affect repeated spaces, source line breaks, and automatic text wrapping.
Select a value to change the formatting of the example text. The container has a fixed maximum width so you can observe how wrapping changes.
HTML Example
<div class="whitespace-demo">
<label for="whitespace-select">Choose a white-space value:</label>
<select id="whitespace-select">
<option value="normal">normal</option>
<option value="nowrap">nowrap</option>
<option value="pre">pre</option>
<option value="pre-wrap">pre-wrap</option>
<option value="pre-line">pre-line</option>
<option value="break-spaces">break-spaces</option>
</select>
<div class="whitespace-output">CSS controls whitespace.
This line contains extra spaces.
Long text can demonstrate how wrapping changes inside a narrow container.</div>
</div>
CSS Example
.whitespace-demo label {
display: block;
margin-bottom: 8px;
font-weight: bold;
}
.whitespace-demo select {
margin-bottom: 15px;
padding: 8px;
}
.whitespace-output {
max-width: 320px;
min-height: 130px;
padding: 15px;
background-color: #e5eef7;
border: 2px solid #0066cc;
border-radius: 6px;
white-space: normal;
}
JavaScript Example
const select = document.querySelector("#whitespace-select");
const output = document.querySelector(".whitespace-output");
select.addEventListener("change", () => {
output.style.whiteSpace = select.value;
});
Understanding the Results
The example contains repeated spaces, source line breaks, and a long line of text.
Selecting normal
Repeated spaces and source line breaks are collapsed. Text wraps normally.
Selecting nowrap
Whitespace collapses, but automatic wrapping is disabled. Long text may extend beyond the container.
Selecting pre
Spaces and line breaks are preserved. Text does not automatically wrap.
Selecting pre-wrap
Spaces and line breaks are preserved while automatic wrapping remains enabled.
Selecting pre-line
Source line breaks remain visible, but repeated spaces are collapsed.
Selecting break-spaces
Whitespace is preserved, and additional wrapping opportunities are available after preserved spaces.
Experiment With the Container Width
.whitespace-output {
max-width: 200px;
}
A narrower container makes the differences between wrapping values more noticeable.
Accessibility Considerations
The white-space property changes visual text formatting but does not change the underlying text content or document semantics.
Allow Text to Reflow
Avoid applying white-space: nowrap to long paragraphs or other content that must remain readable at narrow viewport widths.
Support Text Zoom
Test layouts with increased text size. Preventing wrapping can cause content to overflow or become difficult to read.
Preserve Meaningful Formatting
Use pre-wrap or pre-line when source line breaks communicate useful structure.
Do Not Use Spaces for Page Layout
Use CSS layout systems such as Flexbox and Grid rather than repeated spaces to position content.
Code Examples
When preserving code formatting, provide horizontal scrolling or appropriate wrapping so content remains accessible on smaller screens.
Avoid Unexpected Overflow
Long unbroken strings may need overflow-wrap: anywhere to remain readable.
Screen Reader Considerations
Whitespace presentation does not replace semantic HTML. Use appropriate elements and accessible labels to communicate structure and meaning.
Common Mistakes
Using white-space: none
.element {
white-space: none;
}
The value none is invalid. Use an appropriate supported keyword such as normal or nowrap.
Expecting nowrap to Hide Overflow
The nowrap value prevents automatic wrapping but does not clip overflowing text.
Expecting pre to Wrap Long Lines
The pre value preserves formatting and disables automatic wrapping. Use pre-wrap when wrapping is needed.
Confusing pre-wrap and pre-line
The pre-wrap value preserves repeated spaces, while pre-line collapses them.
Confusing pre-wrap and break-spaces
Both preserve whitespace, but break-spaces handles preserved spaces at line endings and wrapping opportunities differently.
Using Repeated Spaces for Layout
Whitespace preservation is not a substitute for CSS layout properties.
Ignoring Inheritance
A parent with white-space: nowrap can prevent descendant text from wrapping unless it is overridden.
Assuming white-space Breaks Long Words
The property controls whitespace and wrapping behavior. Long unbroken strings may still need overflow-wrap.
Ignoring Browser Support for New Values
Newer shorthand combinations and trimming features may not be available in every browser.
Using nowrap on Responsive Content
Preventing wrapping across an entire content area can cause horizontal overflow on small screens.
Important Considerations
Keep the following points in mind when using white-space:
- The property controls whitespace processing and automatic text wrapping.
- Its initial value is
normal. - The property is inherited.
- The established values are
normal,nowrap,pre,pre-wrap,pre-line, andbreak-spaces. - The
normalvalue collapses repeated whitespace and allows wrapping. - The
nowrapvalue prevents automatic wrapping. - The
prevalue preserves whitespace without automatic wrapping. - The
pre-wrapvalue preserves whitespace and allows wrapping. - The
pre-linevalue preserves source line breaks while collapsing repeated spaces. - The
break-spacesvalue preserves whitespace and provides additional wrapping opportunities after preserved spaces. - The property does not remove whitespace characters from the HTML source.
- The property can affect line length, overflow, and layout dimensions.
- Preventing wrapping can cause horizontal overflow.
- Long unbroken strings may require
overflow-wrap. - The
text-overflowproperty controls how certain clipped text is represented. - The
overflowproperty controls clipping and scrolling behavior. - The
white-spaceproperty is defined as a shorthand in modern CSS. - The shorthand includes
white-space-collapse,text-wrap-mode, andwhite-space-trim. - Newer shorthand combinations may have limited browser support.
- The
white-space-trimproperty remains experimental. - Child elements inherit whitespace behavior unless overridden.
- Use
pre-wrapfor plain-text content with meaningful source formatting. - Use
pre-linewhen source line breaks matter but repeated spaces do not. - Use
nowrapselectively for short labels and controls. - Use CSS layout systems rather than repeated spaces to position elements.
- Test text reflow at narrow viewport widths and increased text sizes.
- Check browser compatibility when using newer values.
Browser Support
The CSS white-space property is widely available across modern browsers and has been supported for many years.
The established keyword values have broad compatibility. Newer shorthand combinations and constituent properties, especially white-space-trim, may have limited support.
For current compatibility information, visit Can I Use? and search for white-space.
Standard Declaration
.element {
white-space: pre-wrap;
}
CSS Feature Detection
@supports (white-space: break-spaces) {
.element {
white-space: break-spaces;
}
}
Fallback Example
.element {
white-space: pre-wrap;
}
@supports (white-space: break-spaces) {
.element {
white-space: break-spaces;
}
}
Browsers that do not support break-spaces retain the broadly supported pre-wrap declaration.
Vendor Prefixes
The standard white-space property does not require vendor prefixes in modern browsers.
Testing Recommendations
Test long lines, repeated spaces, source line breaks, narrow containers, and newer shorthand combinations in the browsers you intend to support.
Specifications
The white-space property is defined in CSS Text Module Level 3, with additional shorthand behavior and constituent properties specified in CSS Text Module Level 4.
CSS Text Module Level 3 - white-space
CSS Text Module Level 4 - white-space
These specifications define whitespace collapsing, line breaking, wrapping behavior, and the modern shorthand structure.
For additional documentation and examples, consult MDN Web Docs.
