Sobes.tech
Junior

What is doctype and what is its role in an HTML document?

sobes.tech AI

Answer from AI

DOCTYPE (Declaration Type) is an instruction that tells the browser which version of HTML or XML the document uses.

Its roles:

  • Standard Definition: Indicates that the document conforms to a specific specification (e.g., HTML5, HTML 4.01, XHTML 1.0 Strict).
  • Enabling Standard Mode: Helps the browser switch to "standard mode," where it renders the page according to web standards like CSS and JavaScript. Without DOCTYPE or with incorrect usage, the browser may switch to "quirks mode," where rendering can differ and lead to unpredictable behavior.
  • Validation: Used by validation tools to check the document's compliance with the declared standard.

For HTML5, the DOCTYPE looks like:

<!DOCTYPE html>

This is the most common and recommended DOCTYPE today. It is simple and ensures that the browser enters standard mode.

In older versions of HTML and XHTML, DOCTYPE was more complex and included a URI to the DTD (Document Type Definition), for example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

However, for HTML5, a shorter form is sufficient. The DOCTYPE must be the first element in the HTML document, before the root <html> element.