How to Declare HTML5 doctype in Your Webpages

A doctype short video presentation

DOCTYPE declaration

The <!doctype > is the way to declare or instruct your web browser that which version of HTML you are going to use to write a web page. This is the first thing to do on your web page even before the <html> tag. This definition is also called 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

<!DOCTYPE html>


<!—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>.

DOCTYPE declaration for HTML 5

In HTML 5, which is the latest version of HTML the <!doctype> is declared as follows:

<!DOCTYPE html>

That is how you simply use HTML after the DOCTYPE declaration. 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.

Examples of using doctype

Following are a few DOCTYPE examples with different HTML tags.

Example of div style for setting font size and color

The following example sets the font size and color of the text by using div style attribute.

Experience this online

div background color

The example below shows how to set the div background color with the style attribute. The background property of CSS background-color is used to define the background color of the div.

Experience this online

Example of using HTML <th> tag

The example below shows a table with the employee names, ages, and salaries. The headers are placed in <th> tags.

Experience this online

You can see, we started the web page with HTML 5 declaration: <!DOCTYPE html> in above examples.

What if doctype tag or DTD is not specified

Your web page may work normally 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 HTML public – for HTML 4.01

This is covered in a separate chapter. Click here to learn about HTML 4 declaration.