Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML Introduction to HTML and CSS (2016) HTML: The Structural Foundation of Web Pages and Applications The Basic Structure of a Web Page

Is <!doctype html> really necessary?

When I was first learning basic HTML5, I remember there was a lot more code before the opening html tag besides <!doctype html>. I was later told that this is unnecessary, and that nothing is needed before the opening html tag because nowadays browsers already know when they are being presented with HTML code. I tried deleting the <!doctype html> tag in this workspace and sure enough, the page loaded just fine. Can someone please explain this to me?

4 Answers

Steven Parker
Steven Parker
231,275 Points

As you had heard, browsers will let you get away without a doctype declaration. But it is "best practice" to always include it, and as you noticed, the modern HTML5 version is much more compact and easier to include than before.

Interesting! Thanks for the detailed replies!

Great, thanks! Do we include it so that older versions of browsers can read the code properly? Or are there problems that occur with more complex projects if it isn't included? Thanks.

Steven Parker
Steven Parker
231,275 Points

The HTML specs say that A DOCTYPE is a required preamble.

But it also explains that:

DOCTYPEs are required for legacy reasons. When omitted, browsers tend to use a different rendering mode that is incompatible with some specifications. Including the DOCTYPE in a document ensures that the browser makes a best-effort attempt at following the relevant specifications.