Every HTML document needs to start with a Document Type Declaration, commonly known as a doctype. This declaration informs the browser about the version of HTML used in the webpage.
To ensure compatibility and proper rendering, it is essential to include the doctype declaration as the first line of an HTML document. The doctype declaration for HTML5 is:
<!DOCTYPE html>
This declaration tells the browser that the document is an HTML5 document.
Browser Rendering Mode:
The doctype declaration plays a significant role in determining the browser’s rendering mode for the webpage.
With the doctype declaration, the browser renders the document in “standards mode,” which adheres to the specified HTML standards and functionality.
Without the doctype declaration, browsers render the page in “quirks mode,” which is a compatibility mode that allows older websites to function properly with newer standards and features.
To obtain the desired “standards mode” rendering, it is important to include the following doctype declaration:
<!DOCTYPE html>
Additionally, to prevent quirks mode in Internet Explorer version 10 and below, it is recommended to include the following meta tag within the head tag before loading any scripts:
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
Older HTML Versions:
HTML has gone through various versions over the years. Some of the notable versions include:
- HTML (1991)
- HTML 2.0 (1995)
- HTML 3.2 (1997)
- HTML 4.01 (1999)
- XHTML (2000)
- HTML5 (2014)
For HTML 4.01 Strict, the doctype declaration was:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
XHTML had a similar doctype declaration:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
These older versions required a Document Type Definition (DTD) due to their basis on SGML, a document formatting standard. Additionally, XHTML required the html
tag to have a namespace, defined as:
<html xmlns="http://www.w3.org/1999/xhtml">
Remembering and including the correct DTD declaration for specific modes (strict, transitional) was necessary.
HTML5, on the other hand, is not based on SGML and has its own standard. As a result, the DTD is no longer required. With HTML5, the doctype declaration becomes as simple as:
<!DOCTYPE html>
These optimizations in HTML5 reduce the complexity and improve the flexibility of the doctype declaration.