HTML Doctypes
In this page:
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?
<!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
<!-- 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
<!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
<!-- 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
<!DOCTYPE html>
<html lang="en">
</html>
- Forgetting to include the doctype declaration on the very first line of your HTML file.
- Placing HTML comments or spaces above the doctype declaration, causing browsers to switch into Quirks Mode.
- Using long, outdated HTML4 doctype strings in new web projects.
- 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: .
The HTML5 doctype is natively supported by all modern and legacy web browsers.
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- HTML Keyboard Shortcuts
- HTML Browser Support
- HTML Character Sets
- HTML Doctypes
- HTML Audio/Video Reference
- HTML Meta Tags
- HTML ARIA Roles
- HTML Input Validation
- HTML Iframe Security
- HTML Responsive Images
- HTML Web Fonts
- HTML Icon Fonts
- HTML Progress & Meter
- HTML Output Element
- HTML Datalist
- HTML Image Maps
- HTML Browser DevTools
- HTML Validation
- HTML Quiz
- HTML Interview Prep