Learning Goals
- Explain what <!DOCTYPE html> is and why it exists.
- Describe Standards Mode, Almost Standards Mode, and Quirks Mode.
- Detect the current rendering mode using DevTools and
document.compatMode
. - Avoid common mistakes that trigger Quirks Mode.
Part 1 — What is <!DOCTYPE html>
?
The DOCTYPE is an instruction to the browser telling it to use modern standards when interpreting your HTML and CSS. In modern HTML, it’s always the short, simple form:
<!DOCTYPE html>
Place it as the very first line in your HTML file—no blank lines above it.
Part 2 — Rendering Modes
Modern goal
- Standards Mode — consistent, spec-compliant behavior.
- Almost Standards — nearly identical; a historic table-cells quirk.
- Quirks Mode — legacy behavior emulating very old browsers.
How the browser decides
- Valid modern DOCTYPE → Standards (or Almost Standards).
- Missing/legacy/malformed DOCTYPE → Quirks Mode.
Part 3 — Detecting the Mode
Open DevTools Console and run:
document.compatMode // 'CSS1Compat' = Standards; 'BackCompat' = Quirks
Where to find this in DevTools?
- Open DevTools (Right-click → Inspect).
- Go to Console and type the command above.
- On the Elements panel, if you see a DOCTYPE at the top, you’re likely in Standards Mode.
Live demo: With & Without DOCTYPE
With DOCTYPE (Standards)
Without DOCTYPE (Quirks)
Part 4 — Common Mistakes (and Fixes)
❌ Missing or malformed DOCTYPE
<html>
<head>...</head>
<body>...</body>
</html>
✅ Always start with
<!DOCTYPE html>
<html lang="en">
<head>...</head>
<body>...</body>
</html>
- Don’t add extra characters or comments before the DOCTYPE.
- Don’t copy long, legacy HTML4/XHTML doctypes—use the short modern one.
Part 5 — Step-by-Step: Fix an Old Page
- Open a file that’s missing a DOCTYPE.
- Add
<!DOCTYPE html>
as the first line. - Reload and run
document.compatMode
again — expectCSS1Compat
. - Re-test layout in the browser; box model issues should vanish.
Practice Tasks
- Create lesson-07/index.html with and without DOCTYPE in two copies. Compare
document.compatMode
. - Validate both files. What does the validator report?
- Try adding a blank line before DOCTYPE; check the mode again.
Quick Quiz
- What does the DOCTYPE do in modern HTML?
- How can you tell if you’re in Quirks Mode?
- Which values can
document.compatMode
return? - Where should the DOCTYPE appear in an HTML file?
Sample answers
- It signals the browser to use modern standards mode.
- Check DevTools/Console:
document.compatMode
returns BackCompat; missing DOCTYPE in Elements. - CSS1Compat (Standards/Almost Standards) or BackCompat (Quirks).
- As the very first line—no blank lines before it.
Lesson 7 Dictionary
- Almost Standards Mode
- A near-standards rendering mode with minor legacy behavior (rarely impacts modern layouts).
- BackCompat
- The string returned by
document.compatMode
when the page is in Quirks Mode. - CSS1Compat
- The string returned by
document.compatMode
in Standards/Almost Standards Mode. - document.compatMode
- Read-only property indicating the current rendering mode: CSS1Compat or BackCompat.
- Quirks Mode
- Legacy rendering mode emulating old browsers; triggered by missing/invalid DOCTYPE.
- Rendering Mode
- The browser’s chosen behavior profile (Standards, Almost Standards, or Quirks) for the current page.
- Standards Mode
- Spec-compliant mode used by modern browsers when a valid DOCTYPE is present.
- <!DOCTYPE html>
- The modern DOCTYPE that triggers Standards Mode; always the first line of the document.