Sub topic 3
Build a Clean Heading Hierarchy
This chapter assumes you know basic HTML elements (headings, paragraphs, lists, tables), have seen how search engines display results, and have written or edited web content. You do not need to know how retrieval systems work internally. The earlier chapters in this tutorial cover that.
Why the heading hierarchy matters for AI answers
A heading hierarchy is the outline of a page, expressed through the elements h1 to h6. Retrieval-based answer systems often split a page into passages before they pick which ones to quote. Headings are one of the clearest signals for where one passage ends and the next begins. They also tell a system what each passage is about.
A clean hierarchy gives you three practical benefits:
- A passage keeps its context. A paragraph under "What does a factory reset do?" inside "How to Reset a Router" is easy to interpret even when it is lifted out of the page.
- Tools can build an outline. Screen readers, browser extensions, crawlers, and your own scripts can all read the outline without rendering the page.
- You can check it automatically. Heading levels are numbers, so a script can find jumps and duplicates.
No heading structure guarantees that a page is cited. Different systems chunk pages in different ways, and some ignore headings entirely. A clean hierarchy removes a source of ambiguity, and it also helps human readers and assistive technology.
The rules of a clean hierarchy
There are four rules. Each one is simple, and most real pages break at least one.
- Use one
h1per page. Theh1names the whole page. It should match the page's main topic, and it is usually close to the<title>element. - Nest levels in order. An
h2sits under theh1. Anh3sits under anh2. Each heading is a child of the nearest preceding heading with a lower number. - Do not skip levels going down. After an
h2, the next heading may be anotherh2, anh3, or a heading of a higher level such as anh1in a new document. It may not be anh4. - You may jump back up freely. After an
h4, the next heading can be anh2. That closes theh4andh3sections and starts a new top-level section.
The following diagram shows a valid outline as a tree.
Reading the tree from the top, every heading has exactly one parent, and every parent is exactly one level above its child. That property is what "no skipped levels" means.
A correct example
The next block shows a small article with a correct hierarchy. It uses one h1, an h2 phrased as a question, and an h3 nested beneath it. Each section opens with its direct answer.
<article>
<h1>How to Reset a Router</h1>
<section>
<h2>What does a factory reset do?</h2>
<p>A factory reset erases all saved settings and restores the router to its default configuration.</p>
<h3>When a factory reset is not needed</h3>
<p>A simple restart fixes most connection problems without erasing settings.</p>
</section>
</article>Notice three things:
- The
h3is placed after theh2that it refines. It is a subtopic of the factory reset question, not a separate topic. - Each heading is followed by a paragraph that answers it. The next chapters cover this answer-first habit in detail.
- The
sectionelement groups theh2and its content. This is a semantic container. It helps tools see where the section begins and ends, but it does not replace correct heading levels.
Choosing levels by meaning, not by size
The most common cause of a broken hierarchy is choosing a heading level because of how large it looks. The h1 to h6 elements describe the document's structure. They are not font sizes. If you want smaller text, use CSS and keep the correct level.
The table below shows the two directions of this mistake.
| Situation | What people do | Why it is a problem | What to do instead |
|---|---|---|---|
| A subheading looks too large | Change `h2` to `h4` to get smaller text | The outline now skips a level | Keep `h2` and reduce the size in CSS |
| A label should look like a heading | Wrap text in `div` with bold large text | Tools do not see a heading at all | Use the real heading element and style it |
| A sidebar title needs emphasis | Use `h1` for a second large title | The page now has two `h1` elements | Use `h2` or a lower level, or use a non-heading element |
A good test is to remove all CSS in your mind. If the outline still reads as a sensible table of contents, the levels are correct.
Where the h1 comes from in templates and CMSs
Many hierarchy errors come from templates rather than from authors. Check these three places first:
- Page title fields. Many content management systems render the page title as an
h1automatically. If authors then type anh1into the body, the page has two. Tell authors to start the body ath2. - Site-wide components. A logo, a banner, or a promotional block in the header is sometimes marked up as an
h1. The logo should be an image link, not a heading. - Widgets in sidebars and footers. "Related posts" and "Newsletter" blocks often use
h3orh4, which puts a low-level heading before the firsth2in the source order.
The source order matters because tools read the document from top to bottom. A footer widget that uses h2 will appear as a top-level section of your article, even though it is unrelated to the article.
Handling headings in repeated components
Cards, accordions, and tabs repeat the same structure many times. Decide once what level each repeated title should have, and base the choice on where the component sits in the outline.
- A list of product cards under an
h2called "Our plans" should useh3for each card title. - The same card component placed directly under the
h1should useh2.
If a component is reused at several places, make its heading level a parameter instead of hard-coding it. Many component libraries accept a prop such as level, and a template can compute it from context.
Accordions need extra care. A button that opens and closes a panel is not a heading. If the button label also names a section, put the button inside a heading element, so that the outline shows the section name.
Headings that should not exist
Not every piece of bold text is a heading. Use a heading only when text introduces a section of content. Avoid headings for:
- Short callouts or pull quotes.
- Text that is only visual decoration, such as a large number in a banner.
- Navigation labels, such as "Menu" or "Skip to content".
- Empty headings. An
h2with no text, or with only an icon and no accessible name, still appears in the outline.
If a heading is hidden with CSS display: none, most tools ignore it. Do not use hidden headings to stuff keywords. It makes the outline disagree with what readers see, which is the opposite of the goal here.
Check your outline with a script
You can read the outline of a page with a short script. The following Python program uses only the standard library. It extracts every heading with its level and reports any place where the level jumps down by more than one.
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:
self.buffer.append(data)
def handle_endtag(self, tag):
if self.level and tag == "h" + str(self.level):
self.headings.append((self.level, "".join(self.buffer).strip()))
self.level = None
def skipped_levels(headings):
return [
(prev_text, text)
for (prev, prev_text), (cur, text) in zip(headings, headings[1:])
if cur > prev + 1
]
html = "<h1>Guide</h1><h3>Details</h3><h2>Next steps</h2>"
parser = Outline()
parser.feed(html)
print(parser.headings)
print(skipped_levels(parser.headings))Running it prints two lines:
[(1, 'Guide'), (3, 'Details'), (2, 'Next steps')]
[('Guide', 'Details')]The first line is the outline. The second line lists each pair where the level jumped by more than one. Here the h3 "Details" follows the h1 "Guide" directly, so the pair is reported. The step from h3 back up to h2 is fine, because moving up is allowed.
What the script does not check
This script is a starting point. It has limits you should know:
- It does not count
h1elements. Add a check thatsum(1 for level, _ in headings if level == 1) == 1. - It reads static HTML. If your page builds headings with JavaScript, parse the rendered HTML from a browser instead.
- It treats a heading with nested tags, such as
<h2><span>Text</span></h2>, correctly, because it collects all text between the opening and closing tags. It does not detect empty headings, so check that the stripped text is not empty. - It cannot judge meaning. A heading called "More" passes every check and still helps no one.
Common errors and how to spot them
| Symptom in the outline | Likely cause | Fix |
|---|---|---|
| Two or more `h1` elements | Title rendered by the template and again in the body | Remove the extra `h1`, start body content at `h2` |
| `h1` followed directly by `h3` | Level chosen for size | Change to `h2`, restyle with CSS |
| Outline begins with `h3` or `h4` | Sidebar or banner appears first in the source | Change its level, or move it after the main content |
| Many headings with identical text | Repeated component, such as "Details" in each card | Make the text specific to each item |
| Headings that are empty or only icons | Decorative markup | Remove the heading element or add visible text |
Fix errors from the top of the outline downward. A change near the top often resolves several later symptoms at once.
A short review routine
Before you publish a page, run through this list:
- Open the page and list all headings, using a script like the one above or a browser extension that shows the outline.
- Confirm there is exactly one
h1, and that it matches the page topic. - Confirm that no heading jumps down more than one level.
- Read only the headings, top to bottom. Check that the sequence describes the page.
- Confirm that every heading has content beneath it, and that the content answers what the heading promises.
Step four is the one authors skip most often, and it catches the most useful problems. The next chapter looks at what makes that heading text useful.
Exercise
A page has this outline: h1 "Home Network Guide", h2 "Choosing a router", h4 "Wi-Fi 6", h3 "Setup", h2 "Troubleshooting". Which transition breaks the hierarchy, and how would you fix it?
Show answer
The jump from the h2 "Choosing a router" to the h4 "Wi-Fi 6" skips the h3 level. Change "Wi-Fi 6" to an h3 so it sits directly under the h2. Going from h4 back to h3 and from h3 to h2 is allowed, because moving up never skips a level in a harmful way. If the author wanted smaller text for "Wi-Fi 6", keep the h3 and reduce the size with CSS.
Exercise
Your CMS template prints the page title as an h1. An author also starts the article body with an h1. List two ways to fix it, and name the one you would choose.
Show answer
Option one: edit the article so the body starts at h2 and the template supplies the only h1. Option two: change the template so it does not print the title as an h1 when the body provides one. Choose option one, because it keeps the fix on the content side, works for every page that uses the template, and does not change the template's behavior elsewhere.
Exercise
Extend the Python script so it also reports the number of h1 elements and any heading whose text is empty. Write the two helper functions.
Show answer
def h1count(headings): return sum(1 for level, in headings if level == 1) def emptyheadings(headings): return [level for level, text in headings if not text] Call them with parser.headings. A valid page returns 1 from h1count and an empty list from empty_headings.