1

I am a little curious,

I have a link, it show different behaviour with and without following declaration:

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

link with declaration:

link with html dclaration

link without declaration:

link without html declaration

Why we need the declaration and Is the declaration affect style?

Wildan Muhlis
  • 1,553
  • 2
  • 22
  • 43

3 Answers3

2

Why we need the declaration and

Historically? It provided a way for a validator to know what elements and attributes were allowed where and for a parser to expand entity references.

HTML 5 gives up on this idea and has a simpler Doctype that does nothing except trigger standards mode (see below).

Is the declaration affect style?

Browser venders, as they were coming out of the era of "having lots and lots of bugs" decided to use the Doctype as a heuristic to determine if the author of the page knew what they were doing.

With no Doctype, they decide that the author doesn't know what they are doing and emulate a lot of ancient bugs. This creates a lot of inconsistencies in how different browsers render a given page. This is called Quirks mode.

With a Doctype (except for certain Doctypes), they decide that the author does know what they are doing, and try to render the page as close to how the standards say as possible. This is Standards mode.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

Doctype as defined in specification.

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.

What DOCTYPE declaration does:

  1. When performing HTML validation testing on a web page it tells the HTML (HyperText Markup Language) validator which version of (X)HTML standard the web page coding is supposed to comply with. When you validate your web page the HTML validator checks the coding against the applicable standard then reports which portions of the coding do not pass HTML validation (are not compliant).

  2. It tells the browser how to render the page in standards compliant mode.

If DOCTYPE is not included :

  1. You will not be able to use a HTML (HyperText Markup Language) Validator to check the page coding.

  2. The browser rending the web page will process the coding in "quirks" mode.

  3. The stylesheet may not be implemented as planned.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
0

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/Tips/Doctype

iConnor
  • 19,997
  • 14
  • 62
  • 97