tags:

views:

80

answers:

1

Hello,

Could anyone 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.

+7  A: 

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:

  1. 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.

  2. 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 is block rather than inline), 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, the width of an element, for example, is the total width of the displayed element and its content area is reduced by padding).

Notes:

  1. 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; }

  2. 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.

eyelidlessness
I think you’re confusing document type *definition* and document type *declaration*. You’re talking about the latter.
Gumbo
@Gumbo, can you explain how they're practically different as an answer to the question?
eyelidlessness
Whats the difference between *definition* and *declaration* ?
Shoaib
@eyelidlessness: The document type definition specifies some grammar. And the document type declaration refers to a document type definition that is to be used for the current document. And `<!DOCTYPE … >` is just a document type declaration. (See http://www.w3.org/TR/html4/struct/global.html#version-info)
Gumbo
@Gumbo, I understand that. I'm asking how that distinction is important in a practical way to the question.
eyelidlessness
@eyelidlessness: It’s important to distinct these two terms because they describe two different things. The one describes the grammar of a language called HTML and the other describes what grammar is to be used when interpreting a certain document. It’s like mixing up *English* (the language defined by some grammar) and *I’m using English* (the statement to interpret my sentences as English).
Gumbo
@Gumbo, to clarify, I understand that they're different. I don't understand how the difference has a practical impact for the question. shoaibmohammed was clearly asking about a declaration (even while specifying a definition).
eyelidlessness
@eyelidlessness: Well, in retrospect I have to admit that it really doesn’t make much sense if he actually meant document type definition. But as you see, it is important to be specific. Otherwise you can get a right answers for a wrong question.
Gumbo