Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of the <html> element?

Tags:

html

Doesn't the file type already let the browser know that the document is an html document. MDN mentions that it is the root element, so is using it just a formality?

like image 857
Nikhil S Avatar asked Jun 21 '18 17:06

Nikhil S


2 Answers

It is a family trait of HTML, XML, and SGML that they all need to be nested inside a root element. It's just part of the data standard and lets the interpreter know where to start and stop, and verifies that the document is complete and well-formed.

<!DOCTYPE html> specifies the type of document. In that case it means that it is HTML 5 currently, as opposed to XML or XHTML 1.0 transitional, as examples. Keep in mind that if you are downloading these as byte streams you may not always know the file type.

like image 196
greendemiurge Avatar answered Oct 27 '22 22:10

greendemiurge


Yes. The <html> tag is the root and can even be omitted in somes cases (from MDN):

The start tag may be omitted if the first thing inside the <html> element is not a comment. The end tag may be omitted if the <html> element is not immediately followed by a comment, and it contains a <body> element either that is not empty or whose start tag is present.

But not only:

  • It can be styled with CSS (though styling the <body> will usually be enough).
  • It can have global attributes, especially lang, which is the W3C way of defining an HTML document language.

There is probably more to say but that’s what I see as arguments for the <html> element, apart from its main role of being the root element for an HTML document.

like image 42
Bertrand Avatar answered Oct 27 '22 23:10

Bertrand