What does this tag do?
<!doctype html>
I can see that with this tag on top, my HTML behaves in another way than without.
How do I know if I need this tag?
Even though a doctype may look a bit strange, it is required by the HTML and XHTML specifications. If you don't include one you will get a validation error when you check the syntax of your document with the W3C Markup validator or other tools that check HTML documents for errors.
The doctype explains what type of HTML is to be expected and therefore what spec validators (for example the W3C HTML validator) should validate your document against.
It's an integral part of HTML as defined in the specification:
8.1.1 The DOCTYPE
A DOCTYPE is a required preamble.
DOCTYPEs are required for legacy reasons. When omitted, browsers tend to use a different rendering mode that is incompatible with some specifications. Including the DOCTYPE in a document ensures that the browser makes a best-effort attempt at following the relevant specifications.
Edit to add:
What does this seatbelt do?
What I can see is that, with this seatbelt on, my car behaves the same way as without. How do I know if I need this seatbelt?
You wont know if you'll need it until something goes wrong and you don't have it.
DOCTYPE Declaration is the abbreviation for Document Type Declaration (DTD).
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.
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 rendering the webpage will process the coding in Quirks Mode.
The stylesheet may not be implemented as planned.
Which DOCTYPE should you 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 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
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" "http://www.w3.org/TR/html4/strict.dtd">
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With