← Back to HTML & CSS, page 10

html-css-0191 · Easy fresher

How does the browser handle spaces and line breaks in HTML, and what does the white-space property change?

HTML collapses runs of spaces and line breaks into one space. The white-space property changes this: nowrap stops wrapping, pre keeps everything, pre-wrap keeps spaces and wraps, and pre-line keeps only line breaks.   makes a non-breaking space that does not collapse.

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 with text-overflow: ellipsis.
  • pre: keep spaces and line breaks, and do not wrap. This is what the pre element 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: like pre-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 br element forces a line break. Use it in addresses and poems, and not to add vertical space. Use margins for that.
  • innerText shows the text as rendered, and textContent returns 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.

html
<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&nbsp;&nbsp;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.
  • &nbsp; does not collapse or break.
  • A space between inline elements is a real gap.

Common mistakes

  • Using many &nbsp; 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.