Sub topic 3
Build a Clean Heading Hierarchy
What should you know first?
This chapter assumes that you know basic HTML headings and understand how search engines display page content. It focuses on structure, not visual design. CSS controls how a heading looks. HTML heading levels express how a section fits into the page.
A clean hierarchy helps people scan a page and helps retrieval-based AI answer systems interpret passages in context. These systems often split a page into smaller passages. A heading gives each passage a label and shows its relationship to nearby content.
How should heading levels relate?
Use one h1 for the page's main subject. Use h2 for major sections. Use h3 for sections inside an h2. Continue with h4 only when an h3 contains distinct subsections.
Do not choose a heading level because its text looks the right size. Choose it because of its place in the document outline. Use CSS to change its appearance.
| Heading | Structural purpose | Example |
|---|---|---|
| `h1` | The page's main subject | Build a Clean Heading Hierarchy |
| `h2` | A major section | How should heading levels relate? |
| `h3` | A subsection of the current `h2` | When is `h3` useful? |
| `h4` | A subsection of the current `h3` | Handling nested examples |
A level should not normally jump over the level above it. An h1 followed directly by an h3 suggests that the h3 belongs to an unseen h2. A later h2 does not repair that earlier gap.
<article>
<h1>Structuring Product Documentation</h1>
<section>
<h2>Plan the page outline</h2>
<p>Start by listing the questions the page must answer.</p>
<h3>Group related questions</h3>
<p>Place questions about the same task in one major section.</p>
</section>
<section>
<h2>Review the finished structure</h2>
<p>Check that every section has a clear relationship to the page topic.</p>
</section>
</article>When is `h3` useful?
An h3 is useful when a major section contains separate, meaningful parts. For example, an h2 named “Plan the page outline” might contain h3 sections for “Group related questions” and “Order the sections.”
Do not add headings only to create visual spacing. A heading should introduce content that could be identified as a section. If a short paragraph does not need its own topic, style the paragraph instead.
Headings should also describe the answer that follows. Question-style headings work well when readers are looking for a specific fact:
h2: How many heading levels can HTML use?h2: Why should headings not be chosen for their size?h3: When is anh4necessary?
A descriptive statement can work just as well:
h2: HTML has six heading levelsh2: CSS controls heading appearanceh3: Deep nesting can make pages harder to scan
The heading should remain useful if an AI system extracts the section without the rest of the page.
How can you audit skipped levels?
Manual review catches most problems. First, view the page's heading outline. Confirm that there is one h1, that each heading describes its section, and that the sequence does not jump from h1 to h3 or from h2 to h4.
A small script can detect skipped levels:
from html.parser import HTMLParser
class Outline(HTMLParser):
def __init__(self):
super().__init__()
self.level = None
self.buffer = []
self.headings = []
def handle_starttag(self, tag, attrs):
if len(tag) == 2 and tag[0] == "h" and tag[1] in "123456":
self.level = int(tag[1])
self.buffer = []
def handle_data(self, data):
if self.level is not None:
self.buffer.append(data)
def handle_endtag(self, tag):
if self.level is not None and tag == f"h{self.level}":
text = "".join(self.buffer).strip()
self.headings.append((self.level, text))
self.level = None
def skipped_levels(headings):
problems = []
for previous, current in zip(headings, headings[1:]):
if current[0] > previous[0] + 1:
problems.append((previous, current))
return problems
html = "<h1>Guide</h1><h2>Setup</h2><h4>Advanced setup</h4>"
parser = Outline()
parser.feed(html)
print(parser.headings)
print("Skipped levels:", skipped_levels(parser.headings))This check reports structural jumps, but it cannot decide whether a heading is accurate or useful. Read the headings as a list as well as in the rendered page. If the list alone does not explain the page's subject and progression, revise the wording or grouping.
Exercise
What is wrong with a page that uses one h1, then an h3 for “Installation,” and then an h2 for “Troubleshooting”?
Show answer
The h3 skips the h2 level, so “Installation” has no clear structural parent. Change it to h2 unless it belongs inside a preceding h2 section.