Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the drawbacks of ignoring <html> and <body>

Tags:

html

Is there any drawback to never using

<html> and <body>

on your web pages that are written in HTML and PHP? I mean, everything works perfectly fine with it or without it, so why use it?

like image 490
Saul Tigh Avatar asked Apr 20 '16 09:04

Saul Tigh


People also ask

Should I use XHTML or HTML?

XHTML was developed to make HTML more extensible and flexible to work with other data formats (such as XML). In addition, browsers ignore errors in HTML pages, and try to display the website even if it has some errors in the markup. So XHTML comes with a much stricter error handling.

What is the root tag of an HTML document ?*?

The <html> HTML element represents the root (top-level element) of an HTML document, so it is also referred to as the root element. All other elements must be descendants of this element. None. One <head> element, followed by one <body> element.


2 Answers

They are explicitly optional in the spec (so the document will still be valid).

This has been true since the original spec (which says <!ELEMENT HTML O O (( HEAD | BODY | %oldstyle)*, PLAINTEXT?)>, O O meaning Start Tag Optional, End Tag Optional) through to the current spec (which says "An html element's start tag can be omitted if the first thing inside the html element is not a comment. An html element's end tag can be omitted if the html element is not immediately followed by a comment.").

They are only mandatory in XHTML since XML has no concept of optional tags.

I've never seen any browser or user-agent fail to handle them correctly in an HTML document. (Note that while the tags are optional, the elements are not, so browsers will insert an HTML, HEAD and BODY elements even if the tags are missing, so any script which tries to find them in the DOM will still work).

The only technical drawback is that you can't put attributes on tags which aren't there, and a lang attribute for the HTML element is useful.

Leaving them out can confuse other people who have to maintain your code who don't know that the tags are optional though.

like image 61
Quentin Avatar answered Oct 12 '22 05:10

Quentin


Both <head> and <body> tags are optional in HTML5. In fact it is recommended by Google's HTML style guide to not use them:

<!-- Not recommended -->
<!DOCTYPE html>
<html>
  <head>
    <title>Spending money, spending bytes</title>
  </head>
  <body>
    <p>Sic.</p>
  </body>
</html>

<!-- Recommended -->
<!DOCTYPE html>
<title>Saving money, saving bytes</title>
<p>Qed.

By not using those tags, some drawbacks include:

  • that it is drastically different from what is typically learned for developers, so it may cause some confusion.
  • a restriction that a comment cannot be immediately after the <html> tag that is omitted.

Reiterating the optional nature of the tags from the spec:

An html element's start tag may be omitted if the first thing inside the html element is not 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 meta, link, script, style, or template element.

See:

  • https://google.github.io/styleguide/htmlcssguide.xml?showone=Optional_Tags#Optional_Tags
  • https://html.spec.whatwg.org/multipage/syntax.html#syntax-tag-omission
like image 3
timolawl Avatar answered Oct 12 '22 05:10

timolawl