Photograph of an iPhone and MacBook from above on a desk with books, pads and pencils

Specifying the correct doctype of your web page is very important. Without it, the client (the web browser, iPhone, etc) will have no idea which version of HTML you are using to write the document, and will default to guessing how the HTML should be rendered. In many browsers, this is known as “Quirks” mode, and can result in pages displaying oddly. By specifying the doctype, we tell the client “try to render this document according to the standards of this specification”. That information is added at the very start of the page, as follows:

<!DOCTYPE html>

The first line for HTML5 – the doctype – tells the browser which version of HTML we are using on a page. You may wish to think of the doctype as a negotiation or an exchange of information between the page and the browser, much as you might, upon meeting a stranger in Switzerland, attempt words in several different languages before both parties settle on speaking English. In our case, our web page is saying “Hi. I can give you the information that follows as at least HTML 5.”

The modern HTML5 doctype is unusual in that it doesn’t specify a version name or number: HTML is now a “living language” that is constantly under development and change. Version numbers are only used in the formal specification.

The doctype is required to validate the page, in order to check that the page has the right structure, and that browsers will understand it. Without a doctype, the validator has no idea what version of HTML you are using, or what rules to check your code against.

Photograph by Alejandro Pinto, used under a Creative Commons Generic Attribution 2.0 license

Enjoy this piece? I invite you to follow me at twitter.com/dudleystorey to learn more.