JavaScript Document compatMode Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

Document.compatMode is a read-only instance property that tells you whether the page is rendered in quirks mode or standards (no-quirks / limited-quirks) mode. Learn the BackCompat and CSS1Compat values, how the doctype chooses the mode, and how to check it in JavaScript—with five examples and try-it labs.

01

Kind

Read-only

02

Returns

string

03

Quirks

BackCompat

04

Standards

CSS1Compat

05

Fix via

<!DOCTYPE html>

06

Status

Baseline widely

Introduction

When a browser loads HTML, it chooses a rendering mode. Pages with a proper doctype (such as <!DOCTYPE html>) usually run in standards mode. Pages without a doctype may fall into quirks mode, where layout rules behave more like old browsers.

document.compatMode exposes that choice as a simple string. It is useful when debugging weird box-model or percentage-height bugs on legacy pages—or when confirming a template always ships a doctype.

💡
Always start HTML with a doctype

Put <!DOCTYPE html> as the first line of every modern HTML document. That is the simplest way to get CSS1Compat (standards) rendering.

Related Document tutorials: children, characterSet, Document constructor.

Understanding Document.compatMode

A read-only instance property on Document. Its value is a string describing the document’s compatibility (rendering) mode.

  • "BackCompat" — quirks mode (MDN).
  • "CSS1Compat" — no-quirks (standards) or limited-quirks (almost standards) mode (MDN).
  • Read-only — you cannot assign a new mode from JavaScript.
  • Chosen at parse time — mainly from the doctype and related document rules.
  • Historical names — MDN notes that older “standards” labels are outdated; the modes themselves are standardized.

📝 Syntax

JavaScript
document.compatMode

Value

A string that is one of:

  • "BackCompat" — quirks mode
  • "CSS1Compat" — no-quirks or limited-quirks mode

MDN check

JavaScript
if (document.compatMode === "BackCompat") {
  // in Quirks mode
}

⚡ Quick Reference

GoalCode / note
Read modedocument.compatMode
Detect quirksdocument.compatMode === "BackCompat"
Detect standards-likedocument.compatMode === "CSS1Compat"
Force standards (HTML)<!DOCTYPE html> as first line
Change from JS?No — read-only
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.compatMode.

Type
string

Read-only

Quirks
BackCompat

Legacy layout

Standards
CSS1Compat

Preferred

Status
baseline

Standard API

📋 Quirks vs Standards (why it matters)

TopicQuirks (BackCompat)Standards (CSS1Compat)
Box model historyMay follow older IE-like rulesStandard CSS box model
Percentage heightsOften more forgiving / surprisingStrict ancestor height chain
Modern CSSHarder to reason aboutPredictable for tutorials & apps
New projectsAvoidAlways aim for this

Examples Gallery

Examples follow MDN Document: compatMode. Use View Output or Try It Yourself for each case.

📚 Getting Started

Read the compatibility mode string for the current document.

Example 1 — Read document.compatMode

Log the raw value returned by the browser.

JavaScript
console.log(document.compatMode);
// Usually "CSS1Compat" on pages with <!DOCTYPE html>
Try It Yourself

How It Works

The try-it editor documents include a doctype, so you should see standards mode.

Example 2 — MDN: Detect Quirks Mode

Branch when compatMode is BackCompat.

JavaScript
if (document.compatMode === "BackCompat") {
  console.log("Quirks mode — check the doctype");
} else {
  console.log("Not quirks mode");
}
Try It Yourself

How It Works

This is the exact MDN pattern for detecting quirks mode in legacy debugging.

📈 Labels, Standards & Doctype

Friendly messages and what to fix in HTML.

Example 3 — Confirm Standards-Like Mode

Treat CSS1Compat as the healthy modern result.

JavaScript
const isStandardsLike = document.compatMode === "CSS1Compat";
console.log("Standards-like mode:", isStandardsLike);
console.log("compatMode:", document.compatMode);
Try It Yourself

How It Works

MDN: CSS1Compat covers both no-quirks and limited-quirks modes.

Example 4 — Friendly Mode Label

Map the raw string to a beginner-friendly message.

JavaScript
function describeCompatMode(mode) {
  if (mode === "BackCompat") return "Quirks mode";
  if (mode === "CSS1Compat") return "Standards / almost-standards mode";
  return "Unknown: " + mode;
}

console.log(describeCompatMode(document.compatMode));
Try It Yourself

