Alex Rivera | Logout

Why does HTML require that multiple spaces show up as a single space in the browser?

Asked 2009-01-11T19:38:00.507
64

I have long recognized that any set of whitespace in an HTML file will only be displayed as a single space. For instance, this:

<p>Hello.        Hello. Hello. Hello.                       Hello.</p>

displays as:

Hello. Hello. Hello. Hello. Hello.

This is perfectly fine, as if you need multiple spaces of pre-formatted text you can just use the <pre> tag. But what is the reason? More precisely, why is this in the specification for HTML?

Edit
Report

1 Answer

14

"Why are multiple spaces converted to single spaces?"

First, "why" questions are hard to answer. It's in the spec. That's pretty much the end of it.

Consider that there are several kinds of white space.

  • White space between tags. <p>\n<b>hi</b>\n</p>

  • White space in the content within a tag. <p>Hi <i>everyone</i>.</p>

  • White space in a <pre> or CDATA section.

The first two are hard to distinguish. Whitespace between tags, even in XML, is "optional". But when you have what is called a "mixed content model" -- tags intermixed with content -- the subtlety of "between tags" and "in the content but between tags" and "in the content but not between tags" is impossible to sort out.

So they don't sort it out. Whitespace between tags and whitespace in the content is all optional.

answered 2009-01-11T19:45:48.773

Your Answer