Search code examples
htmlcsswebw3c

[HTML doctype declaration]: Why we need the declaration and Is the declaration affect style?


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?


Solution

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