Search code examples
htmlformattingwhitespace

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


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?


Solution

  • Spaces are compacted in HTML because there's a distinction between how HTML is formatted and how it should be rendered. Consider a page like this:

    <html>
        <body>
            <a href="mylink">A link</a>
        </body>
    </html>
    

    If the HTML was indented using spaces for example, the link would be preceded by several spaces.