The <!doctype> is the way to declare or instruct your web browser that which version of HTML you are going to use to write the web page. This is the first thing to do in your web page even before the <html> tag. This definition is also called as Document type Definition or DTD.
Remember that, the <!doctype> is a declaration of the HTML version. It is neither the part of HTML nor an HTML tag. The <!doctype> is supported by all major web browsers like Chrome, Firefox, Internet Explorer, Safari, Opera etc.
HTML5 doctype example
<!—HTML 5 code, tags –>
The above example tells the web browser that this web page is going to be an HTML 5 document followed by the HTML code.
Note that there is no closing tag of the <!DOCTYPE>.
What if doctype tag or DTD is not specified
Your web page may work normal if no document type definition, DTD is specified. However, this is always recommended to declare the doctype. As such doctype specifies which HTML version you are using, this piece of information helps or is required by some useful tools like Validator by w3c. This tool helps in validating the markup in your web document. Similarly, other tools may also require which version of HTML you are using.
Also this is quite helpful for browsers to trigger standard rendering mode if <doctype> specifies which HTML version is used or else browser will keep on guessing which HTML version is used and may cause some unexpected results.
doctype declaration for different HTML versions
<!DOCTYPE> is declared differently for different versions of HTML. The latest is HTML 5 which is quite popular now. HTML 4.01 doctype declaration is still widely used. Below are ways for declaring <!DOCTYPE> for different HTML versions.
In HTML 5, which is the latest version of HTML, the <!doctype> is declared as follows:
That is how you simply use html after the DOCTYPE html. This tells the web browser that your web page will use HTML 5. The HTML 5 has a lot of new features like new semantic elements, new form types, multimedia elements like <video>, <audio> etc.
DOCTYPE html public – for HTML 4.01
This is covered in a separate chapter. Click here to learn about HTML 4 declaration.