with Imagination: by Dustin Diaz

./with Imagination

A JavaScript, CSS, XHTML web log focusing on usability and accessibility by Dustin Diaz

The Skinny on Doctypes

Monday, November 24th, 2008

Update: For the record, this is the standard forward compatible HTML 5 doctype. This should always work.

Doctypes have long been in standardista discussions circles. Why to use them. Which one to use. Which one is best. These are all <sarcasm>really fun details</sarcasm> to get into, but the most important aspect of any doctype is simply having one, since without one, you’re stuck in the lovely world of “quirks mode”. If you’re interested in understanding the anatomy of a doctype, then by all means, dive in.

At Google, we have this obsession with byte size. And by byte size, any “lack-therof” the better. Therefore the doctype you will find on most Google webpages while still triggering the browser into “standards mode” is as simple as you see below:

The skinny doctype

<!doctype html>

There is really, absolutely no reason you need the rest of the doctype in your declaration unless you’re validating code. Furthermore, it does not mean that your page is even invalid. In the end, it puts your webpages into standards mode, which is what really matters. (Plus it’s easy to memorize ;)

Try it out. It will fix your box model in IE6 and clobber all those other funny gotchas when you’re in quirks mode. Cheers.

Update: David was kind enough to share his results using the skinny doctype on his blog. The results, as expected, keep your browser tame.

