white-space
Specifies how whitespace characters (spaces, tab characters, and line breaks) in text are displayed.
Sample Code
style.css
/* normal: collapse whitespace, wrap as needed (initial value) */
p.test { white-space: normal;}
/* pre: preserve spaces, tabs, and line breaks as-is (good for code display) */
p.test2 { white-space: pre;}
/* nowrap: prevent line breaks (text extends on a single line) */
p.test3 { white-space: nowrap;}
/* pre-wrap: preserve whitespace and line breaks, but wrap at box edge */
p.test4 { white-space: pre-wrap;}
/* pre-line: collapse spaces, preserve explicit line breaks */
p.test5 { white-space: pre-line;}
/* Classic pattern: single-line text with ellipsis overflow */
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis;}
/* Preserve line breaks in chat / comment areas */
.comment { white-space: pre-wrap; word-break: break-word;}
Available Values
| Value | Description |
|---|---|
| normal | Consecutive spaces, tabs, and line breaks in the text are displayed as a single space. Text wraps automatically to fit the containing element's width. This 'normal' is the initial value. |
| pre | Spaces, tabs, and line breaks in the text are preserved and displayed as-is. No automatic wrapping at the box edge. |
| nowrap | Consecutive spaces, tabs, and line breaks in the text are displayed as a single space. Text does not wrap automatically. |
| pre-wrap | Spaces, tabs, and line breaks are preserved and displayed as-is. Text also wraps automatically when it reaches the box edge. |
| pre-line | Consecutive spaces and tabs are collapsed to a single space, but line breaks are preserved. Text also wraps automatically at the box edge. |
Browser Display Result
These samples use the following string:
This is a p element. Text. Text. Text. Text. Text.
<p style="border: solid 1px; white-space: normal; width: 200px;">This is a p element. Text. Text. Text. Text. Text.</p>
<p style="border: solid 1px; white-space: pre; width: 200px;">This is a p element. Text. Text. Text. Text. Text.</p>
<p style="border: solid 1px; white-space: nowrap; width: 200px;">This is a p element. Text. Text. Text. Text. Text.</p>
Common Mistakes
Using nowrap without handling overflow
'white-space: nowrap' keeps text on a single line, but if the text exceeds the box width, it will overflow. Use 'overflow: hidden' and 'text-overflow: ellipsis' together to truncate with an ellipsis.
/* Text overflows without overflow handling */
.title { white-space: nowrap; }
/* Combine with overflow and text-overflow for ellipsis */
.title {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
HTML indentation appears in the rendered output when using pre
When 'white-space: pre' is specified, indentation and line breaks in the HTML source are rendered as-is. Indentation in the HTML source becomes part of the visible content.
/* With pre, the line breaks and spaces in HTML are displayed as-is */
.code-block { white-space: pre; }
Browser Compatibility
8 ○
7 ○
6 ○
3 and earlier ×
Android Browser
4.4+ ○
3 and earlier ×※ Version data is based on MDN.
If you find any errors or copyright issues, please contact us.