Sub topic 4
Headings and document outline
This chapter assumes you know basic HTML syntax, can open browser developer tools, and have read the previous chapter on page landmarks.
What headings do
The elements h1 to h6 label the content that follows them. The number is the heading's level. Level 1 is the highest. Screen reader users often move from heading to heading to scan a page, much as sighted users skim large text. The list of headings and their levels forms the page's document outline.
The working model
Think of the outline as a table of contents. An h2 belongs to the nearest earlier h1. An h3 belongs to the nearest earlier h2, and so on.
<main>
<h1>Growing tomatoes</h1>
<h2>Choosing a variety</h2>
<h3>Cherry tomatoes</h3>
<h3>Beefsteak tomatoes</h3>
<h2>Planting</h2>
<h2>Pests and diseases</h2>
</main>Rules to follow
- Give each page one
h1that names the page's main topic. - Do not skip levels when going deeper. An
h2followed directly by anh4leaves a gap in the outline. - Jumping back up is fine. An
h2after anh3starts a new sibling section. - Pick the level for the structure, not for the size. Use CSS to change how a heading looks.
- Put real text in every heading. An empty heading shows up in the outline as a blank entry.
- Do not use a heading for text that is merely large or bold. Use a
pwith a CSS class instead.
Headings and sectioning
Headings can sit inside article and section. They can also sit inside landmarks such as header. Each heading still carries its own explicit level.
<article>
<header>
<h2>Why semantics matter</h2>
</header>
<p>Meaningful elements help everyone.</p>
</article>Common errors
| Mistake | Fix |
|---|---|
| Choosing `h4` because the default text is smaller | Use the correct level and restyle it with CSS |
| Several `h1` elements used as styling shortcuts | Keep one `h1` and use `h2` for the main sections |
| Bold `p` or `div` styled to look like a heading | Replace it with a real heading element |
| A site logo or tagline marked up as `h1` on every page | Reserve `h1` for the page's own title |
| Headings used only to enlarge text in a sidebar | Use `p` or `strong` with CSS |
Checking your outline
Open the accessibility panel in your browser's devtools and inspect the heading nodes. Each one shows its role as heading and its level. Browser extensions that list headings give you the outline directly. Read it without looking at the page. If it makes sense as a table of contents, the structure is sound.
Exercise
A page has an h1, then an h2 titled "Specs", then an h4 titled "Battery", then an h2 titled "Reviews". Which heading breaks the outline rules, and what is the fix?
Show answer
The h4 "Battery" skips level 3. Change it to an h3 so it sits directly under "Specs". The later h2 "Reviews" is fine, because moving back up to a higher level is allowed.