开发者

What is the DOCTYPE... for [duplicate]

This question already has answers here: Closed 11 years ago.

Poss开发者_运维技巧ible Duplicate:

What's up, Doctype?

When create a new file in Netbeans IDE I get <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> at the beginning of the file.

I delete it and my html still works. I wonder what that is and is it neccessary?

Thank you.


The doctype declaration is not an HTML tag; it is an instruction to the web browser about what version of the markup language the page is written in.

The doctype declaration refers to a Document Type Definition (DTD). The DTD specifies the rules for the markup language, so that the browsers render the content correctly.

take a look here: http://www.w3schools.com/tags/tag_doctype.asp


A Document Type Declaration, or DOCTYPE, is an instruction that associates a particular SGML or XML document (for example, a webpage) with a Document Type Definition (DTD) (for example, the formal definition of a particular version of HTML).

http://en.wikipedia.org/wiki/Document_Type_Declaration

Also, from W3C:

There is not just one type of HTML, there are actually many: HTML 4.01 Strict, HTML 4.01 Transitional, XHTML 1.0 Strict, and many more. All these types of HTML are defined in their respective W3C specifications, but they are also defined in a machine-readable language specifying the legal structure, elements and attributes of a type of HTML.

http://www.w3.org/QA/Tips/Doctype


I believe that if you don't specify a doctype, the browser will add a default one, that's why it works. Adding that line overrides the default to specify that you want that particular markup language.


There are many variations of HTML with various names; XHTML, DHTML etc... Your browser will do its best to work out which variation your document is written in but may not always get it right. Particularly in IE it will default to "quirks mode" if you do not declare a doctype which frequently causes most of your layout to break.

Declaring the doctype means the browser doesn't have to make this best guess and instead, it renders your page according to the specification related to the doctype you have declared.

Here are some interesting articles on the differences between some of the DTDs:

  • Strict vs. Transitional
  • HTML vs. XHTML


To make it clear: unless we care about validation, the only reason why to use doctype is to trigger standards mode (see other comments). Browsers do not differentiate between versions of HTML. This is why it is recommended to choose as simple doctype as possible:

<!doctype html>


The Doctype tells the browser what version of HTML or XHTML are you writing, so it can treat it as it is supposed to.

WIth no Doctype it will work, but the browser wont know exactly what version is it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