I know that different doctypes are essentially about how compliant the html is, but what difference does it make what doctype you specify? Do browsers handle the same code differently depending on the doctype? Thanks
UPDATE - most answers mention quirks mode can be set off if no doctype is specified. But what would be the different between xhtml and html 4.01?
The declaration is not an XHTML tag; it is an instruction to the web browser about what version of the markup language the page is written in.
The declaration refers to a Document Type Definition (DTD). The DTD specifies the rules for the markup language, so that the browsers render the content correctly.
The biggest thing is having a doctype or not. If you don't, the browser will work in a "quirks" mode rather than standards mode and many things will be slightly different. If you have one — any — that typically activates more standards-compliant behavior in the browser.
See this article for the details of what doctypes do on various different browsers and what modes — quirks, standards, almost-standards, etc. — different browsers have. Quoting a relevant section:
...but see the article for a full discussion.
The doctype declaration should be the first thing in an HTML document, before the tag.
It isn't an HTML tag; it's an instruction to the web browser about what version of the markup language the page is written in.
It's getting simpler with HTML5:
<!DOCTYPE html>
If you don't have that proper doctype, the browser won't know to use HTML5.
It's all about the standards and yes, browsers handles code differently. That means, that all browsers should display the page equally. If no standard is specified, browser will interpret the page as it wants.
Because Doctype is the flag to tell how the browser should handle the page.
For example :
HTML5 need this doctype
<!DOCTYPE html>
If you remove this from the page, the any HTML5 capabilities inside your page won't be activated.You can read more in http://www.w3.org/QA/Tips/Doctype
Back in the past, Browsers implemented CSS to their own rules.
Only over the years have Browser now adapted the W3C standards.
To make sure that websites rendered correctly various browsers, web developers had to implement CSS according to the wishes of these browsers. Thus, most websites used CSS in ways that didn’t quite match the specifications.
Therefore, when standards compliancy became important browser vendors faced a tough choice. Moving closer to the W3C specifications was the way to go, but if they’d just change the CSS implementations to match the standards perfectly, many websites would break to a greater or lesser extent. Existing CSS would start to show odd side effects if it were suddenly interpreted in the correct way.
So moving closer to standards compliance would cause problems. On the other hand, not moving closer to standards compliance would perpetuate the general confusion of the Browser Wars Era.
To this end all Browser had to start supporting both modes. Quirks mode for older designs and standard mode for new design.
Paraphrased from here: Quirks mode and strict mode
Choosing which mode to use requires a trigger, and this trigger was found in ’doctype switching’. According to the standards, any (X)HTML document should have a doctype which tells the world at large which flavour of (X)HTML the document is using.
Taken from here too: Quirks mode and strict mode