Is it OK to omit the html start tag in HTML5?

6

Should we omit the html start tag in an HTML5 document? Many developers ignore its use. Thank you!

<!DOCTYPE html>
   <head>
      <title>...</title>
   </head>
   <body>
      <p>Tag html foi omitida!</p>
   </body>
 </html>
    
asked by anonymous 26.03.2016 / 16:58

3 answers

5

The example you mentioned seems more like a flaw, it may even be the case of a dynamic page with some BUG and all the sites it encountered use the same technology and have the same BUG in validators).

Apparently as I said the BrunoBR you can omit yes the <html> tag, I did a test and also it is possible to omit html , head and body , I did the following test put it in Validate by Direct Input at link :

<!DOCTYPE html>
<title>Test</title>
<meta charset="utf-8">
<link type="text/css" rel="stylesheet" href="...">

<img src="..." alt="...">

And validated normally, ie to validate apparently it is allowed to "omit" several tags.

But it does not mean that because it validates that this is correct or that browsers will process well, tags omitted like <HTML></HTML> , <BODY></BODY> and <HEAD></HEAD> browsers when they load an HTML structure they try to reprocess them and thus end up generating the omitted tags, for example, write this:

<!DOCTYPE html>
<title>Test</title>
<meta charset="utf-8">
<link type="text/css" rel="..." href="...">
<img src="" alt="">

And Chrome reprocessed like this:

  

InternetExplorer11:

  

Firefox:

  

InallteststhebrowserenginesreprocessedtheHTML,butImeanyoucanevenomitandtrustthatallbrowserswillwork(aftertesting),butthedefaultisdesignedtoavoidheadachessuchasHTMLclogging,omittingatagcantriggeraprobleminanotherpartoftheHTML(forexampleifthepageisdynamic),eachengineas Webkit, Blink, Gecko, Trident, EdgeHTML, etc. can interpret variantly according to the content of the page and order of elements, it is best to try to stick to the basic / intermediate and "standardized", keep HTML without omitting tags and I'd prefer to validate as much as you can with your HTML.

In no way omitting tags will bring you any benefit, not even in consumption or code reduction.

There are tags that omit (actually do not have) the closing tag and omit /> by changing > (in HTML only), such as <link> , <meta> , and <img> need to be closed (in HTML):

<!DOCTYPE html>
<html lang="pt">
    <head>
        <title>Test</title>
        <meta charset="utf-8">
        <link type="text/css" rel="..." href="...">
    </head>
    <body>
        <img src="" alt="">
    </body>
</html>

But when we use XHTML (or XML + HTML, usually mimetype application/xhtml+xml ) we should use this:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="pt" xml:lang="pt">
    <head>
        <title>Test</title>
        <meta charset="utf-8" />
        <link type="text/css" rel="..." href="..." />
    </head>
    <body>
        <img src="" alt="" />
    </body>
</html>
  

Older browsers typically require application/xml as mimetype and the use of <?xml ... ?>

These element types do not need end tag , since they are tags of type void (empty element), ie they do not have "child" (child content with text or other tags ): link

  

HTML tag

26.03.2016 / 18:52
5

Interestingly enough, as you can see in the w3c specification > tag opening can be omitted as long as the first thing within the scope of the html tag is not a comment , so the example you used in the question can be considered correct, however the following would not be:

<!DOCTYPE html>
   <!-- Este HTML é inválido segundo a especificação oficial para HTML5 -->
   <head>
      <title>...</title>
   </head>
   <body>
      <p>Tag html foi omitida!</p>
   </body>
 </html>
The fact that it is valid does not mean that you should do it, theoretically it is ok, however you can not assume that all browsers implement the specification defined by w3c strictly (and in fact none of them implements, all have inconsistencies here and there), so by doing so you're running the risk that your HTML does not render correctly in any of them. In the end also unless you are doing the google front page the gain is null, you will save something like 20 ~ bytes of user bandwidth, this optimization is not worth even the time you spent writing the question;)

    
26.03.2016 / 20:10
2

Not that it is correct or incorrect, but it is valid and even recommended for the purpose of reducing the number of codes.

You can even omit the <body> tag.

But this is a controversial and confusing subject.

Until just before the advent of HTML5, for example, it was recommended to specify certain parameters:

<link rel="stylesheet" href="style.css"
  type="text/css">

Today, the recommendation is that we can omit the type attribute:

<link rel="stylesheet" href="style.css">

For this specific CSS example, it is understandable and makes sense, since the rel="stylesheet" attribute already defines what the document is, making the type"text/css" attribute redundant.

The confusion and controversy is due to the fact that until then the recommended one was always to specify the type attribute because in older versions of browsers could cause conflicts and misinterpretation.

A more specific example that caused conflicts:

Loading JavaScript file

<script src="script.js"></script>

Loading VBScript file

 <script src="script.vbs"></script>

This is a classic example that forced the recommendation of setting the type attribute. In addition to the type attribute, we also had to specify the language attribute:

<script language="VBScript" type="text/vbscript" src="script.vbs"></script>

<script language="JavaScript" type="text/javascript" src="script.js"></script>

Around 2004, recommendations began that it was no longer necessary to specify the language attribute, and to use the type attribute. So, since that time, gradually this attribute in the <script> tag has been forgotten. Today is very rare to find. Usually found on older systems.

Periodic changes in standards and recommendations

Standards and recommendations change periodically. Of course, keep the classic semantics.

For further reference on the subject, see this Google guide for current recommendations for HTML5. link

Just do not confuse as this is for HTML5 only. Avoid such practice for an environment where the user can access using older browsers.

Tolerance for semantic errors

The "comic" side of this is that despite being invalid in older browser versions, it is tolerable.

Even in older browsers, you can omit tags such as <html> and <head> because at that time, such browsers started to "tolerate" some errors in the semantics since many sites had errors considered tolerable, this was then implemented in browsers and persists to this day.

Document type definition

Note that this all also depends on the W3C // DTD setting of the page: link , which does not only affect HTML but also CSS and JavaScript.

    
26.03.2016 / 18:01