HTML <!DOCTYPE html>

Beginner
5 min read
Updated: Oct 2026
4 examples
Live preview

Definition and Usage

The DOCTYPE is a required preamble at the top of every HTML document. It is not an element. Its only practical job today is to tell the browser to use no-quirks mode (often called standards mode) instead of quirks mode.

Always write <!DOCTYPE html> as the first line, then the <html> root with <head> and <body>.

Baseline · Widely available.

Not deprecated and not experimental. The short HTML5 form is supported in all modern browsers.

Legacy doctypes.

Long HTML 4 / XHTML doctypes are historical. Do not use them in new pages — they can trigger limited-quirks or quirks mode depending on the exact string.

Case-insensitive.

<!DOCTYPE html> and <!doctype html> are equivalent. MDN examples often use lowercase; both are fine.

HTML Syntax

syntax.html
<!DOCTYPE html>

No end tag. No attributes in modern HTML. Put it before <html>. That is the only form you need.

Attributes

The HTML5 DOCTYPE has no attributes. Older SGML-style forms used PUBLIC / SYSTEM identifiers — study only, do not copy into new work.

FormDescriptionTry it
<!DOCTYPE html>Recommended. Short HTML5 doctype; triggers no-quirks modeTry it
Legacy PUBLIC / DTDLegacy. Long HTML 4 / XHTML strings; unnecessary and risky for mode switchingTry it
about:legacy-compatOptional SYSTEM legacy string for some generators; prefer the short form when you canRare

Global Attributes

DOCTYPE is not an HTML element, so it does not support the Global Attributes in HTML. Put lang, class, and id on <html> (and other elements) instead.

Live Preview

This page already starts with <!DOCTYPE html>. Read the current document.compatMode value below.

CSS1Compat means no-quirks or limited-quirks; BackCompat means quirks mode.

This document

Loading…

Expected on this tutorial page: CSS1Compat (standards / no-quirks).

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Recommended HTML5 DOCTYPE

Put the short declaration on line 1. Nothing else belongs above it.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Hello</title>
</head>
<body>
  <p>Standards mode page.</p>
</body>
</html>
Try It Yourself

How It Works

  1. Start with <!DOCTYPE html> as the first line.
  2. Add the <html> root (include lang for accessibility).
  3. Fill <head> and <body> as usual.
  4. The browser enters no-quirks mode and follows modern layout rules.

Example 2 — Minimal Complete Starter

A practical first file: doctype, charset, viewport, and title.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My first page</title>
</head>
<body>
  <h1>My first page</h1>
</body>
</html>
Try It Yourself

How It Works

  1. Declare the document with <!DOCTYPE html>.
  2. Set charset early with <meta charset="UTF-8">.
  3. Add a viewport meta so mobile layout scales correctly.
  4. Give the tab a <title>, then write body content.

Example 3 — Inspect document.compatMode

Confirm the render mode from script after the page loads.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>compatMode check</title>
</head>
<body>
  <p id="mode"></p>
  <script>
    document.getElementById("mode").textContent =
      "compatMode = " + document.compatMode;
  </script>
</body>
</html>
Try It Yourself

How It Works

  1. Include a proper DOCTYPE so the page is not in quirks mode.
  2. Read document.compatMode after the document is available.
  3. Expect CSS1Compat for a normal HTML5 page.
  4. See BackCompat only if the document is in quirks mode.

Example 4 — Legacy DOCTYPE (Study Only)

Recognize old HTML 4 strings in tutorials. Do not use them for new sites.

HTML
<!-- Legacy — do not use in new projects -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Legacy doctype</title>
</head>
<body>
  <p>Prefer the short HTML5 doctype instead.</p>
</body>
</html>
Try It Yourself

How It Works

  1. Old specs used a DTD URL after PUBLIC.
  2. Browsers mainly used that string for mode switching, not for loading a DTD today.
  3. Wrong legacy forms can trigger quirks or limited-quirks mode.
  4. Migrate to <!DOCTYPE html> whenever you touch the file.

Edge Cases & Pitfalls

Most DOCTYPE bugs come from putting something above it or copying old DTD strings.

order

Keep it first

Comments or junk before the DOCTYPE can force quirks mode. Only an optional BOM may precede it.

legacy

Skip long DTD forms

HTML 4 / XHTML PUBLIC strings are unnecessary. Prefer <!DOCTYPE html> every time.

semantics

Not a tag you style

There is no </DOCTYPE>, no CSS selector for the declaration, and no global attributes on it.

xhtml

Real XHTML is different

Documents served as application/xhtml+xml always use no-quirks mode. Served as text/html, you still need a doctype.

debug

Check compatMode

Use document.compatMode (and browser console warnings) when layout looks oddly “old.”

Browser Support

DOCTYPE mode switching is core HTML behavior in every browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

<!DOCTYPE html>

All modern browsers honor the short HTML5 doctype and enter no-quirks mode. Always include it as the first line of text/html documents.

98% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All
Supported
DOCTYPE Widely supported

Bottom line: Always ship . Support is universal; the risk is omitting it or using a quirky legacy string.

Key Takeaways

  • Purpose: <!DOCTYPE html> turns on no-quirks (standards) mode.
  • Placement: first line of the file — before <html>.
  • Form: short HTML5 only; skip legacy PUBLIC/DTD strings.
  • Verify: document.compatMode === "CSS1Compat" on a healthy page.

One line: every HTML file should start with <!DOCTYPE html>.

Default CSS Settings

DOCTYPE has no CSS. It is not in the DOM tree as a styled element. It only selects the document’s rendering mode, which can change how some CSS rules behave.

note.css
/* There is no doctype { … } rule.
   Style html, body, and other elements instead. */

html {
  /* your page styles */
}

If layout looks wrong for no clear reason, confirm the DOCTYPE is present and document.compatMode is not BackCompat.

Frequently Asked Questions

DOCTYPE is a required preamble at the top of an HTML document. Its job is to put the browser into no-quirks (standards) mode so layout follows modern HTML and CSS rules.
No. It is a document type declaration, not an element. It has no end tag, no content, and no global attributes.
Always use <!DOCTYPE html> (case-insensitive). There is no good reason to use longer legacy doctypes in new pages.
As the first thing in the document (only an optional BOM may come before it). Do not put comments, whitespace-heavy junk, or elements above it if you want reliable standards mode.
A legacy rendering mode that emulates old browser bugs. Missing or certain old doctypes can trigger it. Check with document.compatMode: BackCompat means quirks; CSS1Compat means no-quirks or limited-quirks.
It does not style anything itself. It chooses the rendering mode, which changes how some CSS and layout rules behave.

Next: dt

Mark the term (name) inside a description list.

dt tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful