What is the DOCTYPE declaration and why is it needed?

Answer

The DOCTYPE (Document Type Declaration) is an instruction at the very top of an HTML file that tells the browser which version of HTML the page is written in. In HTML5 it is simply <!DOCTYPE html>. Without a DOCTYPE, browsers enter quirks mode — they emulate old, inconsistent browser behavior to support legacy websites, which can cause unexpected rendering differences across browsers. With a DOCTYPE, browsers use standards mode, applying the CSS box model and layout rules consistently and predictably. It must appear before the <html> tag and is not case-sensitive.