DEV Community

Cover image for What the hell is <!DOCTYPE html>?
Amandeep Singh Malhotra
Amandeep Singh Malhotra

Posted on Edited on

What the hell is <!DOCTYPE html>?

<!DOCTYPE html> <!-- What the hell is this?? -->>
<html lang="en">
<head>
    <title>Document</title>
</head>
<body>
</body>
</html
๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung ๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung

When creating a new HTML file, you might have wondered, โ€œWhy do I need to add this statement to the first line of my HTML file? Doesnโ€™t the .html file extension suffice in telling the browser that this is an HTML file?โ€. While this is true, thereโ€™s still more to it. Before we understand the above statement, letโ€™s try to understand what a DTD is.

Document Type Definition (DTD)

A Document Type Definition (or DTD in short) describes the document structure of an XML document along with a list of the legal elements the document can have.

Prior to HTML5 (for e.g. in HTML 4.01), all HTML documents would have to contain a โ€œDoctypeโ€ declaration on its first line along with a reference to its type definition. This would look something like:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
"http://www.w3.org/TR/html4/strict.dtd">
๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung ๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung

This declaration served two purposes:

  • Provide the list of valid elements the document can contain.
  • Define the flavour of the DTD which could be strict, transitional or frameset. More on this here.

So why donโ€™t you see these long DTD references in the latest HTML files you've encountered?

Enter HTML5!

See, earlier versions of HTML (prior to HTML5) were SGML (Standard Generalized Markup Language) based, and therefore required a reference to DTDs. HTML5 onwards, this reference was no longer needed since this version of HTML is not SGML based.

So now, this โ€œDoctypeโ€ declaration serves another purpose. It allows browser to run the HTML file in a โ€œStandard modeโ€. Now itโ€™s logical to ask, โ€œWhat on earth is this standard mode now??โ€ To understand this, letโ€™s go back in time.

Back in time

Back when tech enthusiasts realised the power of what would be known as the internet, two popular browsers came into existence - the Netscape Navigator & following its success, (drumrolls) Microsoftโ€™s Internet explorer. This was even before the W3C standards came into picture which would eventually standardise how code would run across different browsers. Hence, you can imagine that, given the popularity of these browsers, websites were written in two versions - one for the Navigator, and the other one for IE, which might sound a little redundant now, but was the norm back then. However, after the W3C standards were created and browsers started adhering to them, developers encountered a new problem.

The problem now was that the legacy code started to break. Hence, a possible solution to this was that the sites were now made in two versions - a Standard version (the one which we mentioned earlier) which was W3C standards compliant and hence would run across different browsers and a Quirks version which supported the legacy code.

Now how do browsers identify which mode it needs to use? Well, just add a valid DOCTYPE declaration in the first line of the HTML file, to instruct the browser to run the code in Standard mode. Anything other than that will trigger the Quirks mode in IE9 or older. This is exactly what <!DOCTYPE html> does HTML5 onwards. If you fail to add this line to your HTML file, the browser would interpret this as an instruction to run your code in Quirks mode, and you could end up getting inconsistent results across different browsers.

Conclusion

With this little background in mind, I hope you now understand the importance of the <!DOCTYPE html> statement. If youโ€™d like to read a little more on this, please go through the links I have provided as a reference below.

References

https://www.freecodecamp.org/news/what-is-the-doctype-declaration-in-html/
https://developer.mozilla.org/en-US/docs/Web/HTML/Quirks_Mode_and_Standards_Mode
https://hsivonen.fi/doctype/

Top comments (29)

๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung
 
thomasbnt profile image
Thomas Bonnet •

Cool post about DOCTYPE! ๐Ÿ‘๐Ÿผ

Don't hesitate to put colors on your codeblock like this example for have to have a better understanding of your code ๐Ÿ˜Ž

console.log('Hello world!');
๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung ๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung

Example of how to add colors and syntax in codeblocks

๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung
 
amanmalh profile image
Amandeep Singh Malhotra •

Hey Thomas, since was my first post at dev.to, I was not aware of this. Thanks for your input. :)

๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung
 
thomasbnt profile image
Thomas Bonnet •

Oh don't worry! Very nice first post! And welcome to DEV! ๐Ÿ‘‹๐Ÿผ๐Ÿš€

๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung Thread
 
dragat19 profile image
Albert Sanchez • • Edited
console.log('hi'); 
๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung ๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung
๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung
 
pengeszikra profile image
Peter Vivo •

I take look my old cv (around 2008) doctype and that was:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung ๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung

When debugged :

console.dir(document.doctype)

{
  publicId: "-//W3C//DTD HTML 4.0//EN"
  systemId: "http://www.w3.org/TR/REC-html40/strict.dtd"
...restOfDoctype
}
๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung ๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung

Saddly my old flash animation and program does not run in modern browser, I lost them, just keep the doctype.

๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung
 
randellbrianknight profile image
Randell Brian Knight •

Very nice article about DOCTYPE! ๐ŸŽ‰ It's good to have some useful background information about these topics. Thank you, Amandeep ๐Ÿ˜Ž

๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung
 
amanmalh profile image
Amandeep Singh Malhotra •

Thank you ๐Ÿ™‚

๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung
 
kunalkeshan profile image
Kunal Keshan •

Awesome! didn't know about the quirks mode fallback if the DocType is not mentioned.

๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung
 
craftyminer1971 profile image
CraftyMiner1971 •

Thanks for the simple explanation of the DocType! I'm so glad that the W3C people simplified the DocType, cos i would not be able to remember all that fancy code that went into the former starting line for HTML files.

๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung
 
uponthesky profile image
UponTheSky •

Nice Investigation! It was really informative!

๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung
 
amanmalh profile image
Amandeep Singh Malhotra •

Thank you for going through this article. I have written another article which explains the this keyword in javascript. If you'd like to explore it, please go through this link. Thanks! โœŒ๏ธ

๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung
 
yongchanghe profile image
Yongchang He •

This is interesting!

๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung
 
invalidlenni profile image
InvalidLenni •

Thanks! I didn't know that, but now I know that! Nice post about <!DOCTYPE>

๐Ÿ”ฅ Jadwal & Hasil: amanmalh/what the hell is doctype html om - Jadwal Siaran Langsung
 
atulcodex profile image
๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ •

In starting days of my learning web development i have asked several times to my institute teacher but they are unable to solve my curiosity ๐Ÿ˜Œ thanks for your priceless effort ๐Ÿ™๐Ÿ™ shatsriya a kal ๐Ÿ™