CSS / Reference / white-space: Preserve Spaces, Line Breaks, and Wrapping
CSS white-space controls whether repeated spaces and source line breaks survive and whether text wraps automatically. Use normal for ordinary text and pre-wrap for logs or messages that should keep indentation and line breaks. Use pre-line when the original breaks matter but repeated spaces can collapse.
Choose how to handle spaces and wrapping
The initial value is normal, and the property inherits. A source line break here is a newline in the HTML text. An explicit <br> still starts a new line with nowrap. The CSS white-space specification defines the values.
| Value | Runs of spaces and tabs | Source line breaks | Automatic wrapping |
|---|---|---|---|
normal |
Collapse | Collapse | Allowed |
nowrap |
Collapse | Collapse | Disabled |
pre |
Preserve | Preserve | Disabled |
pre-wrap |
Preserve | Preserve | Allowed |
pre-line |
Collapse | Preserve | Allowed |
break-spaces |
Preserve and take up space at line ends | Preserve | Allowed after each preserved space too |
.copy { white-space: normal; }
.log { white-space: pre-wrap; }
.code-line { white-space: pre; overflow-x: auto; }
For a short button label that should stay on one line, apply nowrap to that label and check that it fits on a narrow screen. When code should keep its original line length, allow the code region to scroll horizontally as in .code-line above.
Compare six values on the same text
Save the complete document as white-space-example.html in UTF-8 and open it in a browser. Each box in the first section starts with four spaces between A and B, a newline after B, and a tab between C and D. All three sections run without an external stylesheet or JavaScript.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare CSS white-space values</title>
<style>
body { margin: 1rem; color: #20242b; background: #fff;
font: 1rem/1.55 system-ui, sans-serif; }
main { max-width: 58rem; margin-inline: auto; }
.samples { display: grid; gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 15rem), 1fr)); }
figure { margin: 0; min-width: 0; }
figcaption { margin-bottom: .35rem; overflow-wrap: anywhere; }
.sample { width: 22ch; max-width: 100%; overflow: auto;
font: 1rem/24px ui-monospace, monospace; tab-size: 4;
background: #edf4f8; outline: 1px solid #60727b; }
.sample:focus-visible { outline: 3px solid #005fcc; }
.narrow { width: 4ch; }
.anywhere { overflow-wrap: anywhere; }
.normal { white-space: normal; }
.nowrap { white-space: nowrap; }
.pre { white-space: pre; }
.pre-wrap { white-space: pre-wrap; }
.pre-line { white-space: pre-line; }
.break-spaces { white-space: break-spaces; }
</style>
</head>
<body>
<main>
<h1>Compare CSS white-space values</h1>
<h2>Six values with identical source text</h2>
<div class="samples">
<figure>
<figcaption id="mode-normal-label"><code>normal</code></figcaption>
<div id="mode-normal" class="sample normal">A B
C D E F G H I J K L M N O P Q</div>
</figure>
<figure>
<figcaption id="mode-nowrap-label"><code>nowrap</code></figcaption>
<div id="mode-nowrap" class="sample nowrap" tabindex="0" role="region" aria-labelledby="mode-nowrap-label">A B
C D E F G H I J K L M N O P Q</div>
</figure>
<figure>
<figcaption id="mode-pre-label"><code>pre</code></figcaption>
<div id="mode-pre" class="sample pre" tabindex="0" role="region" aria-labelledby="mode-pre-label">A B
C D E F G H I J K L M N O P Q</div>
</figure>
<figure>
<figcaption id="mode-pre-wrap-label"><code>pre-wrap</code></figcaption>
<div id="mode-pre-wrap" class="sample pre-wrap">A B
C D E F G H I J K L M N O P Q</div>
</figure>
<figure>
<figcaption id="mode-pre-line-label"><code>pre-line</code></figcaption>
<div id="mode-pre-line" class="sample pre-line">A B
C D E F G H I J K L M N O P Q</div>
</figure>
<figure>
<figcaption id="mode-break-spaces-label"><code>break-spaces</code></figcaption>
<div id="mode-break-spaces" class="sample break-spaces">A B
C D E F G H I J K L M N O P Q</div>
</figure>
</div>
<h2>Six trailing spaces</h2>
<div class="samples">
<figure>
<figcaption id="end-pre-wrap-label"><code>pre-wrap</code></figcaption>
<div id="end-pre-wrap" class="sample narrow pre-wrap">AB      </div>
</figure>
<figure>
<figcaption id="end-break-spaces-label"><code>break-spaces</code></figcaption>
<div id="end-break-spaces" class="sample narrow break-spaces">AB      </div>
</figure>
</div>
<h2>A long string without spaces</h2>
<div class="samples">
<figure>
<figcaption id="token-0-label"><code>overflow-wrap: normal</code></figcaption>
<div id="token-0" class="sample normal" tabindex="0" role="region" aria-labelledby="token-0-label">ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</div>
</figure>
<figure>
<figcaption id="token-1-label"><code>overflow-wrap: anywhere</code></figcaption>
<div id="token-1" class="sample normal anywhere">ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</div>
</figure>
<figure>
<figcaption id="token-2-label"><code>nowrap + anywhere</code></figcaption>
<div id="token-2" class="sample nowrap anywhere" tabindex="0" role="region" aria-labelledby="token-2-label">ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</div>
</figure>
</div>
</main>
</body>
</html>
With normal, the gaps shrink and the source newline no longer forces C onto another line. nowrap also disables automatic wrapping. pre-line keeps the break before C but collapses the spaces and tab. Both pre and pre-wrap keep the spacing and source breaks. Only pre-wrap wraps the long line to fit.
See where pre-wrap and break-spaces differ
With pre-wrap, preserved spaces at the end of a line can hang beyond the available width. With break-spaces, those spaces still take up room and there is a wrap opportunity after each one. See the CSS white-space processing rules for the exact behavior.
The second section uses a box about four characters wide and puts six spaces after AB. Each   is an HTML character reference for one space. The pre-wrap box stays one line high. The trailing spaces make the break-spaces box two lines high. Remove one or two spaces at a time to see when the line count changes, then put them back.
| Value | Lines | Preview |
|---|---|---|
pre-wrap |
1 | |
break-spaces |
2 |
Handle a string that overflows its container
Allowing wrapping still leaves long identifiers with nowhere to break. In the third section, overflow-wrap: normal keeps the long string on one line and anywhere lets it break inside the string as needed. Adding anywhere to nowrap does not re-enable wrapping. word-wrap is the older name for overflow-wrap. See word-break for rules that change ordinary line breaks inside words.
.message {
white-space: pre-wrap;
overflow-wrap: anywhere;
min-width: 0;
}
Apply the .message rule above to the element that contains the text. If a flex or grid item refuses to shrink, set min-width: 0 on that item. The guide to overflow and long text covers box sizing and scroll behavior, and word-wrap explains the older property name.
Inspect the source text and computed styles
In developer tools, check that the text node still contains spaces and newlines. This property affects rendering, so it cannot recover whitespace an editor removed. Next, check the computed white-space and overflow-wrap values and look for nowrap inherited from a parent.
Where the two-keyword syntax is supported, white-space: preserve wrap is equivalent to pre-wrap. preserve is the white-space-collapse value and wrap is the text-wrap-mode value. The single-keyword form is a good starting point when you support older browsers.




