Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

HTML

Fedor Andreev
Fedor Andreev
6,438 Points

A very silly question about HTML?

You know how you have to type DOCTYPE to state the document the browser will be reading such as HTML or Java.

Well how come in the "Bake Website" lesson all Nick does is type in

<h1> Bake</h1> 

(the only code in the entire page). And yet, the computer shows it up on the screen without any data at all as to what type of document it is?

So my question is, if he can just type in that one code and it will show up on the screen with no problem. What's the point of <DOCTYPE HTML>?

4 Answers

anthonybritton
anthonybritton
2,164 Points

Just to elaborate slightly on Rae and James' answers, the Doctype tells the browser to which standards you have written your markup. As Rae pointed out, this is more apparent before we had the luxury of the simplified HTML 5 <!Doctype html> when the declaration could be something as fun to remember as:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

or

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

Using the correct doctype helps the browser to interpret your markup correctly and apply the proper rules/behavior to certain elements keeping in line with web standards. Also, when you have the joy of supporting Internet Explorer, if you don't provide a valid doctype declaration IE will render the page in "Quirks Mode" resulting in the world (and your website) crumbling to the ground!

In the case of a quick example as it seems you encountered in your tutorial where the doctype and other typically necessary or helpful markup was omitted, any decent browser should be able to make certain decisions about how to render the page without you providing that information. However, when writing a full html document all of these things (doctype, head, title, meta tags like 'charset') should always be used and considered a "best practice".

Fedor Andreev
Fedor Andreev
6,438 Points

I understand he saved the document in

.HTML

But seriously? I'm so confused. I'm just trying to understand the logic behind everything.

Rae Yung
Rae Yung
9,113 Points

To my understanding, it's not really about declaring what kind of file it is; the file extension takes care of that. It's about declaring what version of HTML it is.

In HTML5, you just need to write <!DOCTYPE html> rather than including a long URL and declaration and a bunch of other details. This is good in a lot of ways, but as you've found out, its meaning is a lot less intuitive if you've never slogged through the old-style declarations. In earlier versions of HTML, doctype declarations would specify 'XHTML 1.0 Strict' or whatever.

While most browsers will process the code okay without that info, it helps if you want to do anything like run a markup validator. Plus, it just eliminates a lot of client-side guesswork, so you know whatever code you're using is going to be read the same way in the browser or text editor or whatever the end-user is viewing the file in.

James Barnett
James Barnett
39,199 Points

tl;dr Sometimes browsers get confused if you don't specify the correct doctype


HTML doesn't have error messages it will try to make the best of whatever you give it. However if you get invalid markup it might do what you want in some cases it might not. One of the easiest ways to "confuse" CSS is to write invalid HTML.