Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does <!doctype html> do?

Tags:

html

doctype

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?

like image 601
Niklas Rosencrantz Avatar asked Oct 08 '11 05:10

Niklas Rosencrantz


People also ask

Is DOCTYPE HTML really necessary?

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.

What does the DOCTYPE tell you?

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.


2 Answers

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?

Seatbelt image

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.

like image 53
zzzzBov Avatar answered Nov 04 '22 05:11

zzzzBov


DOCTYPE Declaration is the abbreviation for Document Type Declaration (DTD).

The DOCTYPE Declaration (DTD or Document Type Declaration) does a couple of things:

  1. 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).

  2. 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:

  1. You will not be able to use a HTML (HyperText Markup Language) Validator to check the page coding. HTML validation requires the DOCTYPE declaration.

  2. The browser rendering the webpage will process the coding in Quirks Mode.

  3. 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.

like image 21
Random Guy Avatar answered Nov 04 '22 07:11

Random Guy