HTML Doctype: How to Declare It and Fix a Missing Doctype
The doctype is the first line of every web page. See what it does, how our free report checks it and the quick fix when it is missing.
Free to use 8 min read Updated Oct 2026
What Is the HTML Doctype? The Quick Answer
Quick answer: The HTML doctype is the declaration on the first line of a web page, written
<!DOCTYPE html>, and it tells the browser to render the page in standards mode instead of quirks mode. Our report checks that the page the browser builds starts with one. Having it keeps layout predictable and clears a best practices item, but it is not a ranking boost.
- Pass: the report found a doctype at the top of the rendered page.
- Fail: no doctype was found, so browsers fall back to quirks mode and may lay the page out like it's decades old.
- Fix: put
<!DOCTYPE html>alone on line one of the template, above the html tag.
What Is a Doctype Declaration in HTML?
People call it the html doctype tag, but it isn't a tag. It is an instruction with no closing tag and no attributes, and it never belongs inside head or body. When the parser accepts it, the browser creates a doctype node in the Document Object Model, which you can inspect in the console as document.doctype.
The name is short for document type declaration. In the SGML era, pages pointed at a document type definition, or DTD, that listed the allowed elements. That's why older doctypes carry a doctype public identifier and often a system identifier, a URL ending in strict.dtd or loose.dtd. HTML5 dropped the DTD and kept the doctype only as the standards mode switch.
Don't mix it up with two neighbors: the lang attribute names the language, and the charset declaration says how bytes are decoded. Our charset declaration guide covers the last one.
How to Write the HTML Doctype Declaration
The doctype syntax is a single short line, and it goes first in the file. Here is a minimal page you can copy into a template.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Doctype demo</title>
</head>
<body>
<p>Hello, standards mode.</p>
</body>
</html>The doctype sits alone on line one, the html root element follows and the charset tag leads off the head. The keyword is case insensitive, so DOCTYPE and doctype both work. Skip any version number, quotes or closing slash.
The HTML Standard doctype section allows comments and whitespace before it, but don't use that freedom. XML generators sometimes print the legacy form <!DOCTYPE html SYSTEM "about:legacy-compat">, which is valid and still gives standards mode, though Lighthouse expects the plain short form and will flag it, so switch to <!DOCTYPE html> if you control the template.
Paste the sample into the Online HTML Viewer, or edit your page in the HTML Editor.
Why the HTML Doctype Matters: Standards Mode vs Quirks Mode
Browsers use one of three rendering modes: no-quirks, which most people call standards mode; limited-quirks, called almost standards mode in older docs; and full quirks. The document.compatMode property returns CSS1Compat for the first two and BackCompat for quirks mode. Quirks mode copies Netscape Navigator 4 and Internet Explorer 5 so old pages keep working. The MDN quirks mode guide explains how the doctype picks the mode.
In our test in headless Chromium, a div set to 24px text held a table. With a doctype the table cell text measured 24px. Without one it dropped to 16px, because quirks mode stops tables inheriting font size. Run document.compatMode in the browser DevTools console to check any page yourself.
Read document.doctype to see the name, publicId and systemId the browser really parsed. Firefox also logs a console warning for quirks mode, so check the Console tab before a validator. The Nu HTML Checker flags a missing doctype too.
Search engines don't reward a page for having a doctype, but you gain stable rendering and a clean Lighthouse result.
How SEO Site Checker Tests Your HTML Doctype
Our free website SEO score checker doesn't search your raw source for the word doctype. A headless Chromium browser loads the page and we serialize the final document.
The rule: the check passes when the first line of the rendered HTML starts with a doctype, tested without regard to letter case.
A pass shows a green tick, a line saying the page has a doctype declaration and a code badge. A fail shows a red icon, a line saying it has none, a red How to Fix button and a Read the Doctype guide link pointing here. The badge always prints the short HTML5 form, so it can't show your version.
The Lighthouse doctype audit also flags a missing doctype. Lighthouse is stricter: it also flags doctypes that push the page out of standards mode and expects the plain short form, so a pass in our report guarantees nothing there.
What passes
- Any doctype on line one counts, in any letter case, including legacy HTML 4.01 and XHTML forms.
- A comment, blank lines, an XML declaration or one byte order mark before it still pass.
What fails
- No doctype declaration at all fails, and so does a typo in the keyword, a doctype with no name or a doctype placed after the html tag.
- Any stray output printed before the doctype also fails: visible text, an element, a PHP notice or a second byte order mark. The late doctype is ignored.
What the check does not test
- It doesn't judge the choice. A misspelled name such as htm and an HTML 4.01 Transitional identifier with no URL both pass while still triggering quirks mode, and junk after the name, such as a stray word after html, also passes while still rendering in quirks mode.
- XHTML served as application/xhtml+xml without a doctype is in standards mode, yet it fails here.
HTML Doctype Types Compared: HTML5, HTML 4.01 and XHTML
This table shows the mode each older doctype triggers.
| Doctype | Public identifier contains | Mode | Advice |
|---|---|---|---|
| HTML5 | None, just the word html | Standards | Use it for every new page. |
| HTML 4.01 Strict | DTD HTML 4.01//EN | Standards | Lighthouse expects the short form, so switch. |
| HTML 4.01 Transitional with its URL | DTD HTML 4.01 Transitional//EN and a URL | Limited quirks | Replace with the short form. |
| HTML 4.01 Transitional without URL | DTD HTML 4.01 Transitional//EN and no URL | Quirks | Replace it. Tutorials paste this one. |
| HTML 4.01 Frameset | DTD HTML 4.01 Frameset//EN | Quirks, or limited quirks with a URL | Frame based pages only. |
| XHTML 1.0 Strict or 1.1 | DTD XHTML 1.0 Strict//EN | Standards | Switch to the short form. |
| XHTML 1.0 Transitional | DTD XHTML 1.0 Transitional//EN | Limited quirks | Replace with the short form. |
Frame based pages are covered in our framesets guide, and old doctypes tend to arrive with font and center tags, which the deprecated HTML tags guide explains.
Where to Add the Doctype in WordPress, Laravel and Static Sites
Each platform prints the page top from a different file.
| Platform | File to check | What to look for |
|---|---|---|
| Static site | The top of each .html file | The doctype alone on line one. |
| WordPress classic theme | header.php in a child theme | The doctype before the html tag. |
| Laravel | The layout in resources/views | The first line of the Blade layout. |
| Shopify | theme.liquid and snippets | Custom code printed before the doctype. |
On WordPress and Laravel, a common culprit is output printed before the layout starts, for example a notice from functions.php, a plugin or a debug call. The mistakes list below shows what to look for.
Hosted builders such as Shopify, Wix and Squarespace print the doctype themselves, so a failure there usually points to custom code or a theme edit that outputs something first. On Shopify, check theme.liquid and any snippet above the opening tag, and ask platform support if you cannot find it. Keep the charset tag first inside head, as the charset guide above explains.
Common HTML Doctype Mistakes
- Output before the doctype. Visible text, a PHP notice, a second byte order mark or a non-breaking space pushes the doctype down and the browser ignores it. Plain blank lines and a single byte order mark do not fail our check, but remove them anyway. In PHP they are sent as output before your headers, which can cause the "headers already sent" warning, and keeping the doctype strictly first removes any doubt about how a browser will treat it.
- A second layout wrapping the page. A nested template can bury the real doctype, so search the raw source for a second opening html tag.
- A doctype after the html tag, or inside head. This usually comes from pasting a snippet at the top of a template, and the parser ignores a doctype it meets late.
- A mistyped name. Writing htm instead of html passes our check but renders in quirks mode, so compare the spelling against the short form.
- An old HTML 4.01 Transitional string. Tutorials paste it without its URL, which means quirks mode.
- A cached copy. A cache or CDN may serve the old page, so test with a throwaway query string.
- Trusting view page source alone. It shows the raw file, not the parsed page our report reads.
How to Fix
The short version, the same steps you see when you click How to Fix in your report.
Why this fails: the page the browser built has no doctype, so browsers switch to quirks mode and may lay it out like a very old page. Our report reads the final page, so visible text or a PHP notice printed before the doctype fails it too.
How to fix it:
- Open the template or file that prints the very top of the page.
- Make line one exactly the short doctype shown below, with nothing above it and the html tag on the next line.
- Remove anything printed before it, such as stray echo or print calls, PHP notices and hidden characters from includes.
- Check that only one layout prints the doctype.
- Save the file, deploy it and purge page, plugin and CDN caches.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">- WordPress: edit
header.phpin a child theme, since parent theme edits are lost on update. A block theme has noheader.php, so look for a notice, echo or stray byte order mark or non-breaking space printed byfunctions.phpor a plugin. - Laravel: put the doctype on the very first line of your Blade layout, for example
resources/views/layouts/app.blade.php, and look for debug output, a stray echo or a hidden character above it. - Static site or plain PHP: fix the top of the file or the shared header include.
- Hosted builders: check custom code or theme edits that print output before the page, then ask platform support if you cannot find it.
A classic WordPress theme should open header.php like this. Use the short form, not an old HTML 4.01 or XHTML string:
<!DOCTYPE html>
<html <?php language_attributes(); ?>>To confirm the mode, run this in the DevTools console. BackCompat means quirks mode, and CSS1Compat means standards or limited-quirks mode.
document.compatModeRe-test: purge your caches first, then re-run the free SEO check and confirm the Doctype check shows a green tick.
Frequently Asked Questions
What does doctype html mean?
Is the doctype required in HTML?
Is doctype html case sensitive?
Is the doctype an HTML tag?
Where should the doctype go in an HTML file?
Why is my doctype not working?
Does the HTML doctype affect SEO?
See how your site scores right now
Paste your address into the free report, then use the How to Fix button on anything that needs work.
Free to use. Results in under a minute for most sites.