Could anyo开发者_如何学Gone specify what is the need and use of the Document Type Declaration in HTML pages. What is the advantages of it? I searched through the Net and found the results to be little bit confusing. Please someone highlight.
To clarify, per Gumbo's question, the Document Type Definition describes the grammar of a document type. This is practically applied with a Document Type Declaration in a document. The following describes the latter.
It does two things:
It ensures that your data is correctly described, so a document parser can know the semantics of the tags and entities you've used in the document structure.
In modern browsers, it triggers rendering modes. Most modern doctypes trigger "standards mode" (which is meant to follow current or proposed web standards) or "almost standards mode" (which is identical to "standards mode" except that the default
display
style for the<img>
element isblock
rather thaninline
), while older doctypes or no doctype will trigger "quirks mode" (which renders according to an older box model where dimensions are calculated differently; in this box model, thewidth
of an element, for example, is the total width of the displayed element and its content area is reduced bypadding
).
Notes:
I don't think any version of IE supports "almost standards mode", but it's easily reproduced with this line in CSS:
img { display: block; }
It is possible to trigger "quirks mode" even with a "standards mode" doctype present, if there is whitespace before the doctype, or in IE6 if there is an XML prologue (eg.
<?xml version="1.0" encoding="UTF-8"?>
) before it.
精彩评论