What is <!DOCTYPE html>?
<!doctype html> is the document type declaration at the very top of an HTML file. It tells the browser to render the page in standards mode, following modern HTML and CSS rules, instead of quirks mode, which emulates old, inconsistent browser behavior. It is not a tag or an element, and it is case-insensitive.
Why it exists
In the 1990s and 2000s, browsers implemented CSS differently. When standards-compliant rendering arrived, browsers kept a quirks mode for old pages so they would not break. The doctype is the switch: with it, you get standards mode; without it, the browser guesses and usually picks quirks mode.
What quirks mode changes
- Box sizing: widths may include padding and borders (old IE box model).
- Tables do not inherit font sizes.
- Percentage heights and some inline layout behave differently.
- Various CSS parsing differences.
These cause hard-to-debug layout differences, which is why the doctype is mandatory in practice.
Old doctypes
HTML 4.01 and XHTML had long declarations referencing DTDs, for example <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">. The modern HTML doctype is short because HTML is no longer based on SGML DTDs.
Rules
- It must be the first thing in the document (only whitespace or comments before it, and comments can trigger issues in old browsers).
<!DOCTYPE html>and<!doctype html>are equivalent.
How to check
document.compatMode returns "CSS1Compat" in standards mode and "BackCompat" in quirks mode.
Code example
1<!doctype html>2<html lang="en">3 <head>4 <meta charset="utf-8">5 <title>Doctype demo</title>6 <style>7 .box { width: 200px; padding: 20px; border: 5px solid #4f46e5; }8 </style>9 </head>10 <body>11 <div class="box">Standards mode: total width is 250px (200 + 40 + 10)</div>12 <p>Rendering mode: <strong id="mode"></strong></p>13 <script>14 // "CSS1Compat" = standards mode, "BackCompat" = quirks mode15 document.getElementById("mode").textContent = document.compatMode;16 </script>17 </body>18</html>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.