HTML Doctype and Document Modes
Choose the modern doctype
A doctype is a declaration, not an element. Begin a text/html document with <!doctype html> to request no-quirks mode. DOMParser parses a supplied string into a separate document; this allows comparison without changing the surrounding playground.
<output id="result"></output><script> const parsed = new DOMParser().parseFromString("<!doctype html><p>Standards</p>", "text/html"); const result = parsed.compatMode; document.querySelector("#result").textContent = String(result);</script>Expected output: CSS1Compat
Compare missing and legacy doctypes
Without a doctype, an ordinary HTML document enters quirks mode, which preserves some old layout behaviors. A few historical declarations trigger limited-quirks mode. compatMode returns BackCompat for quirks and CSS1Compat for both other modes; it cannot distinguish all three.
<output id="result"></output><script> const parser = new DOMParser(); const missing = parser.parseFromString("<p>Old</p>", "text/html"); const legacy = parser.parseFromString('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><p>Old</p>', "text/html"); const result = missing.compatMode + " / " + legacy.compatMode; document.querySelector("#result").textContent = String(result);</script>Expected output: BackCompat / CSS1Compat
Inspect an actual document
Document mode is selected during parsing; adding or removing a doctype node after parsing does not switch the selected mode. Use a complete document for testing, not a fragment inserted into an existing page.
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>Mode</title> </head> <body> <output id="result"></output> <script> document.doctype.remove(); document.querySelector("#result").textContent = document.compatMode; </script> </body></html>The mode remains CSS1Compat after removal.
Common mistakes and practice
Use the short modern doctype in production. Do not use a historical doctype just to repair a CSS bug; inspect the layout instead. The third example removes a doctype deliberately for diagnosis only.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Doctype and Document Modes — Knowledge Check
Apply the internals concepts taught in this lesson.
What is the modern declaration?
What does an ordinary HTML document without a doctype normally use?
Which value identifies quirks?
Can CSS1Compat distinguish limited quirks from no quirks?
Does removing the doctype after parsing change the mode?