HTML <!doctype> Tag
The HTML <!doctype> tag is used for specifying which language and version the document is using. This is referred to as the document type declaration (DTD).
Technically speaking, the <!doctype> tag does not represent an HTML element (there is no <!doctype> element). However, it has been included here, as it is a required preamble to all HTML documents.
Also, it is usually referred to as the "doctype declaration" as opposed to the "doctype tag". Having said that, "tag" is a pretty loose term that tends to be applied quite generically across the web, so I've included it in the list of "HTML tags".
Syntax
The <!doctype> declaration is written as <!doctype html> (no end tag). In modern HTML5, this short syntax is all that is required. The <!doctype> declaration is case-insensitive (e.g. <!DOCTYPE html> or <!doctype html>).
The <!doctype> declaration must be placed at the very beginning of the HTML document, before the <html> root tag.
Like this:
Purpose & Best Practices
- Standards Mode: Including
<!doctype html>ensures browsers render the page in full standards mode according to modern web specifications, rather than legacy "quirks mode". - Accessibility & Language: Always follow the doctype declaration with an
<html>element that includes a validlangattribute (e.g.,<html lang="en">) to assist screen readers and search engines in correctly pronouncing and categorizing content. - Simplified HTML5 Syntax: Unlike older HTML 4.01 or XHTML 1.0 specifications that required complex Document Type Definitions (DTDs) and URLs, HTML5 simplified the declaration to just
<!doctype html>.