DOCTYPE
Definition
Within the HTML/XHTML family of markup languages, differences between the markup languages are in some cases very subtle. For example, it may be only a question of the number of elements/attributes defined. W3C has written specifications that spell out the elements/attributes in each language. These specifications are called document type declarations or DTDs for short.
Since HTML/XHTML markup languages can be so similar, to help Web browsers (and other devices) identify the type of markup language used on a Web page, a document type declaration (DOCTYPE
) that points to a specific DTD should be specified at the top of the Web page.
Example
In the first 2 lines of this example, the DOCTYPE
XHTML 1.0 Strict is declared:
<!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">
<head>
<title>XHTML Reference</title>
</head>
<body>
...
</body>
</html>
This example declares the XHTML 1.1 DOCTYPE
:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XHTML Reference</title>
</head>
<body>
...
</body>
</html>
Best practice
A DOCTYPE
is a commitment to follow the rules of a given markup language. Essentially, you are saying to a machine: "Anything that I send you will conform to the markup rules of the named DTD. So process what I send you according to those rules". For this reason, if markup that you supply does not conform to the specified rules, Web browsers or other devices may not process your markup as expected.
Note: the XHTML 1.1 DOCTYPE
should only be used when Web pages are served as XML.