The Doctype Declaration in HTML

The first line of code required in every HTML document

Samantha Genevay
3 min readMar 6, 2023

If you open up your dev tools on a web page, you’ll notice that the HTML begins with DOCTYPE. Let’s explore this DOCTYPE declaration: what it is, how it’s written, and a brief history.

First, what is it?

The DOCTYPE declaration in HTML is an instruction that specifies the version of HTML being used in a web page. It is included at the very beginning of an HTML document, before the <html> tag.

The purpose of the DOCTYPE declaration is to inform web browsers and other software about which version of HTML the web page is using. This allows the software to parse the web page correctly and to display it in the way that the developer intended.

Without a DOCTYPE declaration, a web browser may not know which version of HTML the page is using, which could lead to unexpected behavior or rendering errors.

HTML5 DOCTYPE and past versions

In the current version of HTML5, the declaration is short and sweet:

While similar in formatting, it is important to note that the declaration is not an HTML tag. The declaration is also not case sensitive, though typically, it is written in all uppercase. The below examples are all valid and would indicate to the browser that the document type is HTML5.

Earlier versions of HTML required that the DOCTYPE include a Document Type Definition (DTD). The DTD indicated which elements and attributes were valid and referenced a link to an external file from the World Wide Web Consortium (W3C). There were three modes, strict, transitional, and frameset, to handle different use cases — primarily whether a page included or excluded elements that the W3C expected to phase out.

This is a example of the HTML4 strict declaration:

Valid elements

Depending on the DOCTYPE, different HTML elements are considered valid. The below link from W3 schools includes a table with elements supported by HTML4 vs. HTML5:

https://www.w3schools.com/tags/ref_html_dtd.asp

As an example, you can see that the HTML acronym element, <acronym>, is valid for HTML4, but not HTML5.

A brief history of DOCTYPE

In the early days of HTML, there wasn’t a formal specification or standard for the language, which often resulted in incompatible and inconsistent rendering of web pages across different browsers.

With the introduction of HTML 3.2, a DOCTYPE declaration was added to the language by the W3C. The W3C developed an agreed upon set of standards to help ensure consistency across browsers.

With HTML4, the DOCTYPE became even more important as it included the Document Type Definition (DTD) which outlined the rules and rendering mode for the HTML document.

HTML5 introduced a simpler DOCTYPE declaration that did not require a specific DTD to be specified. The declaration’s role is to tell the browser to render the document in what is known as “full standards mode”, meaning that it adheres to the W3C web standards.

Conclusion

The DOCTYPE declaration has played an important role in the evolution of HTML by allowing web developers to indicate the version of HTML they are using, ultimately creating better experiences for users across browsers.

--

--