Sub topic 7
Building a Logical Heading Hierarchy
This chapter assumes you can read basic HTML (head, body, common tags) and know what a search query is. It covers how h2 to h6 headings form an outline, how that outline supports your target keyword, and the errors that break it.
What a heading hierarchy is
A heading hierarchy is the outline of a page, expressed through the heading elements h1 to h6. The h1 names the whole page. Each h2 names a major section. Each h3 names a part of the h2 above it, and so on down to h6.
Think of a book's table of contents. The book title is the h1. Chapters are h2. Sections inside a chapter are h3. A reader can scan the contents and decide where to go. Search engines and screen readers do the same thing with your headings.
The levels express containment, not size. Browsers give headings default font sizes, but you should choose a level by where the topic sits in the outline and set the visual size with CSS.
The working model
Three rules cover most of the work:
- One topic per heading. A heading should describe the content that follows it, and only that content.
- Children sit inside parents. An
h3must be a subtopic of the nearesth2above it. - No skipped levels. After an
h2, the next heading is either anotherh2or anh3. It is never anh4.
Here is the outline from the snake plant page, with the nesting shown:
<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>Reading only the headings, you can tell what the page covers and in what order. "Choosing a pot" and "Choosing soil" belong to "What you need", so they are h3. "Aftercare" is a peer of "When to repot", so it is h2.
Why the hierarchy matters
Search engines read headings as signals of what each part of a page is about. A clear outline helps them match a section to a specific query. For example, a heading "Choosing soil" under a repotting guide can help the page qualify for a query about snake plant soil.
Google has said that heading order is not a strict ranking factor, and that pages with imperfect heading order can still rank. Treat the hierarchy as a clarity tool for readers, assistive technology, and crawlers, not as a lever. The practical payoff is indirect: a well-organised page is easier to scan, which keeps readers on the page, and its sections are easier for search engines to extract, for example as featured snippets.
Where the target keyword goes
Your target keyword (here, "repot a snake plant") belongs in the h1, as the previous chapter explained. In the h2 and h3 headings, the goal is different. The headings should cover the subtopics a reader expects, and the keyword or its variations appear where they fit the sentence.
Compare two ways of writing the same h2:
- "Snake plant repotting step by step" repeats the keyword and reads awkwardly.
- "Step-by-step repotting" is shorter, and the page context already supplies "snake plant".
The h1 has already established the subject, so section headings do not need to restate it. Add the keyword to a subheading only when it clarifies the section, such as "When to repot a snake plant" on a page that also discusses other plants.
Choosing headings from search intent
A reliable way to build the outline is to start from the questions a reader has. For a how-to page, those are usually when, what, how, and what next. Each question becomes an h2.
Sources for these questions include:
- The "People also ask" box on a results page for your keyword.
- The headings of pages that already rank for the query.
- Questions from customers, comments, or support messages.
Use competitor headings to find gaps and common expectations, not to copy. If every ranking page has a section on drainage holes and yours does not, your outline is probably missing something readers want.
Common errors
Skipped levels
Jumping from h2 to h4 leaves a hole in the outline. Screen reader users who navigate by heading level will wonder whether they missed a section. This is the bug in the "before" example from the topic's code samples:
<!-- 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>Three problems appear in the headings. The h1 is vague. The h4 appears directly after the h1, skipping h2 and h3, and it repeats the keyword. The h2 "More" says nothing about its content.
A repaired version looks like this:
<title>How to Repot a Snake Plant: Step-by-Step Guide</title>
<h1>How to Repot a Snake Plant</h1>
<h2>Repotting tips</h2>
<h2>Common repotting mistakes</h2>Using headings for styling
Authors sometimes pick h4 because it looks right, or wrap a pull quote in an h3 for its size. This puts false entries in the outline. If text is not a section title, use a p or a div with a CSS class instead.
The reverse also happens: a section title built from a bold p has no heading semantics, so it does not appear in the outline at all.
Vague headings
Headings like "More", "Overview", "Tips", or "Other" give no information in the outline. Test a heading by reading it alone. If you cannot tell what its section contains, rewrite it. "Common repotting mistakes" beats "Mistakes", and "How much to water after repotting" beats "Watering" when the page has several watering topics.
Too deep or too flat
A page with only h1 and a long run of h2 headings may be missing structure that h3 could provide. A page that uses h5 and h6 often has sections that should be split into separate pages. Most articles need only h1 to h3. Go deeper only when the content really nests.
Multiple h1 elements
HTML5 allows more than one h1 in some structures, but browsers and assistive technologies do not apply the nested-outline rules consistently. Use one h1 per page. The previous chapter covers this in more detail.
Edge cases
- Headings in templates. Navigation, footers, and sidebars often contain headings such as "Related posts" or "Newsletter". These add entries to the outline. Give them a level that fits the whole page (usually
h2), or leave them as non-heading elements with a label. - Headings inside components. Reusable components, such as cards, may hard-code an
h3. Placed under anh1with noh2, they skip a level. Let the component accept a level as a setting. - Headings hidden by CSS. Content hidden with
display: noneis not read by screen readers. Do not hide a heading from visitors yet expect it to organise the page for crawlers. Hidden text can also look like manipulation. - Accordion and tab content. Headings inside collapsed sections still count as part of the page. Keep them in order, and write them as real headings, not as button labels alone.
- Heading text from images. Text drawn inside an image is not a heading. Use real text.
- Length. There is no official limit, but a heading that wraps across several lines is hard to scan. Aim for a short phrase.
Checking your outline
You can check any page by extracting its headings and reading only that list. This script prints the outline and reports skipped levels. It uses only the Python standard library.
from html.parser import HTMLParser
HEADINGS = {"h1", "h2", "h3", "h4", "h5", "h6"}
class OutlineParser(HTMLParser):
def __init__(self):
super().__init__()
self.current = None
self.items = []
def handle_starttag(self, tag, attrs):
if tag in HEADINGS:
self.current = [int(tag[1]), ""]
def handle_data(self, data):
if self.current is not None:
self.current[1] += data
def handle_endtag(self, tag):
if tag in HEADINGS and self.current is not None:
self.items.append((self.current[0], self.current[1].strip()))
self.current = None
def check(html):
parser = OutlineParser()
parser.feed(html)
previous = 0
h1_count = 0
for level, text in parser.items:
print(" " * (level - 1) + f"h{level}: {text}")
if level == 1:
h1_count += 1
if level > previous + 1:
print(f" ^ skipped level: h{previous} to h{level}")
previous = level
if h1_count != 1:
print(f"Expected one h1, found {h1_count}")
sample = """
<h1>Snake Plant</h1>
<h4>Snake plant repotting tips snake plant</h4>
<h2>More</h2>
"""
check(sample)Running it on the weak sample prints:
h1: Snake Plant
h4: Snake plant repotting tips snake plant
^ skipped level: h1 to h4
h2: MoreThe script finds structural errors only. It cannot tell you whether "More" is a vague heading. That check needs a human reading the list.
Summary of decisions
| Situation | Decision |
|---|---|
| Section is a major step or question | Use `h2` |
| Section is part of an `h2` topic | Use `h3` under that `h2` |
| Text looks like a heading but names no section | Use `p` or `div` with CSS |
| Heading level is chosen by font size | Choose by outline position, size with CSS |
| Component hard-codes a level | Pass the level in as a setting |
| Keyword already in `h1` | Leave it out of `h2` unless it clarifies |
Exercises
Exercise
A page has this heading order: h1, h2, h3, h3, h5, h2. Which heading breaks the hierarchy, and what is the smallest fix?
Show answer
The h5 breaks it, because it follows an h3 and skips h4. If the content is a subtopic of the preceding h3, change the h5 to an h4. If it is a peer of the h3 headings, change it to an h3.
Exercise
You are writing a guide titled "How to Repot a Snake Plant". A colleague proposes the h2 "Snake plant repotting soil for snake plants". Rewrite it and explain your change.
Show answer
"Choosing soil" (as an h3 under "What you need") or "What soil to use" as an h2. The h1 already names the snake plant, so repeating it adds noise. The rewrite describes the section in plain words and fits the page outline.
Exercise
A reusable "related article" card always renders its title as an h3. It appears on pages that have only an h1 above it. What problem does this cause and how do you fix it?
Show answer
It skips the h2 level, so the outline jumps from h1 to h3. Make the card's heading level a parameter, so each page can set it to h2, or h3 when it sits inside an h2 section.