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

HtmlDoctype

Html Problem Overview


> 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?

Html Solutions


Solution 1 - Html

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

Solution 2 - Html

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.

Solution 3 - Html

> Why? > > Why specify a doctype? Because it > defines which version of (X)HTML your > document is actually using, and this > is a critical piece of information > needed by some tools processing the > document. > > For example, specifying the doctype of > your document allows you to use tools > such as the Markup Validator to check > the syntax of your (X)HTML. Such tools > won't be able to work if they do not > know what kind of document you are > using. > > But the most important thing is that > with most families of browsers, a > doctype declaration will make a lot of > guessing unnecessary, and will thus > trigger a "standard" rendering mode.

Source: http://www.w3.org/QA/Tips/Doctype

Solution 4 - Html

You should have a DOCTYPE for ANY browser. It tells the browser how to interpret the html and css. This is why html4 and html5 have different definitions (as does xhtml). All very important for validation.

What IE will do is put the document into what it calls 'quirks mode' which basically ignores a whole heap of rules for how CSS should (by modern definitions) behave. Here is a good summary of the issue. It harks back to the bad old days of non-standardised CSS support

Solution 5 - Html

Browsers need at the least to render in what is known as standards mode. See John Resig's article on the html 5 doctype: http://ejohn.org/blog/html5-doctype/. Now if you want your browser to not use standards and render like its 1990 go ahead and not add anything and you will see floats and other now standard items not work correctly. If you want to have your page render/validate in accordance to a particular standard then you would want to add more to the doc type but it is not necessary.

Solution 6 - Html

From W3Schools, a doctype is "an instruction to the web browser about what version of the markup language the page is written in." (http://www.w3schools.com/tags/tag_doctype.asp)

If you do not include the doctype, the browser may assume you are using a different language than you really are, causing it to be rendered incorrectly.

Solution 7 - Html

From W3Schools.com:

> The doctype declaration is not an HTML > tag; it is an instruction to the web > browser about what version of the > markup language the page is written > in.

There are a handful of different doctypes, and changing them can drastically change how your page renders.

Solution 8 - Html

> The doctype declaration should be the > very first thing in an HTML document, > before the tag. > > The doctype declaration is not an HTML > tag; it is an instruction to the web > browser about what version of the > markup language the page is written > in. > > The doctype declaration refers to a > Document Type Definition (DTD). The > DTD specifies the rules for the markup > language, so that the browsers render > the content correctly.

Reference

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFreesn&#246;wView Question on Stackoverflow
Solution 1 - HtmlKevin PenoView Answer on Stackoverflow
Solution 2 - HtmlWill MartinView Answer on Stackoverflow
Solution 3 - Htmlonteria_View Answer on Stackoverflow
Solution 4 - HtmlStuart BurrowsView Answer on Stackoverflow
Solution 5 - HtmlscrappedcolaView Answer on Stackoverflow
Solution 6 - HtmlGeorge CumminsView Answer on Stackoverflow
Solution 7 - HtmlKen PespisaView Answer on Stackoverflow
Solution 8 - HtmlbreezyView Answer on Stackoverflow