Wednesday, 14 April 2010

What HTML DOCTYPE to choose?



Intro


If you don't know what DOCTYPE is (http://en.wikipedia.org/wiki/Document_Type_Declaration) - or even worse, don't use it in your HTML documents ... well, you should. When DOCTYPE is not present, some browsers are triggered to the quirks mode and then they render web page in a different way. So, in order to get your HTML page to look and behave similar/same in cross-browsers way, you should use DOCTYPE.



Don't use XHTML


How to choose right DOCTYPE? If you haven't noticed, I haven't put XHTML in the title. You should avoid using XHTML. Although it has some advantages, it's not suitable for web use and many people are not using it properly. First of all, XHTML should be served as XHTML and not as HTML as many people do. This means, HTTP header "Content-Type" should be "application/xhtml+xml" and not "text/html". If you serve XHML page as "text/html", browsers will understand that as malformed HTML (tag soup) and will do corrections. In some cases, this can trigger browser's quirks mode and then you'll probably have WTF-moment, especially if your page is validated. But if you do serve XHTML as "application/xhtml+xml", you'll have one more WTF-moment when you see page in Internet Explorer since it does not understand XHTML's content type (feel free to try that). So, you could loose a lot of potential users if you start using XHTML as it should be used.

There are more reasons why you should not use XHTML, but they are not so important right now. What is important is - what if you are already using XHTML (served as HTML)? Well, you shouldn't correct it because it would take you time and the benefits are few (and are not so great). You should wait for HTML 5 and than do corrections to your code if necessary. If I remember correctly, XHTML 1 will be (almost) valid HTML 5, so your transition won't be time consuming.



Use HTML Strict


So, what's left to use? There are three DTDs (http://en.wikipedia.org/wiki/Document_Type_Definition) for HTML:

  • Strict
  • Transitional
  • Frameset

HTML Strict DOCTYPE does not allow mixing of style and markup. You should use this DOCTYPE as it has many benefits from which some are: smaller markup, cache benefit if you use external style sheet, easier maintenance and style modification, etc.

HTML Transitional DOCTYPE allows some older elements and attributes which are deprecated. By that, I mean it allows mixing of markup and styles. I see many new web sites using this DOCTYPE (probably, web masters just copy DOCTYPE from some other web site, not knowing what does it mean). But they shouldn't since this DOCTYPE should be used for transition (as it's name suggests) to strict DOCTYPE (and new sites are transitioning from what? :) ).

And last DOCTYPE is for documents that use frames. I actually never used it. :)



That's all for this post. I hope more people will start to use proper DOCTYPE. Unfortunately, I can't change DOCTYPE on this blog, so I'm doomed to using XHTML (if someone has wondered why I'm using XHTML and speak against it).



Have a nice day. :)

No comments:

Post a comment