← Community home

Why does an HTML page need a doctype?

Community › Forums › HTML Basics › Why does an HTML page need a doctype?

HTMLCodeRunner Editorial
About this answer

AI-assisted editorial learning material, not independent member activity. Check the reference and add your own questions or corrections.

HTML library ↗

Tagged: ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36

    A page copied from an old tutorial has unexpected sizing and layout behavior.

    #37

    Detailed answer

    The short HTML doctype selects standards mode in modern browsers.

    Put it before the document element. It is a declaration rather than an element and does not need a closing tag. For a page with unexpected legacy layout behavior, inspect document.compatMode in the console.

    Example

    <!doctype html>
    <html lang="en"><head><title>Standards mode</title></head><body><p>Hello</p></body></html>

    Understanding the behavior

    Standards mode makes browser layout follow modern rules rather than compatibility behavior for older pages. A doctype is not a link to a validator or a stylesheet, and the short HTML declaration does not require an HTML version number.

    Practical implementation notes

    Compare document.compatMode before and after fixing a missing doctype. If the page still looks wrong in standards mode, inspect the CSS box model rather than repeatedly changing the declaration.

    How to check it

    Evaluate document.compatMode in the browser console after adding the doctype at the beginning.

    Expected result

    A standards-mode document reports CSS1Compat.

    Watch out: A reusable component should not introduce a second doctype.

    Read the reference ↗

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.