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⟩\langle\texttt{p}\rangle. 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:


⟨h1⟩Welcome⟨/h1⟩\langle\texttt{h1}\rangle\texttt{Welcome}\langle\texttt{/h1}\rangle


A start tag can also carry attributes, such as lang="en"\texttt{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⟩\langle\texttt{h1}\rangle\texttt{Our }\langle\texttt{em}\rangle\texttt{new}\langle\texttt{/em}\rangle\texttt{ site}\langle\texttt{/h1}\rangle

em opens last and closes first

Incorrect

⟨h1⟩Our ⟨em⟩new site⟨/h1⟩⟨/em⟩\langle\texttt{h1}\rangle\texttt{Our }\langle\texttt{em}\rangle\texttt{new site}\langle\texttt{/h1}\rangle\langle\texttt{/em}\rangle

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⟩\langle\texttt{!DOCTYPE html}\rangle

Tells the browser to read the file as modern HTML

2

⟨html lang="en"⟩\langle\texttt{html lang="en"}\rangle

Opens the root element; the attribute names the page language

3

⟨head⟩\quad\langle\texttt{head}\rangle

Opens the head

4

⟨meta charset="utf-8"⟩\quad\quad\langle\texttt{meta charset="utf-8"}\rangle

Declares the UTF-8 character encoding; a void element

5

⟨title⟩My First Page⟨/title⟩\quad\quad\langle\texttt{title}\rangle\texttt{My First Page}\langle\texttt{/title}\rangle

The page title, shown on the browser tab

6

⟨/head⟩\quad\langle\texttt{/head}\rangle

Closes the head

7

⟨body⟩\quad\langle\texttt{body}\rangle

Opens the body

8

⟨h1⟩Welcome⟨/h1⟩\quad\quad\langle\texttt{h1}\rangle\texttt{Welcome}\langle\texttt{/h1}\rangle

A main heading

9

⟨p⟩This is my page.⟨/p⟩\quad\quad\langle\texttt{p}\rangle\texttt{This is my page.}\langle\texttt{/p}\rangle

A paragraph

10

⟨/body⟩\quad\langle\texttt{/body}\rangle

Closes the body

11

⟨/html⟩\langle\texttt{/html}\rangle

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.


A tree diagram with html at the top, head and body below it, meta and title under head, and h1 and p under body.


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⟩\langle\texttt{!DOCTYPE html}\rangle

2

⟨html lang="en"⟩\langle\texttt{html lang="en"}\rangle

3

⟨head⟩\quad\langle\texttt{head}\rangle

4

⟨meta charset="utf-8"⟩\quad\quad\langle\texttt{meta charset="utf-8"}\rangle

5

⟨title⟩Banana Bread⟨/title⟩\quad\quad\langle\texttt{title}\rangle\texttt{Banana Bread}\langle\texttt{/title}\rangle

6

⟨/head⟩\quad\langle\texttt{/head}\rangle

7

⟨body⟩\quad\langle\texttt{body}\rangle

8

⟨h1⟩Banana Bread⟨/h1⟩\quad\quad\langle\texttt{h1}\rangle\texttt{Banana Bread}\langle\texttt{/h1}\rangle

9

⟨ul⟩\quad\quad\langle\texttt{ul}\rangle

10

⟨li⟩3 ripe bananas⟨/li⟩\quad\quad\quad\langle\texttt{li}\rangle\texttt{3 ripe bananas}\langle\texttt{/li}\rangle

11

⟨li⟩2 cups of ⟨strong⟩flour⟨/li⟩⟨/strong⟩\quad\quad\quad\langle\texttt{li}\rangle\texttt{2 cups of }\langle\texttt{strong}\rangle\texttt{flour}\langle\texttt{/li}\rangle\langle\texttt{/strong}\rangle

12

⟨/ul⟩\quad\quad\langle\texttt{/ul}\rangle

13

⟨/body⟩\quad\langle\texttt{/body}\rangle

14

⟨/html⟩\langle\texttt{/html}\rangle


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:


⟨li⟩2 cups of ⟨strong⟩flour⟨/strong⟩⟨/li⟩\langle\texttt{li}\rangle\texttt{2 cups of }\langle\texttt{strong}\rangle\texttt{flour}\langle\texttt{/strong}\rangle\langle\texttt{/li}\rangle


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.


1+3+6=101 + 3 + 6 = 10


The corrected document contains 10 elements. Fixing line 11 changed the order of two end tags, not the number of elements.

Every standard HTML page is built from the same few parts, and each part has one job. Match each part of the document skeleton with the job it performs.

Match each term
A.

Root that contains every other element of the page

B.

Declares the character encoding, usually UTF-8

C.

Holds all the content that appears in the browser window

D.

Sets the text shown on the browser tab and in bookmarks

E.

First line of the file; tells the browser to read the page as modern HTML

F.

Holds information about the page that is not drawn in the window

head element

html element

title element

meta charset element

body element

DOCTYPE declaration

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

In HTML, the p element marks a paragraph and the em element marks emphasised text. Which of the following lines nests its elements correctly?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

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…

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors