11

Possible Duplicate:
What's up, Doctype?

Adding DOCTYPE is a best practice -- HTML validators expect to see it. In theory, it makes the document better by declaring which flavor of HTML is being used. In other words, it is something you should always do.

That said, I don't see any cases where browsers seem to use it. I've tried multiple flavors of HTML in various browsers and am unable to find a single example where the document renders differently when DOCTYPE is added.

Does anyone know of a case where the DOCTYPE has any real effect in a browser?

This question is different from What is DOCTYPE? which does not ask for specific examples where the presence of DOCTYPE has an observable effect in a browser.

Community
  • 1
  • 1
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
  • 1
    Activating browser modes with doctype: http://hsivonen.iki.fi/doctype/ – Rob Aug 25 '12 at 18:05
  • @JukkaK.Korpela "What is a DOCTYPE?" is a very different question from "Are there any known cases where using DOCTYPE has an observable effect?". – Raymond Hettinger Aug 25 '12 at 22:35
  • @Raymond Hettinger, the question carries a jocular heading “What’s up, Doctype”, but the question and answers to it cover well the “real effect” issue. Take a look at the question itself (beyond the title) and the answers. – Jukka K. Korpela Aug 25 '12 at 22:41

2 Answers2

4

The tag itself has a big effect, mainly for Internet Explorer, as it makes it render the page in a way closer to the norm (if the document doesn't start with the doctype declaration, IE enters quirks mode).

There are many cases when simply setting the doctype fix bad rendering in IE8 or IE9.

This being said, the precise doctype should now not be specified and, as you probably follow the HTML5 norm, you should simply set

<!DOCTYPE html> 

(see http://webdesign.about.com/cs/doctype/a/aaquirksmode.htm)

An additional tip :

IE doesn't really follow HTML5 norm as much as it could, except when you insist with a complementary meta header :

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />

(see IE9 Float with Overflow:Hidden and Table Width 100% Not Displaying Properly)

Community
  • 1
  • 1
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Thank you. I tried viewing documents with and without DOCTYPE in a number of browsers but not IE. It would seem to matter the most for IE and no so much with other browsers. – Raymond Hettinger Aug 25 '12 at 22:31
3

Yes, it does have a real effect: it changes the rendering mode of the browser. Most browsers have two rendering modes: standards mode and quirks mode. Some browsers have a third rendering mode usually called "almost standard".

  • Not adding a DOCTYPE forces browsers to go into quirks mode
  • Adding a strict DOCTYPE makes the browser go into standards mode
  • Some browsers go into "almost standards" mode when you use a transitional DOCTYPE

When in quirks mode, browsers mimic old versions of themselves and render boxes, alignments, etc differently. "Almost standards" mode is standards mode with a different behavior for images in tables to mimic what we used to do in old HTML to align stuff in tables using transparent images.

You can read more about this in this Mozilla Developer Network article: https://developer.mozilla.org/en-US/docs/Quirks_Mode_and_Standards_Mode?redirectlocale=en-US&redirectslug=Mozilla%27s_Quirks_Mode

juandopazo
  • 6,283
  • 2
  • 26
  • 29