What does a doctype do?
doctype is an abbreviation for Document Type. A doctype is always associated with a DTD — short for Document Type Definition.
A DTD defines how documents of a certain type should be structured (i.e. a button can contain a span but not a div), whereas a doctype declares what DTD a document supposedly follows (i.e. this document follows the HTML DTD).
For webpages, the doctype declaration is required. It is used to tell user agents what version of the HTML specification your document follows. Once a user agent has recognized a correct doctype, it will trigger the no-quirks mode matching that doctype for reading the document. If a user agent doesn't recognize a correct doctype, it will trigger quirks mode.
The doctype declaration for the HTML5 standard is <!doctype html>.
Why it matters
Without doctype:
- Box model calculations differ
- CSS rendering inconsistencies
- JavaScript behavior variations
- Layout problems across browsers
With proper doctype:
- Predictable CSS box model
- Consistent rendering
- Modern HTML5 features work properly
- Better cross-browser compatibility