Full answer
In normal HTML text, the browser collapses white space. Several spaces, tabs and line breaks in the source become a single space on screen. Spaces at the start and end of a line are removed. This lets you format your HTML source freely.
The white-space property changes the rules:
normal: collapse spaces and wrap lines. This is the default.nowrap: collapse spaces, but never wrap. The text can overflow its box. Use it for short labels and withtext-overflow: ellipsis.pre: keep spaces and line breaks, and do not wrap. This is what thepreelement uses, and it suits code.pre-wrap: keep spaces and line breaks, but wrap long lines. It suits text from a textarea.pre-line: collapse spaces, keep line breaks, and wrap. It suits text with simple line breaks.break-spaces: likepre-wrap, but trailing spaces take up space and can wrap.
Other points:
- Inline elements next to each other with a space or line break between them show a small gap on screen, because that space is real. If the tags touch with no space, there is no gap. Flexbox and Grid ignore this space between items.
- For a space that must not collapse and must not break, use
, for example between a number and its unit, "10 km". Use CSS spacing for layout, and not repeated . - A
brelement forces a line break. Use it in addresses and poems, and not to add vertical space. Use margins for that. innerTextshows the text as rendered, andtextContentreturns the raw source text.
The first paragraph reads as One two three. The pre element keeps its spaces. The third paragraph keeps its line break, so it is two lines high. The nowrap paragraph overflows its box. The two spaces in the last paragraph stay, because they are non-breaking. The spans with a space between them have a gap, and the spans without one touch.
When the page shows the wrong spacing, check the white-space rule before changing the layout.
<style>
.t { line-height: 20px; width: 200px; margin: 0; }
.nw { white-space: nowrap; width: 100px; overflow: hidden; }
</style>
<p class="t" id="a">One two
three</p>
<pre class="t" id="b"> keep this</pre>
<p class="t" id="c" style="white-space: pre-line">line1
line2</p>
<p class="t nw" id="d">A very long label that does not wrap</p>
<p class="t" id="e">x y</p>
<span id="s1">a</span> <span id="s2">b</span><br>
<span id="s3">a</span><span id="s4">b</span>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Spaces and line breaks collapse into one space by default.
- nowrap stops wrapping; pre keeps everything.
- pre-wrap and pre-line keep user line breaks and wrap.
- does not collapse or break.
- A space between inline elements is a real gap.
Common mistakes
- Using many entities to indent text.
- Using br tags for vertical spacing.
- Wondering about gaps between inline-block elements without remembering the space in the source.
Follow-ups
- Why is there a gap between two inline-block buttons?
- What is the difference between pre-wrap and pre-line?
Tips
- Say what collapsing means before listing the property values.
- Mention pre-wrap and pre-line for user text.