← Back to HTML Course | Chapter 10: Reference & Interview Prep | Lesson 4 of 20

HTML Doctypes

Imagine checking in at a luxury hotel. If you arrive dressed neatly in standard formal clothes, the staff greets you politely and guides you to the main lobby (Standard Mode). But if you arrive in messy, outdated clothes, the staff might treat you differently, forcing you to use the side entrance (Quirks Mode). An HTML doctype is that dress code. It must be the very first line of code in your file. It tells the browser exactly what version of HTML you are writing. Without a proper doctype, the browser will switch into an outdated layout mode, causing your modern styles and grids to render as broken elements.

What is a Doctype?

The doctype declaration is an instruction that tells the browser what document type and markup version to expect, ensuring the browser renders your page layout consistently.

Note: The doctype is not an HTML tag; it is a standard declaration that must be written on line one.

Warning: Failing to write the doctype on the very first line will cause the browser to switch into an outdated Quirks Mode.

Example: What is a Doctype?

markup
<!DOCTYPE html>
<html>
</html>

Quirks Mode vs. Standard Mode

If a webpage has a missing or incorrect doctype declaration, browsers will switch into 'Quirks Mode'. In Quirks Mode, browsers render your pages using old, non-standard layout rules to support ancient websites, which will break your modern CSS styles.

Note: Always declare a clean HTML5 doctype to keep the browser running in high-performance Standard Mode.

Warning: Quirks Mode can cause margins, padding, and container sizes to render incorrectly on different devices.

Example: Quirks Mode vs. Standard Mode

markup
<!-- Missing doctype triggers Quirks Mode -->
<!DOCTYPE html>
<html>
</html>

The Modern HTML5 Doctype

In older HTML versions, doctypes were long, complex strings that linked to official document type definitions (DTDs). HTML5 simplified this by introducing a short, clean, and easy-to-remember declaration.

Note: Use the short HTML5 doctype to keep your code lightweight and easy to read.

Warning: Avoid copy-pasting old HTML4 doctype strings into new projects, as they are obsolete.

Example: The Modern HTML5 Doctype

markup
<!DOCTYPE html>

Historical Legacy Doctypes

Older versions of HTML (like HTML 4.01 or XHTML 1.0) required long, complex doctype strings that linked to W3C definitions to validate your code. While these are obsolete today, understanding them is useful when maintaining older websites.

Note: If you are updating an old website, replace its legacy doctype with the clean HTML5 declaration.

Warning: Using transitional or strict HTML4 doctypes can cause modern CSS grid layouts to fail rendering in some browsers.

Example: Historical Legacy Doctypes

markup
<!-- Legacy HTML 4.01 doctype -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!-- Modern HTML5 doctype -->
<!DOCTYPE html>

Validation and Web Standards

Web standards require every HTML page to start with a valid doctype. Including the doctype is essential for validating your code and ensuring it passes W3C markup validation tests.

Note: Use an online markup validator to check your HTML pages for formatting and doctype errors.

Warning: Failing to include a doctype can cause search engine bots to mark down your quality score.

Example: Validation and Web Standards

markup
<!DOCTYPE html>
<html lang="en">
</html>
Common Mistakes
  1. Forgetting to include the doctype declaration on the very first line of your HTML file.
  2. Placing HTML comments or spaces above the doctype declaration, causing browsers to switch into Quirks Mode.
  3. Using long, outdated HTML4 doctype strings in new web projects.
Chapter Summary
  • The doctype is a standard declaration that must be written on line one of your HTML file.
  • It tells the browser what HTML version to expect, ensuring the page renders consistently in Standard Mode.
  • The modern HTML5 doctype is simple and clean: .
Browser Support

The HTML5 doctype is natively supported by all modern and legacy web browsers.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.