How It Works

Useful in support dashboards or teaching demos that show layout mode at a glance.

Example 5 — Doctype Presence Check

Pair compatMode with document.doctype for debugging.

JavaScript
const hasDoctype = document.doctype !== null;
console.log("Has doctype:", hasDoctype);
console.log("compatMode:", document.compatMode);
if (!hasDoctype) {
  console.log("Tip: add <!DOCTYPE html> as the first line");
}
Try It Yourself

How It Works

Missing doctype is the classic path into quirks mode. Fix markup first; do not try to override mode from JS.

🚀 Common Use Cases

  • Legacy page debugging — explain odd layout when a doctype is missing.
  • Template audits — assert that every page reports CSS1Compat.
  • Support tooling — include compatMode in bug-report bundles.
  • Teaching CSS history — show why doctype matters for the box model.
  • Not for feature toggles — prefer feature detection over mode sniffing in new apps.
  • CMS health checks — catch themes that strip the doctype accidentally.

🧠 How the Browser Chooses a Mode

1

HTML bytes arrive

Browser begins parsing the document.

Parse
2

Doctype is inspected

Missing/odd doctype may select quirks mode.

Doctype
3

Rendering mode locked

Layout rules follow quirks or standards-like behavior.

Layout
4

Read with compatMode

JavaScript reports BackCompat or CSS1Compat.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Read-only — change mode by fixing HTML (especially the doctype), not by assignment.
  • BackCompat = quirks; CSS1Compat = no-quirks or limited-quirks.
  • MDN: older “standards” naming is historical; modes are standardized today.
  • Always ship <!DOCTYPE html> as the first line of new HTML.
  • Related: children, characterSet, Document constructor.

Universal Browser Support

Document.compatMode is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Document.compatMode

Read-only quirks vs standards mode string — essential for doctype and layout debugging.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in legacy IE
Full support
Document.compatMode Excellent

Bottom line: Use document.compatMode to detect quirks mode. Prefer CSS1Compat by always including . Do not try to set the mode from JavaScript.

Conclusion

Document.compatMode is the standard way to ask whether a page is in quirks mode (BackCompat) or standards-like mode (CSS1Compat). Use it for diagnostics—and keep modern pages in standards mode with a proper HTML5 doctype.

Continue with contentType, ownerDocument, characterSet, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Start every HTML page with <!DOCTYPE html>
  • Log compatMode when debugging mysterious layout bugs
  • Prefer CSS1Compat for all new projects
  • Audit CMS/theme output for a missing doctype
  • Pair mode checks with document.doctype during support

❌ Don’t

  • Ship HTML without a doctype
  • Try to assign document.compatMode = "CSS1Compat"
  • Build feature detection around quirks mode in new apps
  • Assume every CSS1Compat page is perfectly modern CSS
  • Ignore quirks mode warnings on legacy intranet pages

Key Takeaways

Knowledge Unlocked

Five things to remember about document.compatMode

Read-only mode string — prefer CSS1Compat with an HTML5 doctype.

5
Core concepts
02

Status

baseline

Standard
⚠️03

Quirks

BackCompat

Legacy
🎯04

Standards

CSS1Compat

Preferred
📝05

Fix via

DOCTYPE

HTML

❓ Frequently Asked Questions

A read-only string: "BackCompat" when the document is in quirks mode, or "CSS1Compat" when it is in no-quirks (standards) mode or limited-quirks (almost standards) mode.
No. MDN marks Document.compatMode as Baseline Widely available (since July 2015). It is a standard way to detect rendering mode.
Usually a missing or incomplete HTML doctype. Modern pages should start with <!DOCTYPE html> so the browser uses standards (CSS1Compat) mode.
MDN: the document is in no-quirks mode (standards) or limited-quirks mode (almost standards). Both report CSS1Compat via compatMode.
They are historical. MDN notes that quirks, standards, and almost-standards modes are now standardized, so older marketing names are less meaningful today.
No. It is read-only. Fix the doctype (and related markup) so the browser chooses the mode you want — typically standards mode with <!DOCTYPE html>.
Did you know?

Quirks mode was invented so old pages without doctypes would keep looking roughly like they did in 1990s browsers. Modern sites almost never want that—which is why a one-line <!DOCTYPE html> is still one of the most important habits in web development.

Next: contentType

Learn how to read the document MIME type (such as text/html).

contentType →

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.

6 people found this page helpful