Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need a doctype? (What does it do) [duplicate]

Tags:

html

doctype

Possible Duplicate:
HTML: What is the functionality of !DOCTYPE

I recently asked a question here and the solution was a simple:

You need to add a doctype to the page. This should fix the issue for you.

Now, my pages work fine in every browser without the doctype (except IE). Does IE need a doctype (is this an IE only thing) and do other browsers just assume it OR or is it doing something I'm not seeing.

What are its functions and how does it work?

like image 392
Freesnöw Avatar asked May 20 '11 18:05

Freesnöw


People also ask

Why do I need a DOCTYPE and what does it do?

The DOCTYPE declaration is an instruction to the web browser about what version of HTML the page is written in. This ensures that the web page is parsed the same way by different web browsers. In HTML 4.01, the DOCTYPE declaration refers to a document type definition (DTD).

What happens if I don't provide a DOCTYPE?

The absence of the DOCTYPE or its incorrect usage will force the browser to switch to quirks mode. It means that the browser will do its best to layout the page that is considered to be old or created against web standards.

What is the purpose of a DOCTYPE in your HTML documents?

Doctype HTML is a declaration that tells the browser what version of HTML the document is written in. This declaration appears as the very first line in an HTML file.

Do you need a DOCTYPE?

All HTML need to have a DOCTYPE declared. The DOCTYPE is not actually an element or HTML tag. It lets the browser know how the document should be interpreted, by indicating what version or standard of HTML (or other markup language) is being used.


2 Answers

All browsers need the doctype. Without the DOCTYPE you are forcing the browsers to render in Quirks Mode.

However, DOCTYPE was only partially used by the browsers in determining dialect and parsing, even though that was the purpose. This is why HTML5 has reduced the DOCTYPE to simply:

<!DOCTYPE html>

2.2. The DOCTYPE

The HTML syntax of HTML5 requires a DOCTYPE to be specified to ensure that the browser renders the page in standards mode. The DOCTYPE has no other purpose and is therefore optional for XML. Documents with an XML media type are always handled in standards mode. [DOCTYPE]

The DOCTYPE declaration is <!DOCTYPE html> and is case-insensitive in the HTML syntax. DOCTYPEs from earlier versions of HTML were longer because the HTML language was SGML-based and therefore required a reference to a DTD. With HTML5 this is no longer the case and the DOCTYPE is only needed to enable standards mode for documents written using the HTML syntax. Browsers already do this for <!DOCTYPE html>.

Source: HTML5 differences from HTML4: DOCTYPE

like image 71
Kevin Peno Avatar answered Oct 08 '22 02:10

Kevin Peno


The Doctype does two things.

  1. It identifies which dialect of HTML you're using.
  2. It controls whether the browsers uses "standards" or "quirks" mode to render the document.

If there is no doctype, or there's an unrecognized one, then it uses "quirks" mode and interprets the document as best it can. If there IS a doctype, and it recognizes it, then it follows the standards. The results of the rendering can vary depending on how it interprets the document.

like image 30
Will Martin Avatar answered Oct 08 '22 03:10

Will Martin