您的位置:首页 > 其它

What's a doctype do?

2014-08-26 15:54 162 查看


来源:w3c


8.1.1 The DOCTYPE

A DOCTYPE is a required preamble.

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.

A DOCTYPE must consist of the following components, in this order:
A string that is an ASCII case-insensitive match
for the string "
<!DOCTYPE
".
One or more space characters.
A string that is an ASCII case-insensitive match
for the string "
html
".
Optionally, a DOCTYPE legacy string or an obsolete
permitted DOCTYPE string (defined below).
Zero or more space characters.
A ">" (U+003E) character.

In other words, 
<!DOCTYPE html>
, case-insensitively.

For the purposes of HTML generators that cannot output HTML markup with the short DOCTYPE "
<!DOCTYPE html>
", a DOCTYPE
legacy string may be inserted into the DOCTYPE (in the position defined above). This string must consist of:
One or more space characters.
A string that is an ASCII case-insensitive match
for the string "
SYSTEM
".
One or more space characters.
A U+0022 QUOTATION MARK or U+0027 APOSTROPHE character (the quote mark).
The literal string "
about:legacy-compat
".
A matching U+0022 QUOTATION MARK or U+0027 APOSTROPHE character (i.e. the same character as in the earlier step labeled quote mark).

In other words, 
<!DOCTYPE html SYSTEM "about:legacy-compat">
 or 
<!DOCTYPE html SYSTEM 'about:legacy-compat'>
, case-insensitively except
for the part in single or double quotes.

The DOCTYPE legacy string should not be used unless the document is generated from
a system that cannot output the shorter string.

To help authors transition from HTML4 and XHTML1, an obsolete permitted DOCTYPE string can be inserted into the DOCTYPE (in the position defined above). This string
must consist of:
One or more space characters.
A string that is an ASCII case-insensitive match
for the string "
PUBLIC
".
One or more space characters.
A U+0022 QUOTATION MARK or U+0027 APOSTROPHE character (the first quote mark).
The string from one of the cells in the first column of the table below. The row to which this cell belongs is the selected row.
A matching U+0022 QUOTATION MARK or U+0027 APOSTROPHE character (i.e. the same character as in the earlier step labeled first quote mark).
If a system identifier is used,
One or more space characters.
A U+0022 QUOTATION MARK or U+0027 APOSTROPHE character (the third quote mark).
The string from the cell in the second column of the selected row.
<
4000
li style="margin:0px;padding:0px;">A matching U+0022 QUOTATION MARK or U+0027 APOSTROPHE character (i.e. the same character as in the earlier step labeled third quote mark).

Allowed values for public and system identifiers in an obsolete
permitted DOCTYPE string.
Public identifierSystem identifierSystem identifier optional?
-//W3C//DTD HTML 4.0//EN
http://www.w3.org/TR/REC-html40/strict.dtd
Yes
-//W3C//DTD HTML 4.01//EN
http://www.w3.org/TR/html4/strict.dtd
Yes
-//W3C//DTD XHTML 1.0 Strict//EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd
No
-//W3C//DTD XHTML 1.1//EN
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd
No
DOCTYPE containing an obsolete
permitted DOCTYPE string is an obsolete permitted DOCTYPE. Authors should not use obsolete
permitted DOCTYPEs, as they are unnecessarily long.


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 some 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. 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" rendering mode.

参考:http://www.w3.org/QA/2002/04/valid-dtd-list.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  web doctype