PDA

View Full Version : Optional Closing Tags in HTML


Bodyag
03-10-2007, 07:43 PM
For as long as I've been validating web pages, I never realized that the closing </head> tag was optional in HTML. I feel as if I've missed the boat at times. ;)

While I'm all for trimming excess code, I just cannot for the life of me strip out closing tags because they are optional in HTML. I'd be concerned that something, somewhere would not parse the document correctly if I removed the closing </head> element.

Are my concerns unfounded?

jlhaslip
03-10-2007, 07:47 PM
There are lots of tags where the closing tags are optional, but if you actually go to the trouble of closing them, there is not an issue about whether or where they close and fewer surprises...

And I don't think the few bits/bytes saved are critical. :D

mrdantownsend
03-10-2007, 08:57 PM
is this allowed in XHTML :confused:

felgall
03-10-2007, 09:06 PM
XHTML has the closing tags which should be there as mandatory.

The HTML standard tells the browser writers that they have to allow for stupid people leaving them out in their page and so defines where to add them in that instance. Any sensible coder wont leave them out because it is just making more work for themselves in the future in maintaining the page. Also since the browser has to figure out where to insert the missing tags in order to generate the page leaving them out will not have any significant affect on dowload time for anyone using a 14.4k dialup modem or faster connection.

mrdantownsend
03-10-2007, 09:15 PM
yeah, that's what I thought

it sounded kind of stupid to me.

Arbitrator
03-11-2007, 02:45 PM
The following tags have implied start and end tags if left out in HTML: <html>, </html>, <head>, </head>, <body>, </body>, <tbody>, </tbody>. The first three sets will always be inserted while the last will be inserted for any rows are not already children of thead, tfoot, or tbody. The following are implied end tags in HTML and can technically be omitted for their respective elements: </p>, </li>, </colgroup>, </dd>, </dt>, </thead>, </tfoot>, </td>, </th>, </tr>. I wouldn’t recommend omission of any of these tags, however, except perhaps for the </colgroup> tag in certain cases.

There are no such things as implied tags in XHTML. If you omit an implied tag in XHTML, such as tbody, it will simply not be there. If you omit ending tags elsewhere, such as for a p element, then a fatal error is generated.

The following document is valid HTML 4.01 Strict:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<title>HTML 4.01 Strict Document</title>

<div>This document is valid.</div>

Want to get even shorter? This also correct HTML, although modern browsers will choke on it:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<title/HTML 4.01 Strict Document/
<div/This document is valid./

The second example uses something called NET (null end tag) syntax. You can put them through the W3C Markup Validator (http://validator.w3.org/check) if you’re curious.