Forum Moderators: open

Message Too Old, No Replies

Optional Closing Tags in HTML

</head> is optional in HTML

         

pageoneresults

4:26 pm on Jan 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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?

mattur

5:12 pm on Jan 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's far more alarming than you realise! ;)

The start and end tags for HTML, HEAD and BODY are all optional [w3.org] in HTML4.01. It's perfectly valid to leave them all out.

I'd be concerned that something, somewhere would not parse the document correctly if I removed the closing </head> element.

I wouldn't omit them either. Just because a page validates does not guarantee it will work in all browsers. (Pigeons <- Cat -> Pigeons) :)

Google "Anne's Weblog" for an example of minimalist, valid HTML4.01 strict pages in action.

Little_G

5:45 pm on Jan 29, 2007 (gmt 0)

10+ Year Member



Hi,

I'm not sure if the specification should be interpreted in this way. The DTD specifies, as an example that the TITLE tag requires a start and a close tag, whereas the P tag requires a start tag and the close is optional. In the case of HEAD the DTD contains:

<!ELEMENT HEAD O O (%head.content;) +(%head.misc;) -- document head -->
Which makes the start tag optional as well, therefor I would assume it is implied that the HEAD block is not required as a whole, but, if it is started it must be finished.
If you submit to the W3C validator a document with no closing tags omitted you get errors like this:
end tag for "HEAD" which is not finished.
"BODY" not finished but document ended.

Andrew

Robin_reala

6:19 pm on Jan 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't think it works like that. Something to bear in mind is that an element isn't the same as its tags. For example, if you leave out the </head> closing tag and put in a <body> start tag the browser knows what to do: a head element can never contain a body element so it doesn't need a specific instruction to end the head element.

Similarly, say directly after the DOCTYPE the browser finds a <title> start tag. The browser knows that a title element can never exist outside of a head element so it knows that it can generate a head parent to go around it.

Now combine the two above scenarios together. From a total absence of start and end <head> tags we can still infer the presence of a head element. This is why it's legitimate HTML.

Of course this only applies to HTML, not XHTML.

piatkow

3:21 pm on Jan 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For my own sanity (I do a lot of hand coding) I would always close head and body tags in a pukka web page.

On the other hand at work we send a lot of database reports to the browser and the html we create there normally has no head or body tag, just a table with the results of the query. Works just fine and a lot easier to code as it is independent of the target application. Users can open the html files in spreadsheets or word processors if they need to.