HTML Doctype and Browser Modes

HTML Living Standard

The browser decides how to lay out the page after reading an HTML file. A rendering mode is the set of rules it uses for that layout. The first line of the file can change the mode.

On this page

What is Standards Mode?

Standards Mode is the browser’s normal mode for modern web pages. You may see the name no-quirks mode in the specification. As an example of its layout rules, a CSS length such as 120px needs a unit when the number is nonzero. px is the unit for CSS pixels.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Standards Mode</title>
  </head>
  <body>
    <div style="width: 120px; background: lightblue">120 px</div>
  </body>
</html>

Why does Quirks Mode exist?

Some old pages relied on how earlier browsers lay out content. Quirks Mode keeps selected old layout rules so those pages can still work. For example, the browser may treat certain lengths without a unit as pixels. Certain historical doctypes can also select limited-quirks mode, which keeps fewer of the old rules.

<!-- This legacy example omits the doctype. -->
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Quirks Mode</title>
  </head>
  <body>
    <div style="width: 120; background: lightblue">Sample</div>
  </body>
</html>

What does <!doctype html> do?

Write this declaration before <html>. It is an instruction for the browser about how to process the document. This form selects Standards Mode. In a typical HTML file, omitting the declaration selects Quirks Mode.

How to try it Check that the first line of index.html is exactly the declaration below.

<!doctype html>

What to expect The line is used by the browser. The text you wrote in body still appears as the page content.

Compare the two modes in the browser

The blue box has a width without a unit. The script prints document.compatMode, the name exposed to JavaScript for the current mode. It also prints whether the width was accepted. CSS1Compat covers both Standards Mode and limited-quirks, while BackCompat indicates Quirks Mode.

How to try it Save the full code as standards.html. Save a second copy as quirks.html, remove its first line, and open both files in the same browser.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Compare rendering modes</title>
</head>
<body>
  <div id="box" style="width: 120; background: lightblue">Sample</div>
  <p id="result"></p>
  <script>
    const box = document.querySelector("#box");
    document.querySelector("#result").textContent =
      document.compatMode + " | width = " + (box.style.width || "ignored");
  </script>
</body>
</html>

What to expect The first file shows CSS1Compat | width = ignored. The file without a doctype shows BackCompat | width = 120px in browsers that implement this quirk. Change 120 to 120px to see both layouts accept the width.

Build your first document

A browser requests a document, parses its markup into a DOM, and displays its body. Compare the tab title with the visible heading. Edit the heading in DevTools, reload, then edit the file and reload again to see which change persists.

Official references