DEV Community

Cover image for What is <!DOCTYPE> in HTML
Ehsan
Ehsan

Posted on

What is <!DOCTYPE> in HTML

When creating a webpage, one of the first things you might notice at the top of an HTML document is <!DOCTYPE>. This strange-looking line is actually very important! It tells the web browser what kind of HTML you're using so that it can display your webpage correctly. Let's dive into what <!DOCTYPE> is and why it matters.


What is <!DOCTYPE>?

<!DOCTYPE> is short for "document type declaration". It's a way to let the web browser know which version of HTML you are using. Think of it as setting the rules for how the browser should interpret the rest of the document.

Why is <!DOCTYPE> important?

  1. Correct Display: Without <!DOCTYPE>, web browsers might guess how to display your webpage, leading to unpredictable results.
  2. Standards Mode vs Quirks Mode:
    • Standards Mode: When you use <!DOCTYPE>, browsers follow the web standards to display your page.
    • Quirks Mode: Without <!DOCTYPE>, browsers use old rules to display the page, which can cause weird layout issues.

The Most Common <!DOCTYPE> Declaration

For modern web development, the most commonly used <!DOCTYPE> declaration is for HTML5:

<!DOCTYPE html>
Enter fullscreen mode Exit fullscreen mode

This simple line of code ensures that your webpage is treated as an HTML5 document, which is the latest version of HTML.

Examples of Other Doctypes

While HTML5 is the most common today, there are other doctypes for older versions of HTML:

  • HTML 4.01 Strict:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
Enter fullscreen mode Exit fullscreen mode
  • HTML 4.01 Transitional (allows some older tags and attributes):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
Enter fullscreen mode Exit fullscreen mode
  • XHTML 1.0 Strict (for a stricter version of HTML):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">
Enter fullscreen mode Exit fullscreen mode

Conclusion

The <!DOCTYPE> declaration is a small but essential part of any HTML document. It helps ensure that your webpage is displayed correctly across different web browsers by telling them which version of HTML to use. For most modern web development, a simple <!DOCTYPE html> is all you need.

Top comments (0)