Sub topic 7
Building a logical heading hierarchy
This chapter assumes you know basic HTML (head, body, common tags), what a search results page looks like, and what a keyword is. You have already written a title and an h1. Now you will build the headings below the h1.
What a heading hierarchy is
Headings run from h1 to h6. The number shows the level. An h1 names the whole page. An h2 names a main section. An h3 names a part of an h2 section, and so on. Together the headings form an outline of the page.
Search engines and readers both use this outline. A reader scans it to find the part they need. A search engine reads it as a hint about what each section covers.
Rules that keep the outline clean
- Use one h1 per page. It states the main topic.
- Use h2 for the main sections of the page.
- Use h3 only inside an h2 section, to split that section into parts.
- Do not skip levels. An h2 must not be followed directly by an h4.
- Pick a level for its meaning in the outline, not for its size. Control size with CSS.
Here is an outline for a page about repotting a snake plant.
<body>
<h1>How to Repot a Snake Plant</h1>
<h2>When to repot</h2>
<h2>What you need</h2>
<h3>Choosing a pot</h3>
<h3>Choosing soil</h3>
<h2>Step-by-step repotting</h2>
<h2>Aftercare</h2>
</body>The h1 matches the target keyword, "repot a snake plant". Each h2 is a step a reader takes. The two h3 headings sit under "What you need" because they are both things you need.
Common errors
The next block shows a weak outline with three problems.
<!-- Weak: repeated keyword, skipped level, vague headings -->
<title>Snake plant | snake plant repotting | repot snake plant | best snake plant</title>
<h1>Snake Plant</h1>
<h4>Snake plant repotting tips snake plant</h4>
<h2>More</h2>- Skipped level. The h4 follows the h1 with no h2 or h3 between them. The outline has a gap, and screen reader users who move by heading level will notice it.
- Vague heading. "More" says nothing about the section. A reader scanning the page cannot tell what it holds.
- Stuffed heading. "Snake plant repotting tips snake plant" repeats the keyword and reads badly. Chapter 9 covers this in detail.
- Vague h1. "Snake Plant" is a broad topic, not the task the page covers.
To fix the outline, give each heading a clear job and put the sections in order. Compare the result with the first block: the h1 states the task, the h2 headings name the main steps, and no level is skipped.
Writing headings that work
Write each heading as a label for the text under it. A good heading tells the reader what they will find. Where it fits naturally, use the target keyword or a related phrase, but do not force it into every heading. "When to repot" is a good h2 even though it does not contain the full keyword, because the h1 already carries it and the section is plainly about repotting.
A quick test: read only the headings, top to bottom. If you can tell what the page teaches and in what order, the hierarchy works.
Checking your hierarchy
You can list the headings of a page with a short script. This one reads an HTML string and prints each heading with indentation, then warns if a level is skipped.
from html.parser import HTMLParser
class Outline(HTMLParser):
def __init__(self):
super().__init__()
self.level = None
self.last = 0
def handle_starttag(self, tag, attrs):
if len(tag) == 2 and tag[0] == "h" and tag[1] in "123456":
self.level = int(tag[1])
def handle_data(self, data):
if self.level and data.strip():
skipped = self.level > self.last + 1
note = " <-- skipped level" if skipped else ""
print(" " * (self.level - 1) + f"h{self.level}: {data.strip()}{note}")
self.last = self.level
self.level = None
html = """
<h1>Snake Plant</h1>
<h4>Snake plant repotting tips snake plant</h4>
<h2>More</h2>
"""
Outline().feed(html)Running it prints the h4 line with the skipped-level note. The h2 after it is not flagged, because going back up the outline is allowed.
Exercise
A page has an h1, then an h3, then an h2, then another h3. Which heading breaks the rules, and how do you fix it?
Show answer
The first h3 breaks the rules because it follows the h1 directly and skips the h2 level. Change it to an h2, or put an h2 section above it so the h3 sits inside that section. The later h3 is fine because it follows an h2.
Exercise
Write an h2 and two h3 headings for a section about choosing soil on the snake plant page. Do not repeat the full keyword in each heading.
Show answer
One valid answer: h2 "Choosing the right soil", h3 "What a good mix contains", h3 "Mixes to avoid". Each heading names what the section holds, and none of them is stuffed with the keyword.