designwithfocus

HTML

What Is Doctype Used For? The HTML Preamble, Not a Tag

By Design With Focus Team 6 min read
A short code line at the top of an HTML document on a screen

Short answer

The HTML doctype is the preamble `<!DOCTYPE html>`, and it is not an element. WHATWG calls a DOCTYPE a required preamble. MDN says its purpose is to keep the browser out of quirks mode so layout follows current HTML and CSS. The match is ASCII case-insensitive, so `<!doctype html>` is the same preamble. A longer legacy form exists only for generators that cannot emit the short line.

Last verified against the sources listed below.

Key takeaways

  • Put `<!DOCTYPE html>` at the top of an HTML document, before any other content.
  • WHATWG defines it as a preamble. Elements are a separate section of the syntax.
  • MDN names three modes: quirks, limited-quirks, and no-quirks.
  • The short doctype is what current HTML standards recommend for no-quirks mode.
  • ASCII case-insensitive means the lowercase forms of the two strings match.
  • In the DOM, document.doctype returns the doctype child, or null if there is none.

What is doctype used for? In HTML it is the preamble that asks the browser to follow current specifications instead of an old compatibility mode. The line you write is <!DOCTYPE html>.

HTML is the markup of the page. The doctype is the line before that markup, and the standard treats it as a different kind of syntax from the elements that follow.

What is a doctype declaration in HTML?

It is the required preamble <!DOCTYPE html>, and the standard does not define it as an element.

WHATWG’s syntax section says a DOCTYPE is a required preamble. The note under that definition says doctypes exist for legacy reasons. When the preamble is missing, browsers tend to use a different rendering mode that is incompatible with some specifications. Including it makes the browser attempt to follow the relevant specifications.

The required pieces, in order, are a case-insensitive match for <!DOCTYPE, one or more ASCII whitespace characters, a case-insensitive match for html, optional whitespace, and a > character. In other words, <!DOCTYPE html>. A generator that cannot emit that short string may insert a legacy string, producing <!DOCTYPE html SYSTEM "about:legacy-compat">. The standard says not to use the legacy string unless the generator has no other choice.

What is the doctype tag, if people call it a tag?

People say tag, and the syntax calls it a preamble, which is why the word tag does not appear in the definition.

The next section of the HTML standard is about elements. It names six kinds: void, template, raw text, escapable raw text, foreign, and normal. A preamble is not on that list, and it has no end tag, no attributes, and no content model.

You do not write </doctype>.

MDN’s doctype glossary uses the same split. It calls <!doctype html> the required preamble at the top of all documents, and it says the sole purpose is to stop the browser switching into quirks mode. MDN’s examples prefer lowercase. The uppercase form <!DOCTYPE html> is also common, and both match.

What does standards mode versus quirks mode mean?

MDN describes three modes, and the short doctype is how an HTML document asks for no-quirks mode.

Quirks mode emulates layout from Navigator 4 and Internet Explorer 5, so old pages keep working. No-quirks mode is the behavior described by modern HTML and CSS. Limited-quirks mode implements only a small number of quirks.

MDN notes that limited-quirks used to be called almost-standards mode, and no-quirks used to be called full standards mode. The names changed because the behavior is now specified.

Mode on MDN Older name MDN records What the page says it does
Quirks none given as a rename Emulates Navigator 4 and Internet Explorer 5
Limited-quirks Almost-standards A small set of quirks only
No-quirks Full standards Modern HTML and CSS behavior

Put the preamble at the beginning, before any other content. MDN says there is no valid reason to use a more complicated doctype. Another doctype can trigger limited-quirks or quirks mode. The older public identifiers are the ones that risk that switch.

One exception is on the same MDN page. If you serve the document as XHTML with the MIME type application/xhtml+xml, you do not need a doctype to get no-quirks mode, because those documents always use it. An ordinary .html file sent as HTML does not get that exception.

What is doctype html called in the DOM?

The DOM calls it a doctype node, and document.doctype returns that node or null.

The DOM standard says the doctype getter returns the child of the document that is a doctype. If there is no such child, the result is null. That is a lookup of a node that is already in the tree.

It is not a mapping from the preamble onto a table of tags, which neither WHATWG nor MDN defines. “Doctype mapping” is not a term in those two documents.

Case follows the Infra standard. A string is an ASCII case-insensitive match for another when the ASCII lowercase of the first equals the ASCII lowercase of the second. That is why <!DOCTYPE html>, <!doctype html>, and <!DocType html> are the same preamble.

Characters outside ASCII are not folded by that rule. The word you need here is the ASCII word html.

What mistake is easy to ship?

The easy mistake is to drop the preamble, or to bury it under a comment or a blank line of content the parser sees first.

MDN says the doctype has to be at the beginning, before any other content, and that its only job is to activate no-quirks mode. A file that starts with a visible note, or with an element, skips that job. The page then lays out with the old rules, which is a layout bug, not a missing <html> tag.

Adding the preamble does not validate your elements. It only selects the mode.

A second mistake is to paste a long legacy doctype from an old template. WHATWG allows SYSTEM "about:legacy-compat" only for generators that cannot print the short line. MDN says there is no valid reason to choose a more complicated doctype, because the wrong one can select limited-quirks or quirks mode.

An SVG to JSX converter turns a drawing into a component. The page that hosts that component is still an HTML document, and that document still wants <!DOCTYPE html> at the top if it is served as HTML. The converter does not write the preamble for you.

Frequently asked questions

What is doctype used for?

It selects the rendering mode. WHATWG says doctypes are required for legacy reasons, and that omitting one tends to make browsers use a rendering mode that is incompatible with some specifications. Including the doctype is how the document asks the browser to follow the relevant specifications. MDN narrows the same idea: the preamble's purpose is to prevent quirks mode. It does not create a heading, a paragraph, or any other element, and it is not a stylesheet.

What is a doctype declaration in HTML?

People say declaration for the line at the top of the file. In the HTML standard the name of that line is DOCTYPE, and the required form is the preamble `<!DOCTYPE html>`. It is a string match, not an element with a start tag and an end tag. The pieces are the characters `<!DOCTYPE`, whitespace, the word html, optional whitespace, and a closing `>`. An optional legacy string can sit before that closing mark, and the standard says not to use it unless a generator cannot emit the short form.

What is the doctype tag?

There is no doctype tag in the HTML syntax. A tag belongs to an element. WHATWG lists six kinds of elements in the section after the doctype: void elements, the template element, raw text elements, escapable raw text elements, foreign elements, and normal elements. The doctype is defined before that list, as a preamble. Calling it a tag is a nickname. The parser is looking for the preamble, not for an element named doctype.

What does doctype html mean?

It is the HTML preamble written so a browser can recognize it. `<!DOCTYPE html>` and `<!doctype html>` match because the comparison is ASCII case-insensitive. Infra defines that as the ASCII lowercase of one string equaling the ASCII lowercase of the other. MDN's code samples use the lowercase spelling, and MDN also notes that the uppercase spelling is common. The word html here names the syntax. It does not point at a DTD file the way older public identifiers did.

What is doctype mapping?

WHATWG and MDN do not define a doctype mapping table for HTML. There is no step that maps the preamble onto a list of element names or onto a schema file. The preamble is the string itself, and the browser uses it to choose quirks mode, limited-quirks mode, or no-quirks mode. If you see the phrase in a search box, the HTML living standard's answer is the short preamble plus the rendering mode, not a map. Document.doctype in the DOM simply returns that doctype node or null.

Sources