< Back

Understanding Doctypes in HTML

2012-08-01

Introduction

Back in the 1990s, when I first started developing websites, there was very little in the way of 'web standards'. There were two main web browsers, Netscape Navigator and Internet Explorer, and people wrote HTML so that their web pages looked correct in those two browsers.

In the beginning, HTML documents were simply started with an <HTML> tag:

<HTML>
	<BODY>
		<P>A simple web page.
	</BODY>
</HTML>

When web standards started being written, it became necessary to be able to determine what standard a particular HTML document was following. Because of this, DOCTYPEs were added.

DOCTYPEs Are Not Just For HTML

A Document Type Declaration (DOCTYPE) is a special statement at the beginning of a document that states what sort of document it is. DOCTYPEs are not unique to HTML files, but can also be found in SGML and XML files.

Modern web browsers will often display a web page differently, depending on the DOCTYPE of the page, so it is very important that you consistently use the correct DOCTYPE when creating web pages.

There are three DOCTYPEs for HTML pages that are commonly used today.

HTML 4.01

The following shows the DOCTYPE for HTML 4.01 documents. This has been the dominant DOCTYPE on the web for many years, but is soon to be overtaken by HTML5:

<!DOCTYPE HTML PUBLIC
	"-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">

<html>
	<body>
		<p>An HTML 4.01 web page.</p>
	</body>
</html>

XHTML 1.0

Similar to HTML 4.01, the XHTML 1.0 standard is a little more strict in its syntax:

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

<html xmlns="http://www.w3.org/1999/xhtml">
	<body>
		<p>An XHTML 1.0 web page.</p>
	</body>
</html>

Technically, XHTML 1.0 documents are also XML files, so they are also supposed to have an XML declaration before the DOCTYPE, but in practice this is omitted due to some versions of Internet Explorer rendering pages incorrectly.

HTML5

HTML5 is the latest and greatest HTML standard. Many of its features are still being developed, but it is likely to quickly become the dominant DOCTYPE on the web.

<!DOCTYPE html>

<html>
	<body>
		<p>An HTML5 web page.</p>
	</body>
</html>