Is it necessary to write HEAD, BODY and HTML tags?

Omitting the html, head, and body tags is certainly allowed by the HTML specs. The underlying reason is that browsers have always sought to be consistent with existing web pages, and the very early versions of HTML didn’t define those elements. When HTML 2.0 first did, it was done in a way that the tags would be inferred when missing.

I often find it convenient to omit the tags when prototyping and especially when writing test cases as it helps keep the mark-up focused on the test in question. The inference process should create the elements in exactly the manner that you see in Firebug, and browsers are pretty consistent in doing that.

  • An html element’s start tag may be omitted if the first thing inside the html element is not a comment.
  • An html element’s end tag may be omitted if the html element is not immediately followed by a comment.
  • A head element’s start tag may be omitted if the element is empty, or if the first thing inside the head element is an element.
  • A head element’s end tag may be omitted if the head element is not immediately followed by a space character or a comment.
  • A body element’s start tag may be omitted if the element is empty, or if the first thing inside the body element is not a space character or a comment, except if the first thing inside the body element is a script or style element.
  • A body element’s end tag may be omitted if the body element is not immediately followed by a comment.