Are leading and trailing whitespaces ignored in HTML?

8k Views Asked by At

html4 says this:

In order to avoid problems with SGML line break rules and inconsistencies among extant implementations, authors should not rely on user agents to render white space immediately after a start tag or immediately before an end tag. Thus, authors, and in particular authoring tools, should write:

<P>We offer free <A>technical support</A> for subscribers.</P>

and not:

<P>We offer free<A> technical support </A>for subscribers.</P>

and this:

SGML (see [ISO8879], section 7.6.1) specifies that a line break immediately following a start tag must be ignored, as must a line break immediately before an end tag. This applies to all HTML elements without exception.

The following two HTML examples must be rendered identically:

<P>Thomas is watching TV.</P>

<P>
Thomas is watching TV.
</P>

So must the following two examples:

<A>My favorite Website</A>

<A>
My favorite Website
</A>

So, one shouldn't rely on them being ignored or not. What about html5?

UPD Or let us put it this way: can I treat them as being ignored or sometimes they matter (manifest themselves in one way or another)? In which ways, if any?

UPD Um, should I have said I had refactoring in mind...? I'm trying to make templates a little more readable, that's what made me think about it.

2

There are 2 best solutions below

2
On BEST ANSWER

Spaces are definitely not ignored in inline tags (i.e. <a>, <span>, <strong>, ...), for instance in this example,

<p>We offer free <a>technical support</a> for subscribers.</p>
<p>We offer free<a> technical support </a>for subscribers.</p>

if you set the CSS to something like this

a { text-decoration: underline; }

you can definitely see the difference.

Sometimes line breaks can produce weird results in inline tags, for example if you write the code like this,

<p>We offer free <a>
technical
support
</a> for subscribers.</p>

it seems to ignore the first line break, but not the last.

Here's a fiddle for both examples: http://jsfiddle.net/Niffler/fnnanru2/

Within block tags (i.e. <p>, <h1>, <div>, ...) spaces as well as line breaks at the beginning or end of the tags should always be ignored (i.e. <p>test</p> should look the same as <p> test </p>).

And as another user wrote in a comment, a line break will generally render the same as a space.

Also, multiple spaces or line breaks or combinations thereof generally get summarized to one space.

1
On

SHOULD spaces be ignored by browsers, and ARE spaces ignored by browsers are two totally different questions.

You should not put spaces after the opening tag, even though it (sometimes) works. Because of the documentation on this, the browsers could change to obey that rule without notice.

They shouldn't be rendered the same way, but currently most browsers render

<P>We offer free<a> technical support </a>for subscribers.</P> and

<P>We offer free <a>technical support</a> for subscribers.</P>

the same.

Remember: all of this could change without notice, so I would definitely follow the documentation's rules.