Skip to content

Instantly share code, notes, and snippets.

@satansdeer
Created January 26, 2019 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satansdeer/2abd45404ff7ef16745b224b6fad5258 to your computer and use it in GitHub Desktop.
Save satansdeer/2abd45404ff7ef16745b224b6fad5258 to your computer and use it in GitHub Desktop.

What is doctype in HTML documents? And why do you have to specify the doctype?

If you open any webpage, and look at it's source code, you'll always see a little thingy just before the opening html tag. So what is it, and what does it do?

This string is a document type declaration, and it's important to note that it's not an html tag itself, but it's an instruction for the browser on what version of HTML is used on this page.

Good news, modern web developers don't have to know any doctypes other than doctype html, because since the html5 it's the only doctype you should set for your webpages.

Before html5, there were several others that had more complex syntax. You had to specify the link to the actual doctype declaration. For example like in this html4.01 strict. We specify the link to the doctype on w3.org website

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

https://www.w3.org/TR/html401/strict.dtd

If you open the link, you will see how those type declarations used to look. It was a structured document that was telling browser what elements and attributes are allowed to use in current html version.

But as I said the modern type declaration dosent' requiere you to specify the link to declaration file manually. And the only thing you have to worry about is to include this instruction in the beginning of your html document.

Now why, do you need to specify it?

In the dark ages of the internet, before the web standards, there we two huge browsers: Netscape Navigator and Microsoft Internet Explorer. And web pages were usually developed separately for each of them.

After the creation of the standards browsers introduced two ways of rendering, one for standards compliant websites and quirks mode to support the legacy sites.

For example you can see the list of quirks that will be used for your page on mozilla website.

So when you specify the doctype in the beginning of your doument - you tell the browser that the webpage should be rendered in standards mode.

Otherwise if you omit it - you make the browser to use the quirks mode.

Now to sum it up: Doctype is a type declaration that should go as firs line in your html document before the opening html tag. And you should only use the <!doctype html> since the html5 was released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment