HTML and CSS   XSLT   JavaScript   Images   Soft   Etc  
Andrey Lechev

Document type: To declare or not to declare? 22 November 2005


If you ever viewed page source you might have noticed that it often starts with <!DOCTYPE… >. This is what is called Document Type Declaration (not to confuse with DTD which is Document Type Definition).

According to authors of advanced HTML guides, automatic validators and Jeffrey Zeldman using document type declaration is a proper thing to do. But many websites can be viewed just fine without it which makes us wonder: “Do we really need <!DOCTYPE>?” Lets figure this out.

Where did it come from?

Document type declaration could be seen in the earliest HTML version that didn’t have a number and was called HTML Internet Draft. Back then there already existed several markup languages (TEX, PostScript and SGML) that were used mainly for scientific documents (it is not surprising, because we know that the web was created at the European Organization for Nuclear Research as a combination of simple hypertext and an even more simple protocol).

Those languages were complicated, so the developers approached the task of making life of people and graphic terminals easier by simplifying markup. And they came up with HTML based on Standard Generalized Markup Language (SGML).

It was SGML that required document type declaration. The first HTML 2.0 official specification said that documents should start with the <!DOCTYPE> declaration and HTML 3.2 made it a must.

However, no browser of the time asked for explicit document type declaration to display an HTML page. And since browsers did not need it, developers didn’t care either and so they ignored <!DOCTYPE> (to be fair, I mean the developers who wrote scripts “by hand” not using HotDog).

Standards and backward compatibility

The first browser to pay attention to document type was Internet Explorer 5.0 for Mac OS (Tasman layout engine). It appeared in early 2000 and instantly became the best as to supporting HTML standards (to those who bother: Mozilla was nowhere near version 1.0 and Opera was hopeless as it kept copying MSIE for Windows).

At that moment no one needed a browser capable of rendering HTML according to standards, because the standards disagreed with the actual way browsers handled HTML scripts.

As a result Internet Explorer 5.0 for Mac OS would not display pages the way they were intended, so the situation needed a special switch to specify whether a browser could go on and render HTML or had to do it in compliance with standards.

It was document type declaration to serve the purpose. The rule was simple—if the type was not declared or it was declared but didn’t include DTD (this time it is Document Type Definition), then HTML page was rendered like older browsers used to do it (in quirks mode) or, if DTD definition was found, according to HTML 4.01 and CSS 1 standards.

That sparked a new era, the ear of fighting for standard-compliant scripts. Complaints appeared—about fonts in nested tables being too small, about websites falling apart because of weird box models, about mysterious gaps between images (Forgive me, Mozilla) and so on. It all was due to developers’ careless attitude to the wonderful <!DOCTYPE> declaration.

Internet Explorer 6.0 for Windows legalized switching between the modes, and today almost all browsers enable standards and quirks modes. Mozilla and Safari went a step further— they have an ‘almost standards’ mode which matches Internet Explorer 6.0 standards mode. But that’s a whole different story…

To define or not to define?

If you intend to use <!DOCTYPE>, be sure to bring you HTML into compliance with standards with no exceptions. Or you can allow exceptions, if you aim at standards rendering mode. The rest of the time you don’t need <!DOCTYPE>. Trust me.


Order a design...