45 Responses to “The Skinny on Doctypes”

  1. Luke Dorny

    Hmmm. Mental note.

  2. Andrew Jaswa

    I’m guessing that it wouldn’t matter if it’s html or xhtml? Since you wouldn’t be validating it?

  3. Dan

    Very interesting viewpoint; I still attempt to use a full doctype which best describes the content being served on the page instead of saving bytes… but I like your view on this.

    2 posts in one week, are you feeling ok? Kidding aside, I hope to see more posts here.

  4. Dustin Diaz

    @Luke: At least it’s a simple mental note. Memorizing the doctype in this case is pretty easy :)

    @Andrew: Doesn’t matter. the second argument in the doctype declaration defines the root element of your document. Therefore, in both cases of HTML & XHTML, the root element is always <html>

    @Dan: Haha. Yeah. 2 posts in a week. Amazing isn’t it. There’s only one way to get back in, and that’s full force ahead.

  5. Apostolos Tsakpinis

    The skinny DOCTYPE enables full strict mode in modern browsers causing all IMG tags to be rendered inline, which means that some space should be reserved for possible descender characters like g, j, or q. This causes a noticeable bottom margin below images. Open the following pages side-by-side to see the difference:

    http://www.zdimensions.gr/apostolos/doctype/html4.htm
    http://www.zdimensions.gr/apostolos/doctype/html5.htm

    You can fix this behavior by using img { display: block; }, so I guess it's just a minor a inconvenience. Other DOCTYPEs enable "almost strict mode" where images are rendered conventionally.

  6. Edward O'Connor

    Of course, you’re also setting yourself up for forwards compatibility, as that will be the doctype of HTML 5. Yay!

  7. Apostolos Tsakpinis

    The skinny DOCTYPE enables full strict mode in modern browsers causing all IMG tags to be rendered inline, which means that some space should be reserved for possible descender characters like g, j, or q. This causes a noticeable bottom margin below images. Open the following pages side-by-side to see the difference:

    http://www.zdimensions.gr/apostolos/doctype/html4.htm
    http://www.zdimensions.gr/apostolos/doctype/html5.htm

    You can fix this behavior by using img { display: block; }, so I guess it’s just a minor a inconvenience. Other DOCTYPEs enable “almost strict mode” where images are rendered conventionally.

  8. Dustin Diaz on DocTypes | Garrick Cheung

    [...] just had to share this one with you as soon as I read it. Dustin Diaz shows us a way of declaring a doctype, that puts the browser is standards mode. Thought it won’t validate, which he explains: There [...]

  9. Michael(tm) Smith

    validator.nu developer Henri Sivonen’s “Activating Browser Modes with Doctype” at http://hsivonen.iki.fi/doctype/ is worth citing in addition to or instead of the about.com page you cite. It’s worth noting that Henri doesn’t recommend that most sites switch to using the doctype yet.

  10. Michael(tm) Smith

    …I meant to say, It’s worth noting that Henri doesn’t recommend that most sites switch to using the HTML5 doctype yet (that is, the short form you give an example of in your post).

    Also maybe worth noting that all the normally recommended doctypes in use now (e.g., the ones that Henri gives in his article) are also forwards-compatible — in that nothing HTML5 specifies is going to break any of the existing browser behavior in regard to them.

  11. Chris Tokar

    The chart on
    http://en.wikipedia.org/wiki/Quirks_mode
    shows that browsers switch into either quirks, standard or “almost standards mode” (”Almost standards mode” is non-standard table cell height rendering) based on which doctype is used. Are we sure this would switch all browsers into full standards mode?

  12. Chad Lindstrom

    Nice! This is actually a very useful tip!! Thanks

  13. links for 2008-11-25 | Amasijo

    [...] The Skinny on Doctypes <!doctype html> (tags: standards html doctype) [...]

  14. Baki

    Interesting approach. Wouldn’t that defeat the whole purpose of validating. I’l give it a try on one of my projects to see.

  15. Marc Grabanski

    Doctype freedom! Thanks Dustin for setting me free.

  16. Jay

    Love it. I am going to use it for sure!

  17. Adrian Cooke

    Michael(tm) Smith mentioned Henri Sivonen, and I’d add that you can validate against the emerging HTML5 spec using Sivonen’s validator, http://html5.validator.nu/ so if you dump all that HTML4/XHTML1 header cruft and switch to using you can still check your pages, and little else in your markup needs to change (some minor HTML5-specific things).

  18. Karl Swedberg

    Yep, this is basically the HTML 5 doctype. Among others, John Resig wrote about this back in January.

    Also, I noticed earlier this week that the W3C Validator is now validating HTML 5:

    Using experimental feature: HTML5 Conformance Checker.

    The validator checked your document with an experimental feature: HTML5 Conformance Checker. This feature has been made available for your convenience, but be aware that it may be unreliable, or not perfectly up to date with the latest development of some cutting-edge technologies. If you find any issue with this feature, please report them. Thank you.

    Pretty cool.

  19. Andrew Rowls

    @Baki: the purpose of validating is to ensure that your code is valid according to a specific doctype. Shocking, I know :P

    But valid code doesn’t mean “proper” rendering, and “improper” rendering doesn’t mean invalid code. According to the wikipedia article Chris posted, this skinny doctype will make most modern browsers use “standards mode” and will make IE use “almost-standard mode”

    Basically, if I’m reading this right, the skinny doctype will standardize some/most element behaviors across browsers without tying you down to a specific language’s syntactic rules. Though I’m not sure what the implications are for running the W3C’s validator on such a page.

    It is worth noting, though, that the HTML 4.01 Strict doctype has the same effect on the major browsers, with the exception of putting NS6 in standards mode rather than quirks mode.

  20. FreePress Blog » GR Shared Link - The Skinny on Doctypes

    [...] The Skinny on Doctypes [...]

  21. Useful Links #4 | Gilbert Pellegrom

    [...] The Skinny on Doctypes Google have decided to go skinny with their Doctypes. [...]

  22. Ajaxian » <!doctype html>

    [...] is what HTML 5 defines, and Dustin Diaz agrees as he lays down the skinny: Doctypes have long been in standardista discussions circles. Why to use them. Which one to use. [...]

  23. Turns out doctypes in HTML are, practica … « Paul M. Watson

    [...] | # | Tags: dev, web Turns out doctypes in HTML are, practically, a lot simpler than I thought. This is used over at Google and apparently it will be used in HTML5. No more templates or copying and pasting from previous HTML. [...]

  24. <!doctype html> | Eroarea 403

    [...] is what HTML 5 defines, and Dustin Diaz agrees as he lays down the skinny: Doctypes have long been in standardista discussions circles. Why to use them. Which one to use. [...]

  25. Javascript News » Blog Archive » <!doctype html>

    [...] is what HTML 5 defines, and Dustin Diaz agrees as he lays down the skinny: Doctypes have long been in standardista discussions circles. Why to use them. Which one to use. [...]

  26. All you need is - David’s Web Development Blog

    [...] Dustin Diaz at Google, taking into account the fact that “every byte matters”, chopped down the doctype to the following: [...]

  27. David Hammond

    “Furthermore, it does not mean that your page is even invalid.”

    This depends on your definition of “valid”. According to the traditional definition — conformance to an SGML profile and DTD — then sorry, it’s invalid. Practical, but invalid.

    Now, if you’re talking about conformance with HTML 5, that’s another story. Unlike all previous versions of HTML and XHTML, HTML 5 isn’t built on the SGML standard, and it therefore carries a different definition of the word “valid”. But then, HTML 5 is still very much a work in progress, and I think it’s a bad idea to promote the general use of any technical specification that’s still in the drafting stages.

  28. Dustin Diaz

    David,
    the question that is being begged here is the question of validity itself… not the definition of validity. When people see this doctype, people wonder if their page will still validate, which is why I brought it up.

    Also, on what grounds is it a bad idea?

  29. Il Doctype alla Google | Fidicaro.net

    [...] fa questo doctype? Stando alle parole di Dustin Diaz, fa quello che deve. Mette i browser in modalità standard. Ovviamente il [...]

  30. Scott Galloway

    Interesting…I’ve been setting HTML 4.01 strict for awhile now with good results, but skinny seems to be sufficient for pretty much all browsers (sans NS6, maybe Konqueror) that I care to test.

    I’m not sold that saving 75 or so characters on each page is all that valuable, but if it triggers similar behavior, it doesn’t sound like it would hurt to skinny-ify my doctype. I do however still worry about what effects making this change now might have on how browsers want to render my pages (which are widely variable in terms of both content and structure (unfortunately)).

    Really, until I see a lot more test results, and groups like W3C, WASP, etc start recommending it’s use, I’ll be conservative and leave things as HTML 4.01 strict.

  31. David Hammond

    The question of validity depends on the definition of validity, unless you’re just talking about whether people are allowed to put the “look ma, it validates!” sticker on their page. A document using your suggestion does not conform to any currently existing technical standard or W3C recommendation, so I think it’s very questionable to state that it is valid.

    As for premature use being a bad idea, I think this touches on a fundamental reason standards are valuable in the first place: they provide a static set of requirements and expectations. The key word there is “static”. Yes, a standard may evolve over time, but it generally does so in a series of milestones that don’t contradict each other. But the HTML 5 standard isn’t finalized yet and won’t be until at least 2010 (probably later, considering how these things usually go). A lot can change between now and then, and there is definitely no guarantee that the final standard will be compatible with your recommendation.

    This isn’t just a practical issue for web developers (”What if the finalized standard conflicts with what I’ve been doing?”). It’s also an issue for the developers of that standard. They have a certain expectation of freedom in the drafting process, in order to eventually develop what they believe is the best standard they can make. If people start implementing those drafts prematurely, the “real world support” forces may end up tying the drafters’ hands.

    One could argue that widespread use of a drafted syntax or feature testifies that the syntax or feature was well-designed and shouldn’t change, but I think that’s a narrow-sighted view. The Web is a diverse place, and I don’t think a (relatively) small ambitious group of early adopters should tie the hands of the specification drafters who are trying to create the best standard possible for the entire Web.

  32. Jens Meiert

    Feeling free to generally reply to some comments: Yes, you can use HTML 5.

  33. Max Design - standards based web design, development and training » Some links for light reading (2/12/08)

    [...] The Skinny on Doctypes [...]

  34. Richard Filing » Good enough for Google - The skinny Doctype

    [...] Russ at Maxdesign a short and sweet window to the minimalist doctype identifier from Dustin Diaz, a Google user interface engineer. His [...]

  35. Daniel Shaw

    Wow, thanks. Didn’t even know that form existed or was valid. I hadn’t noticed it in the source.

  36. Jon Wiley

    As David says, your page isn’t valid of you use that “doctype”. Your markup might be, but your page isn’t. I’m not objecting to using it, but claiming your page is valid is incorrect.

    It also seems like a hassle to use a real doctype when validating and then have to switch over to this when you publish it. Are the bytes really worth the time?

  37. Jodi Suguitan

    Thanks for clearing that up. I was never really sure what the significance was of the doc type.

  38. Dave Johnson

    Great post. Totally agree that the only reason you actually need a doctype is to keep out of quirks mode.

  39. Zach Leatherman

    Noticed that the Google Search results page specifies the short doctype, but doesn’t include an <html> node. Any reason why?

  40. Dustin Diaz

    Zach — the fact that it still works is probably why it’s used…. not that I personally recommend that bit.

  41. bradt.ca » Preventing IE6 bugs

    [...] Dustin Diaz recently pointed out, the following is all that is needed to make sure all browsers render the page in standards mode, [...]

  42. Wait till I come! » Blog Archive » Will a new browser war help web innovation?

    [...] tweaking and saving bytes wherever we can. However you can overdo that. As Dustin Diaz explained Google are using <!doctype html> as their doctype to save on some bytes and David Calhoun proved that it is working across the [...]

  43. Daniel Shaw

    Any idea why google.com dropped the doctype again?

  44. HTML DocType Text Made Easy: World’s Tiniest HTML DocType DTD Declaration | Make A Website

    [...] Dustin Diaz made an interesting discovery to trigger “standards mode” with just a tiny little declaration instead of the regular lengthy one we see everywhere. [...]

  45. sigara b?rakma

    i think if you doesn’t know about doctypes this will not hurt , but if you can perform some true operations about doctypes,it will be better

Get "JavaScript Design Patterns"

"As a web developer, you'll already know that JavaScript™ is a powerful language, allowing you to add an impressive array of dynamic functionality to otherwise static web sites. But there is more power waiting to be unlocked--JavaScript is capable of full object-oriented capabilities, and by applying OOP principles, best practices, and design patterns to your code, you can make it more powerful, more efficient, and easier to work with alone or as part of a team."

Buy JS Design Patterns from Amazon.com Buy JS Design Patterns from Apress

All content copyright © 2003 - 2009 under the Creative Commons License.

Archives | Blog Search