In HTML documents we often see a tag in the very first line i.e. <!DOCTYPE… and wonder why it is there.
The DOCTYPE tag (may also be in lowercase doctype) is often used as an additional identifier at the beginning of a file to help identify the DTD used to define the interpretation of the contents of the document. This can be particularly important when there are different versions available.
A type DOCTYPE declaration looks like:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
It is not necessary to define the full URL to the DTD except in the case of XHTML. Another example would be:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
DOCTYPE for HTML5 is simply:
The DOCTYPE identifies the rules for the markup language used in the document, so that Web Browsers can render the HTML document correctly. It is also used to identify SVG versions, MathML, and several other file types.
Some believe that this entry is not needed and indeed it is not actually required. It is possible to define versions and other data within the root HTML statement itself such as:
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
However DOCTYPE is now the dominant method.