English 中文(简体)
Is it ok to remove the lines below in a HTML file?
原标题:

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">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <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.

最佳回答

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.

问题回答

No that is not allowed. It sets the character encoding, i.e. how the browser should read it. But the new HTML5 elements make the whole thing look easier and cleaner. So:

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

becomes --> <!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"
      lang="en"
      xml:lang="en">

becomes --> <html lang="en">

<meta http-equiv="Content-Type" content="text/html; 
charset=UTF-8">

becomes --> <meta charset="UTF-8">

<link rel="stylesheet" href="style.css" type="text/css">

becomes --> <link rel="stylesheet" href="style.css">

So the whole code in the <head> becomes:

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">

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.

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.

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.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签