html

HTML Document Type Explained

The document type contains the information that defines the markup language type of the document. In HTML, the document type tells the browser about the version of the HTML so that the browser handles the elements accordingly. In HTML 5, the <!DOCTYPE html> specifies that the document must be recognized as an HTML 5.0 document. However, the previous versions of HTML may have strict, transitional, or frameset document types.

This article would serve as the building block to learn the basic concepts of HTML document types and after that, you would be able to decide which document type better serves your purpose.

Document types in HTML

The declaration of document type varies from version to version. Let’s explore various ways to declare document type for your document.

How to declare document type in HTML5

HTML5 is the latest version of HTML being used nowadays. The document type declaration can be done by using the following line.

<!DOCTYPE html>

Once the above line is written before the <html> tag, the browser instantly recognizes it as an HTML5 page. It is recommended to use the document type as you may not get the desired output in older versions of browsers. The image provided below serves as the basic structure of an HTML5 document where the <!DOCTYPE html> is declared at the top.

Text Description automatically generated

Note: Most of the modern code editors add this document type automatically. If not, you can add it manually as well.

How to declare document type in HTML4

Although, the document type used by HTML4 is obsolete now and no longer usable in HTML5. However, the purpose is to clear the difference between the document type of HTML5 and HTML4. An HTML4 document may have one of the following DTDs (Document Definition Types):

HTML4 Strict DOCTYPE

The strict type allows you to use only the elements supported by the current version. Thus, the depreciated elements cannot be exercised when a document has strict DOCTYPE. The syntax to declare this document type is written below.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 strictl//EN"

"http://www.w3.org/TR/html4/strict.dtd">

HTML4 Transitional DOCTYPE

The transitional DTD accepts the current and depreciated elements as well. This syntax is used to declare the transitional doctype.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/transitional.dtd">

HTML4 frameset DOCTYPE

This <DOCTYPE> is a derivative of the transitional doctype and thus contains all the elements supported by it. The frameset basically converts the document body (the body element is replaced by a frameset or frame element) into rectangular frames. Following is the syntax for declaring a HTML4 frameset DOCTYPE.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"

"http://www.w3.org/TR/html4/frameset.dtd">

Conclusion

In HTML documents, the <!DOCTYPE> tells the browser about the version of the HTML language being used. There are different ways to declare a document type in HTML5 and HTML4. If the document type is not defined, the latest versions of the browser recognize it as an HTML5 document. For HTML4, you must specify it to use the elements/properties supported by HTML4. This article provided a detailed overview of <!DOCTYPE> in HTML. Nowadays, it is recommended (not necessary because the modern editors/browsers consider it as an HTML5 document) to use the <!DOCTYPE html> for every HTML document.

About the author

Adnan Shabbir