views:

97

answers:

4

Is it ok not to include such lines in a HTML file? Removing these lines makes the code look more clean.

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
        <html xmlns="http://www.w3.org/1999/xhtml"&gt;
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

When I use dreamweaver to create a HTML file, these lines are automatically included.

+1  A: 

No, you shouldn't remove these lines. The first two lines tell the browser what type of document your page is, and helps the browser render it properly.

The third line tells the browser what character set you're using, in this case so that it knows to render non-latin characters properly.

John McCollum
That's not IE-specific. Every browser does doctype sniffing to switch to quirks mode or not.
Joey
Thanks, you're right of course. I'll amend my answer.
John McCollum
+1  A: 

Those declares the DOCTYPE, which shouldn't be forgotten to add.

Why?

Why specify a doctype? Because it defines which version of (X)HTML your document is actually using, and this is a critical piece of information needed by browsers or other tools processing the document.

For example, specifying the doctype of your document allows you to use tools such as the Markup Validator to check the syntax of your (X)HTML (and hence discovers errors that may affect the way your page is rendered by various browsers). Such tools won't be able to work if they do not know what kind of document you are using.

But the most important thing is that with most families of browsers, a doctype declaration will make a lot of guessing unnecessary, and will thus trigger a "standard" parsing mode, where the understanding (and, as a result, the display) of the document is not only faster, it is also consistent and free of any bad surprise that documents without doctype will create.

Moayad Mardini
+2  A: 

No, you should NOT remove those lines.

You can, however, switch the <!doctype>-declaration to the one of HTML5, since that will still trigger standards mode in all current browsers, even though they don't yet implement HTML 5. It looks like the following:

<!DOCTYPE html>

Which is a bit more clean than the ordinary one you use looks like. You can also read a little more about the new doctype-declaration here. You can also learn more about what will change in HTML5 here.

PatrikAkerstrand
+1  A: 

You can remove the DOCTYPE, html tags and meta tags and still have valid HTML, and if you are happy for your page to take browser default styling they can be safely omitted. The content type and charset can be specified by the HTTP headers if you prefer. As others have already pointed out, the DOCTYPE will affect how styling instructions are interpreted, and also how HTML parsers interpret some invalid markup, so you will need to allow for this.

Alohci