HTML Whitespace
Here is a puzzle that confuses almost every beginner. You type a paragraph in your HTML file, press Enter a few times to add some breathing room, add a bunch of extra spaces for good measure, save, refresh... and the browser shows everything squashed together as if you had done nothing at all.
Is the browser broken? No. It is following a rule called whitespace collapsing, and once you understand it, you will know exactly how much control you have over spacing, and when you need a different tool.
Whitespace is any invisible character that creates space in your code: the space bar, the Tab key, and line breaks (pressing Enter). In HTML, browsers treat almost all of these very differently from how a word processor would.
The Core Rule: Whitespace Collapsingβ
When a browser reads your HTML, it takes every run of whitespace characters, however long, and collapses it into a single space. Tabs and line breaks are treated the same way.
Consider this code:
<p>Hello World</p>
<p>Hello
World</p>
<p>Hello
World</p>
All three paragraphs contain very different amounts of whitespace between the two words, but they all render identically:
Hello World
Hello World
Hello World
Each one shows simply: Hello World.
Why does HTML work this way?β
It is actually a feature, not a bug. Because whitespace collapses, you are free to format your source code however you like, with indentation and line breaks, so it is easy for humans to read, without changing how the page looks.
If browsers preserved every space, you would have to write all your HTML on one long line just to avoid accidental gaps.
What Else Gets Trimmed?β
Collapsing is only part of the story. Browsers also ignore whitespace in a few other situations.
| Situation | What happens |
|---|---|
| Multiple spaces or tabs in a row | Collapsed into one space |
| Line breaks inside text | Treated as a single space |
| Spaces at the start of a block element | Removed |
| Spaces at the end of a block element | Removed |
| Whitespace between block elements | Ignored entirely |
That is why indenting your code with tabs or spaces never causes visible gaps at the beginning of your paragraphs.
- With indentation
- No indentation
<div>
<p>
Indented paragraph
</p>
</div>
<div><p>Indented paragraph</p></div>
Both versions display the same thing. Use indentation for readability, and trust the browser to ignore it.
Taking Control: How to Add Space on Purposeβ
If the browser collapses your whitespace, how do you intentionally add space or line breaks? HTML gives you dedicated tools for each situation.
- π New paragraph
- β©οΈ Line break
- β΅ Non-breaking space
- β Horizontal rule
Use a separate <p> element to start a new paragraph. Browsers automatically add vertical space between paragraphs.
<p>This is the first paragraph.</p>
<p>This is the second paragraph.</p>
This is the first paragraph.
This is the second paragraph.
Use <br /> to force a line break inside the same paragraph, without starting a new one. This is ideal for addresses and poetry.
<p>
CodeHarborHub<br />
Mandsaur, Madhya Pradesh<br />
India
</p>
CodeHarborHub
Mandsaur, Madhya Pradesh
India
Use the entity to insert a space that the browser will not collapse and will not break across lines.
<p>Hello World</p>
HelloΒ Β Β Β World
The four characters survive, so you can see a visibly wider gap. You will learn more about entities like this in the Entities lesson.
Use <hr /> to draw a dividing line between sections of content.
<p>Section one</p>
<hr />
<p>Section two</p>
<br /> and for layoutIt is tempting to stack many <br /> tags or dozens of characters to push content around. This is fragile, hurts accessibility, and breaks on different screen sizes. For spacing and layout, the correct tool is CSS (margins, padding, and gaps), which you will learn in a later course.
Preserving Whitespace with <pre>β
Sometimes whitespace really matters: source code, ASCII art, poems, or text tables. For these situations, HTML provides the <pre> (preformatted text) element. Inside it, the browser keeps every space and line break exactly as you typed it, and uses a monospace font.
<pre>
/\_/\
( o.o )
> ^ <
</pre>
/\_/\ ( o.o ) > ^ <
Here is a side-by-side comparison of the same text with and without <pre>:
- Inside a normal <p>
- Inside a <pre>
<p>
Name: Ajay
Age: 25
City: Mandsaur
</p>
Everything collapses onto one line: Name: Ajay Age: 25 City: Mandsaur
<pre>
Name: Ajay
Age: 25
City: Mandsaur
</pre>
Every space and line break is preserved, so the columns stay aligned.
<pre> with <code>When displaying source code on a web page, wrap it like this: <pre><code>...your code...</code></pre>. The <pre> keeps the spacing, and the <code> element tells browsers and screen readers that the content is code.
Whitespace Control with CSSβ
You can also change how whitespace behaves using the CSS white-space property, without changing your HTML at all. Here are the most useful values:
| Value | Collapses spaces? | Keeps line breaks? | Wraps long lines? |
|---|---|---|---|
normal (default) | Yes | No | Yes |
nowrap | Yes | No | No |
pre | No | Yes | No |
pre-wrap | No | Yes | Yes |
pre-line | Yes | Yes | Yes |
.poem {
white-space: pre-line; /* keep line breaks, collapse extra spaces */
}
.no-wrap-title {
white-space: nowrap; /* never wrap onto a second line */
}
You do not need to memorize this table now. Just remember that whitespace behavior can be changed with CSS. You will practice it properly once you reach the CSS module.