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.
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>
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.
This is the DOCTYPE declaration for HTML5. It's short, simple, and case-insensitive.
<!-- 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">
<!DOCTYPE html>
is recommended for all new projects.
The <html>
element is the root element of an HTML page. All other elements must be descendants of this element.
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. |
lang
attribute on the <html>
element to improve accessibility and help search engines understand your content.
The <head>
element contains meta information about the document, like its title, character set, styles, scripts, and other metadata.
<title>
- Defines the title of the document (required)<meta>
- Provides metadata about the HTML document<link>
- Defines relationships between the document and external resources<style>
- Contains document styles<script>
- Contains client-side scripts<base>
- Specifies a base URL for all relative URLs in the pageElement | 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"> |
The <body>
element contains all the content that is visible to users, such as text, images, links, lists, tables, and more.
<header>
- Typically contains introductory content, navigation<nav>
- Contains navigation links<main>
- Contains the main content<section>
- Defines sections of content<article>
- Defines independent, self-contained content<aside>
- Defines content aside from the main content (like a sidebar)<footer>
- Contains footer informationHTML comments allow you to add notes to your code that browsers will ignore. They're useful for documentation or temporarily disabling code.
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>
The basic structure described in this guide is supported by all modern browsers:
lang
attribute on the <html>
element<meta charset="UTF-8">
in the <head>
section