DOCTYPE - DTD - Document Type Declaration
The DOCTYPE Declaration (DTD or Document Type Declaration), what it does and why each web page needs it. HTML validation requires the DOCTYPE declaration (DTD). The browsers will go into Quirks mode if a DOCTYPE declaration (DTD) is not included.
What Does DOCTYPE Declaration (DTD) Stand for?
DOCTYPE Declaration is the abbreviation for Document Type Declaration (DTD).
What Does the DOCTYPE Declaration (DTD) Do?
The DOCTYPE Declaration (DTD or Document Type Declaration) does a couple of things:
- When performing HTML validation testing on a web page it tells the HTML (HyperText Markup Language) validator which version of (X)HTML standard the web page coding is supposed to comply with. When you validate your web page the HTML validator checks the coding against the applicable standard then reports which portions of the coding do not pass HTML validation (are not compliant).
- It tells the browser how to render the page in standards compliant mode.
Which DOCTYPE Declaration Should I Use?
If you are a beginner to HTML (HyperText Markup Language) then I would suggest you use the HTML 4.01 Transitional declaration. It is much more forgiving for the beginner when performing HTML validation. You would also use this declaration if you know that your audience will not have a browser that supports CSS (Cascading Style Sheets).
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
This DOCTYPE declaration still allows elements and presentation attributes that should be in the stylesheet.
If you want to learn to code in preparation for the future but still not ready for XHTML then you would use the Strict declaration.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
With this declaration all presentation attributes are moved to the stylesheet.
A complete list of DOCTYPE Declarations (DTDs) is available at List of valid DTDs you can use in your document.
What Happens If the DOCTYPE Declaration (DTD) is Not Included or is Incorrect?
If the web page coding does not include a DOCTYPE Declaration (DTD or Document Type Declaration) or it is done incorrectly:
- You will not be able to use a HTML (HyperText Markup Language) Validator to check the page coding. HTML validation requires the DOCTYPE declaration.
- The browser rending the web page will process the coding in Quirks Mode.
- The stylesheet may not be implemented as planned.
Using the correct DOCTYPE Declaration (DTD or Document Type Declaration) is important. Compliant browsers have the technology to switch between Standards and Quirks mode. If the correct DOCTYPE Declaration is used the browser switches to Standards mode.
Compliant browsers have 2 modes, Quirk and Standard.
- Quirk mode renders the page as it would appear in older browsers. If there is no DOCTYPE Declaration the browser will go into Quirk mode.
- Standards mode will render the page as intended if the web page coding is valid.
If you are interested in how each browser reacts to the different DOCTYPE Declarations there is a table available at Doctypes and their respective layout mode
To see an example of how a page will be rendered in each mode 'Standards' vs 'Quirks' modes Note: This example is for XHTML but it still demonstrates what happens when the browser uses each mode.
To learn more about HTML code validation:
- Code Validation - Clean Code - What is Code Validation? - Why Validate Your Code? Why Code Validation-Clean Code is Important - Code Validation from the Accessibility Point of View - Code Validation from a Search Engine Optimization Point of View
- Validators - HTML Code Validators - What HTML Code Validators Do - HTML Code Validators Available - Using the W3C's Validation Services - My Page Won't Validate! - "Fatal Error: No DOCTYPE specified!"