Skip to main content

HTML Basics

HTML Structure

Written by Updated HTML Structure exercises

Every HTML page has the same skeleton: a doctype, then html, then a head with information about the page, then a body with the content. Get these five pieces in the right order and the rest of HTML is filling in the body.

What Is the Basic Structure of an HTML Document?

An HTML document is a set of elements nested inside each other. This is the smallest complete page:

HTML Basic Structure

html

<!DOCTYPE html>
<html>
  <head>
    <title>My Web Page</title>
  </head>
  <body>
    <h1>Welcome to My Website</h1>
    <p>This is a simple HTML document structure.</p>
  </body>
</html>

Each piece has one job, explained below. Browsers are forgiving and will often guess when something is missing, but writing the full skeleton means there is nothing for them to guess.

<!DOCTYPE html>: Document Type Declaration

The first line of the file. It tells the browser to use modern standards mode.

html

<!DOCTYPE html>

Without it, browsers switch to an old "quirks mode" that lays pages out differently. Full details in the doctype tag.

<html>: Root Element

The root element: everything else goes inside it. Add the page's language here, like <html lang="en">, so screen readers pronounce the text correctly.

html

<html>
  ... all your content goes here ...
</html>

<head>: Head Section

Information about the page that is not shown on it:

  • The page title
  • Meta tags (description, keywords, viewport)
  • Links to CSS files or favicons
  • Scripts to run before the page loads

html

<head>
  <title>My First Page</title>
  <meta charset="UTF-8">
  <meta name="description" content="A simple HTML structure example">
</head>

More in the head tag.

<title>: Title Tag

The page's name. It appears in the browser tab, in bookmarks and as the headline in search results.

html

<title>My Web Page</title>

<body>: Body Section

Everything visible on the page: text, images, links, forms. There is exactly one body, right after the head.

html

<body>
  <h1>Hello, World!</h1>
  <p>This is my first HTML page.</p>
</body>

Complete Example with Explanation

HTML Example

html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Basic HTML Structure</title>
  </head>
  <body>
    <h1>Welcome to HTML Structure</h1>
    <p>This example shows the essential building blocks of an HTML document.</p>
  </body>
</html>

This version adds the two lines every real page should have: <meta charset="UTF-8">, so all characters display correctly, and the viewport line, so the page fits phone screens. Copy this as the starting point for every new page.

Good to know

  • Indent nested elements, as in the examples. Browsers do not care, but people reading your code do.
  • In VS Code, typing ! and pressing Tab writes this whole skeleton for you.
  • The browser reads the page from top to bottom, which is why CSS goes in the head and scripts often load with defer.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

HTML Structure exercises

Describe the page for search engines

Make a page with the title Weeknight Recipes, a description for search engines that says Dinners ready in 30 minutes., and the main heading Weeknight Recipes.

Result

Check your answer to see it here.