tags:

views:

90

answers:

5

I just read this article from Google. I always thought that closing tags is important to keep html documents clean and make them machine-readable. But they recommend the opposite.

What do you think about this?

+3  A: 

It depends entirely on your doctype. If you use HTML 4 transitional, then yes, you're fine leaving off end tags. If you use XHTML or HTML 4 strict, then no, close your tags.

Randolpho
I haven't looked, but does HTML5 require endtags?
James Black
HTML5 doesn't change the situation; the HTML-style serialisation can omit tags, the XHTML5 serialisation can't.
bobince
A: 

As long as you are conforming to the DTD, I agree with trying to minimize the clutter. Personally, I've never seen anyone use a

</br>

Parsers will (should) parse according to the DTD, so it will still be "machine readable".

Jeff Storey
Indeed, people use the shorten version `<br/>`
Pascal Thivent
I should clarify. I've never seen anyone do something like <br></br>, usually I just see it as <br>.
Jeff Storey
It's useful to note that in html it is **forbidden** to close a `BR` element. As opposed to other elements (e.g. `P`) where the closing tag is optional.
Ian Boyd
+3  A: 

Google are in a somewhat unusual situation in that they serve so very many copies of their main search pages that any tiny saving in page size soon add up. This means for them it's economical to make their pages smaller by removing close tags and whitespace, at the cost of making them harder to maintain.

You aren't Google; it's unlikely to pay off for you. You'll notice the Google page you link to itself doesn't omit the tags it recommends, because it's not one of the critical pages that Google serves a lot of. Compare that to the extreme minification of www.google.com front page.

Use gzip/deflate compression and you'll already get mostly minimal transfer size; reducing markup further is a desperate measure which unless you're serving loads will be a premature optimisation.

bobince
A: 

That article suggests omitting the end tag of a tag pair that is better represented as a singleton to begin with. Since the tags it references, with the strike through, should be used as singletons, and thus not have an end tag, this is neither a best practice recommendation or erroneous. I would suggest completely disregarding this article since it is attempting to recommend a better method than a practice that is completely flawed to begin with.

Honestly, I don't suggest you take any advise about markup from Google as they have chosen to completely disregard any standard if they can reduce the number of characters served to the end user. Attempt to validate any of their code and see for yourself that they are not any example by which markup best practices should be addressed.

+2  A: 

The HTML spec allows you to omit certain end tags. In general these are tags that cannot self-nest and therefore the closing tag is never ambiguous. Take this example:

<p>A paragraph of text.
<p>Another paragraph of text.</p>

<div>A divider element...
<div>Another divider element...</div>

In the first example, when you get to the second <p>, this means the previous paragraph has finished, because you cannot nest paragraph tags. The browser interprets that as:

<p>A paragraph of text.</p>
<p>Another paragraph of text.</p>

In the second example, however the second <div> creates a nested div element, and the </div> closes the inner element. You would need to add another </div> wherever you wanted the outer one to finish.

However, having said all this, it will probably be easier to close all tags as you go to avoid any problems. It depends how well you will remember the whitelist. If you forget, for example that divs must always be closed, you will mess up your layout no end.

And as bobince said, with gzip it's kind of irrelevant. If you have 30 paragraphs, the difference between the versions with and without closing tags would be 120 bytes. However, if both are gzipped, the difference is going to be tiny - less than 10 bytes. I suggest writing HTML however you are comfortable.

DisgruntledGoat