Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does DOCTYPE have any real effect? [duplicate]

Tags:

html

doctype

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.

like image 986
Raymond Hettinger Avatar asked Aug 25 '12 17:08

Raymond Hettinger


2 Answers

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)

like image 111
Denys Séguret Avatar answered Oct 20 '22 08:10

Denys Séguret


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

like image 29
juandopazo Avatar answered Oct 20 '22 08:10

juandopazo