Handling of non breaking space: <p>&nbsp;</p> vs. <p> </p>

grabner picture grabner · Sep 5, 2012 · Viewed 482.8k times · Source

&nbsp; is a non breaking space, which represents an empty space where no line break occurs.

If I use

<p>&nbsp;</p>

I have a space between two passages (bigger break). If I use

<p> </p>

I only have a new line between the two passage (no break). Why?

Answer

BoltClock picture BoltClock · Sep 5, 2012

In HTML, elements containing nothing but normal whitespace characters are considered empty. A paragraph that contains just a normal space character will have zero height. A non-breaking space is a special kind of whitespace character that isn't considered to be insignificant, so it can be used as content for a non-empty paragraph.

Even if you consider CSS margins on paragraphs, since an "empty" paragraph has zero height, its vertical margins will collapse. This causes it to have no height and no margins, making it appear as if it were never there at all.