Doctype
A DOCTYPE is the first line of an HTML document that tells browsers which HTML standard to expect and helps them render your page in a given Standards Mode. DOCTYPE stands for Document Type Declaration.
- The DOCTYPE is an instruction to the browser, not an HTML tag.
- A correct DOCTYPE helps browsers render pages consistently using Standards Mode instead of Quirks Mode.
- For modern websites, the recommended DOCTYPE is the simple HTML5 declaration:
<!DOCTYPE html>. - Missing or incorrect doctypes can cause layout inconsistencies across browsers (spacing, box model issues, typography, etc.).
- Checking DOCTYPE is a quick, high-impact best practice in technical SEO and QA workflows.
What is a DOCTYPE?
A DOCTYPE declaration tells the browser what type of document it is about to read, so it can choose the right rendering rules.
A doctype declaration is added at the top of the HTML document, an example is shown below:
<!DOCTYPE html>
The DOCTYPE should be the very first thing in your HTML before <html>, before <head>, before <body> and before any comments.
Why Does DOCTYPE Matter?
Even if your page “looks fine” today, DOCTYPE can cause subtle problems that turn into bigger QA issues later.
- It helps browsers render the page correctly - With the right DOCTYPE, browsers generally use a modern standards-based rendering approach (Standards Mode). Without it, some browsers may fall back to Quirks Mode behaviors.
- It reduces cross-browser layout surprises - DOCTYPE issues can show up as annoying inconsistencies like unexpected spacing and margins, differences in box sizing behavior, table and form layout weirdness, font sizing differences
Browsers typically choose either between "Standards mode" or "Quirks mode". The standards mode supports modern rendering rules while the quirks mode is legacy behaviors meant to support older web pages.
A correct HTML5 DOCTYPE strongly encourages Standards Mode.
FAQs
Without a Doctype, browsers might use 'quirks mode,' leading to varied displays based on their interpretation of the content.
Yes, for HTML5 documents, you should declare the Doctype (<!DOCTYPE html>) to inform browsers of the document's version.
While XML can have a Doctype, it usually defines data structure and type. In XML, Doctype may reference a DTD (Document Type Definition) for structure validation.
The doctype should always be at the start of an HTML document, preceding the <html> tag.
No, a document should have only one Doctype declaration. Having more can lead to errors or unexpected behaviors.