HTML Document Structure Guide

Understanding the basic structure of an HTML document is essential for web development. This guide covers the fundamental elements that make up the skeleton of every HTML page.

1. Basic HTML Document Structure

Every HTML document follows this basic structure:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document Title</title>
</head>
<body>
  <!-- Content goes here -->
</body>
</html>
!DOCTYPE html
<html>
<head>
meta, title, links, scripts, etc.
<body>
Visible page content

2. Document Type Declaration

The <!DOCTYPE> declaration must be the first thing in your HTML document, before the <html> tag. It tells the browser which version of HTML the page is using.

<!DOCTYPE html>

This is the DOCTYPE declaration for HTML5. It's short, simple, and case-insensitive.

Legacy DOCTYPEs (Historical Reference)

<!-- HTML 4.01 Strict -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<!-- HTML 4.01 Transitional -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<!-- XHTML 1.0 Strict -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Note: Modern web development uses HTML5, so the simple <!DOCTYPE html> is recommended for all new projects.

3. The html Element

The <html> element is the root element of an HTML page. All other elements must be descendants of this element.

<html lang="en"> <!-- head and body go here --> </html>

Important Attributes

Attribute Value Description
lang Language code (e.g., "en", "es", "fr") Specifies the language of the document. Important for accessibility and SEO.
dir "ltr" or "rtl" Specifies the text direction. "ltr" (left-to-right) is default, "rtl" is for languages like Arabic and Hebrew.
Best Practice: Always include the lang attribute on the <html> element to improve accessibility and help search engines understand your content.

4. The head Element

The <head> element contains meta information about the document, like its title, character set, styles, scripts, and other metadata.

<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content="This is a description of the page content"> <title>Page Title</title> <link rel="stylesheet" href="styles.css"> <script src="script.js" defer></script> </head>

Common Elements in the head

Important meta Elements

Element Description Example
<meta charset> Specifies the character encoding for the document <meta charset="UTF-8">
<meta name="viewport"> Controls viewport settings for responsive design <meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description"> Provides a description of the page for search engines <meta name="description" content="Page description here">
<meta name="keywords"> Provides keywords for search engines (less important now) <meta name="keywords" content="HTML, CSS, JavaScript">
Best Practices:
  • Always include UTF-8 character encoding
  • Always include a viewport meta tag for responsive design
  • Keep the title concise but descriptive (55-60 characters)
  • Include a meaningful meta description (around 150-160 characters)

5. The body Element

The <body> element contains all the content that is visible to users, such as text, images, links, lists, tables, and more.

<body> <header> <h1>Website Title</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <main> <section> <h2>Section Title</h2> <p>This is a paragraph of text.</p> </section> <article> <h2>Article Title</h2> <p>This is another paragraph of text.</p> </article> </main> <footer> <p>© 2025 My Website. All rights reserved.</p> </footer> </body>

Common body Structure (Semantic HTML5)

Note: We'll cover these semantic elements in more detail in the Semantic HTML guide.

6. Comments in HTML

HTML comments allow you to add notes to your code that browsers will ignore. They're useful for documentation or temporarily disabling code.

<!-- This is a comment --> <!-- This is a multi-line comment --> <p>This will be visible to users.</p> <!-- This comment won't be visible to users -->
Note: Comments are visible in the page source, so don't include sensitive information in them.

7. HTML Templates

Here's a complete HTML5 template you can use as a starting point for your web pages:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description" content="Description of your page">
  <title>Your Page Title</title>
  <link rel="stylesheet" href="styles.css">
  <script src="script.js" defer></script>
</head>
<body>
  <header>
    <h1>Website Title</h1>
    <nav>
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </nav>
  </header>
  
  <main>
    <section>
      <h2>Main Content</h2>
      <p>Your content goes here.</p>
    </section>
  </main>
  
  <footer>
    <p>© 2025 Your Name. All rights reserved.</p>
  </footer>
</body>
</html>

8. Browser Support

The basic structure described in this guide is supported by all modern browsers:

CH
Chrome
Yes
FF
Firefox
Yes
SF
Safari
Yes
ED
Edge
Yes
OP
Opera
Yes
IE
IE 11
Yes

9. Best Practices