HTML Document Structure
HTML is text that describes the structure of a web page. A browser reads that text from top to bottom and works out which pieces sit inside which before it can show a heading or a list. That arrangement is the document's structure, and every page shares the same basic skeleton, from a one-line test file to a large news site. You can type the examples in this lesson into a code editor and save each one as a plain text file ending in .html. This lesson covers the parts of the skeleton, the rule for nesting one piece inside another, and how to read a page as a tree.
In these notes a tag is printed as ⟨p⟩. When you type it in a code editor, use the less-than and greater-than keys.
Elements and the nesting rule
An element is one piece of a page. In these notes every element that takes content is written in full, as a start tag, its content and a matching end tag. HTML allows some end tags to be left out, but writing all of them keeps the structure easy to see. The end tag repeats the element's name with a slash in front of it:
A start tag can also carry attributes, such as lang="en". A few void elements, such as meta, have no content and no end tag at all.
Elements sit inside other elements. An element's parent is the element directly enclosing it, and it is that parent's child. Any enclosing element, including the parent or one further up the tree, is an ancestor. An element that opens inside another must close before its parent closes, so when nested elements are written with both tags, the end tags appear in the reverse order of their start tags. In the table below, em marks emphasised text inside a heading.
Version | Code | What happens |
|---|---|---|
Correct | ⟨h1⟩Our ⟨em⟩new⟨/em⟩ site⟨/h1⟩ | em opens last and closes first |
Incorrect | ⟨h1⟩Our ⟨em⟩new site⟨/h1⟩⟨/em⟩ | h1 closes while em, its child, is still open |
The skeleton every page shares
The file below is a complete, valid page. The indentation is there for human readers: it shows the structure at a glance, but the tags, not the indents, decide which element is the parent of which.
Line | Code | Role |
|---|---|---|
1 | ⟨!DOCTYPE html⟩ | Tells the browser to read the file as modern HTML |
2 | ⟨html lang="en"⟩ | Opens the root element; the attribute names the page language |
3 | ⟨head⟩ | Opens the head |
4 | ⟨meta charset="utf-8"⟩ | Declares the UTF-8 character encoding; a void element |
5 | ⟨title⟩My First Page⟨/title⟩ | The page title, shown on the browser tab |
6 | ⟨/head⟩ | Closes the head |
7 | ⟨body⟩ | Opens the body |
8 | ⟨h1⟩Welcome⟨/h1⟩ | A main heading |
9 | ⟨p⟩This is my page.⟨/p⟩ | A paragraph |
10 | ⟨/body⟩ | Closes the body |
11 | ⟨/html⟩ | Closes the root element |
Line 1 is the doctype declaration. It is an instruction to the browser rather than an element, so it has no end tag and is not counted as part of the page's content. Everything else sits inside the html element, the root of the page. The root has exactly two children, head and body, and they divide the work between them.
Feature | head | body |
|---|---|---|
Intended for | Metadata: information about the page | Content the reader sees |
Typical children | title, meta | h1, p, ul, ol |
Where it shows up | The title is used by the browser itself, on the tab and in history and bookmarks, not as a heading inside the page | In the browser window, as the page itself |
Number per page | Exactly one | Exactly one |
Put everything the reader should see inside body. The head is meant for metadata such as the title and the character encoding, so visible page text belongs in the body, where it is drawn in the browser window.
Reading the document tree
Because every element except the root has exactly one parent, the whole page forms a document tree: html at the top, branching into head and body, then branching again into their children. Elements with the same parent, such as head and body, are siblings.
The tree gives each element a nesting level. Count the elements on the path from html down to the element, including both ends, so html is level 1, head and body are level 2, and their children are level 3. In a tidy file, each extra step of indentation adds one to the nesting level.
Example: In the skeleton above, the p element on line 9 sits inside body, which sits inside html. The path is html, body, p, so p is at nesting level 3.
What to remember
- In these notes an element with content is written in full: a start tag, content and an end tag. Void elements such as meta have only a start tag.
- When nested elements are written with both tags, the inner element closes first, so end tags appear in the reverse order of start tags.
- Every page has a doctype line, then one html root containing exactly one head and one body.
- The head holds metadata such as the title; the content the reader sees belongs in the body.
- In the document tree, html is level 1 and each child is one level below its parent.
Example: Tracing the structure of a recipe page
A student writes the page below for a banana bread recipe. In the body, ul makes a bulleted list, each li is one list item, and strong marks important text. Use the convention that html is nesting level 1.
Line | Code |
|---|---|
1 | ⟨!DOCTYPE html⟩ |
2 | ⟨html lang="en"⟩ |
3 | ⟨head⟩ |
4 | ⟨meta charset="utf-8"⟩ |
5 | ⟨title⟩Banana Bread⟨/title⟩ |
6 | ⟨/head⟩ |
7 | ⟨body⟩ |
8 | ⟨h1⟩Banana Bread⟨/h1⟩ |
9 | ⟨ul⟩ |
10 | ⟨li⟩3 ripe bananas⟨/li⟩ |
11 | ⟨li⟩2 cups of ⟨strong⟩flour⟨/li⟩⟨/strong⟩ |
12 | ⟨/ul⟩ |
13 | ⟨/body⟩ |
14 | ⟨/html⟩ |
a) Which lines make up the head, and how many elements does the head contain, not counting head itself?
The head opens with its start tag on line 3 and closes with its end tag on line 6. Between them sit line 4, the ________ element, and line 5, the ________ element. Meta is a void element, so it counts as one element even though it has no end tag.
The head runs from line 3 to line 6 and contains 2 elements.
b) What is the nesting level of the strong element on line 11?
Trace the parents from strong upward. strong opens inside the li on line 11. That li sits inside the ul that opens on line 9, and the parent of that ul is ________. body is a child of html, the root.
Counting down from the root: html is level 1, body is level 2, ul is level 3, li is level ________, and strong is one level deeper.
The strong element is at nesting level 5.
c) Line 11 breaks the nesting rule. Explain the error and write the corrected line.
On line 11 strong opens after li, so strong is the inner element. The inner element must close first, so the first end tag after the word flour should belong to ________. Instead the line closes li while strong is still open.
Swap the two end tags so they appear in the reverse order of the start tags:
Now li opens first and closes last, with strong completely inside it.
d) How many elements does the corrected document contain? Should line 1 be counted?
Line 1 is the doctype declaration. It is an instruction to the browser, not an element, so it is not counted.
Count by branch. The html root is 1 element. The head branch holds head, meta and title, which is ________ elements. The body branch holds body, h1, ul, two li elements and strong, which is ________ elements.
The corrected document contains 10 elements. Fixing line 11 changed the order of two end tags, not the number of elements.
In HTML, the p element marks a paragraph and the em element marks emphasised text. Which of the following lines nests its elements correctly?
Priya is building her first web page for a school club using only a plain-text editor and a web browser. Put her steps in the order she should carry them out, from starting the file to seeing a correction appear in the browser.
Loading ordering steps…