Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

HTML concept: doctype

The `<!doctype html>` declaration is the required HTML document preamble and selects modern rendering behavior; it is neither an element nor a language version number.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<!doctype html>

The conforming short form is <!doctype html>: DOCTYPE and html are matched ASCII case-insensitively. The parser creates a DocumentType node before the document element; element selectors do not find it.

For a text/html resource, the correct declaration produces no-quirks mode, normally exposed as CSS1Compat. An omitted declaration or certain historical identifiers can produce BackCompat or limited-quirks mode, so otherwise identical CSS can compute differently.

Comments and whitespace may precede the declaration, but it must occur before the document element. <!DOCTYPE html SYSTEM "about:legacy-compat"> exists only for generators unable to emit the short form; about:legacy-compat is not a version selector. An application/xhtml+xml document follows XML syntax instead of this HTML algorithm.

The example uses DOMParser to create documents with and without the declaration, reads Document.doctype and compatMode, and observes CSS1Compat versus BackCompat. The null selector result demonstrates that the declaration is not an element.

Examples

<!doctype html>
<meta charset="utf-8">
<output id="result"></output>
<script>
const parser = new DOMParser();
const standard = parser.parseFromString("<!DOCTYPE HTML><title>Standard</title>", "text/html");
const missing = parser.parseFromString("<title>Missing</title>", "text/html");
result.textContent = `node=${standard.doctype.nodeName}; standard=${standard.compatMode}; missing=${missing.compatMode}; query=${standard.querySelector("doctype")}`;
</script>

Result

node=html; standard=CSS1Compat; missing=BackCompat; query=null

The executed example verifies the declaration node and mode selection; it does not survey every historical identifier that triggers limited or full quirks mode.

Sources