1

I know in HTML5 it is not neccessary to write <html> tag. This is discussed in this question, regarding if it is it necessary to write head body and html tags.

But should I skip it? Is it recommended to skip? The reason I ask this is I am seeing lots of HTML5 tutorials/templates on the internet that does this in their code:

<!DOCTYPE html>
<!--[if IE 9 ]><html class="ie9"><![endif]-->
<head>
.....

Looks superfluous and completely unnecessary to me, or is it?

Community
  • 1
  • 1
Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154
  • 2
    That particular snippet isn't superfluous. It contains a conditional comment for IE9 to insert an `ie9` class into the html element. You can't really omit a start tag if you want to declare any attributes on the element. What *is* superfluous is the start tag for head... – BoltClock Apr 22 '15 at 05:01
  • http://stackoverflow.com/questions/9797046/whats-a-valid-html5-document . It has been discussed here as well. Take a look. – Sandeep Nayak Apr 22 '15 at 05:01
  • @BoltClock, so assuming I am not supporting IE9, should I still skip `` tag? Because the tutorials/templates I am seeing are skipping them... so I am curious, is it best practice to do so... – Rosdi Kasim Apr 22 '15 at 05:53

2 Answers2

3

The question linked in the comments answers the question if you can, and that is you can omit in the opening <html> tag. However...

You shouldn't.

As the answers in this question and the question linked above show, the rules around when you can or cannot omit certain tags are long and lawyer-esque.

Written code isn't just for machines its for people too. This isn't to suggest that HTML should be human-readable without a browser, that's farcical, but it should be programmer-readable and there is a difference.

This is perfectly valid HTML:

<!DOCTYPE HTML>
<title>Hello</title>
<p>Welcome to this example.</p>

However, without consulting the spec I am not immediately sure where each element will be in the computed DOM, for the curious its equivalent to:

<!DOCTYPE HTML>
<html><head><title>Hello</title>
</head><body><p>Welcome to this example.</p></body></html>

However, if time is at a premium (and programmer time is almost always more expensive than computing time nowadays) then knowing quite clearly what a document is doing is vital.


No doubt someone will ask, what about when space or transmission speed is at a premium. In those cases thorough profiling to determine the exact bottleneck is important, and on the fly compression, good caching of HTML, or even automated removal of elements using a library will all be vastly superior options to removing tags in source code that is to be written and interpreted by people.

Community
  • 1
  • 1
0

<html> is required for a valid HTML document only if the tag is preceded by a comment.

An html element's start tag may be omitted if the first thing inside the html element is not a comment.

See the W3C specification for details.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
grill
  • 1,160
  • 1
  • 11
  • 24
  • Are you sure you're not thinking of XHTML instead? – BoltClock Apr 22 '15 at 05:05
  • Pretty sure the linked document says HTML – grill Apr 22 '15 at 05:07
  • 3
    The correct link is [here](http://www.w3.org/html/wg/drafts/html/master/single-page.html#optional-tags) (or [here](http://drafts.htmlwg.org/html/master/syntax.html#optional-tags) for the less bandwidth-inclined) and it states pretty clearly that the tags can be omitted, and furthermore that omitting the tags doesn't mean the element isn't present, particularly for the html element. – BoltClock Apr 22 '15 at 05:09
  • That looks better. (Well, absent the hashtag anyway.) – BoltClock Apr 22 '15 at 05:16