Sub topic 4
Headings and document outline
This chapter assumes you know basic HTML syntax, have used browser developer tools, and have read the earlier chapters on landmarks. It covers how the six heading elements build a document outline, how assistive technology uses that outline, and how to avoid the usual mistakes.
What headings do
HTML has six heading elements: h1 to h6. The number is the heading's level. Level 1 is the highest, and each lower number-to-higher number step means a more specific subtopic. The element name states two things: this text is a heading, and it sits at this depth in the document.
Headings are not a way to get big or bold text. Size and weight belong to CSS. If you pick h3 because it looks right, you have changed the meaning of the page for everyone who doesn't see the styling.
Three groups use headings as structure rather than decoration:
- Screen reader users can list all headings on a page and jump between them. Many use this as their main way to scan a page.
- Search engines use headings as a signal of what each part of a page is about.
- Browser features such as reader modes and table-of-contents tools build navigation from them.
The outline you can rely on
The document outline is the tree you get by reading the headings in source order and nesting each one under the nearest preceding heading of a higher level.
HTML5 once defined an algorithm that computed outline depth from sectioning elements (section, article, nav, aside), so an h1 inside each section would automatically count as a lower level. No browser or assistive technology ever implemented it, and the specification has since removed it. Today the outline comes only from the heading level numbers you write. Do not rely on nesting h1 elements inside sections.
Rules that keep the outline sound
- Use one
h1per page, and make it name the page. HTML allows several, but a singleh1gives screen reader users an unambiguous starting point. It often matches thetitletext, though it need not be identical. - Do not skip levels going down. Move from
h2toh3, not fromh2toh4. A skipped level suggests a missing section. - You may jump back up any distance. After an
h4subsection, the next section can start ath2. That closes the deeper sections. - Write headings that make sense alone. In a headings list, "More" or "Details" says nothing. "Shipping costs" does.
- Never leave a heading empty. An empty
h2is announced as a heading with no name, or gets skipped. - Keep the order meaningful. The outline follows source order, not visual position. If CSS moves a heading visually, check that the source order still tells the right story.
A correct page outline
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Semantic HTML guide</title>
</head>
<body>
<header>
<p><a href="/">Example Site</a></p>
</header>
<main>
<h1>Semantic HTML guide</h1>
<p>A short guide to writing markup that says what it means.</p>
<section aria-labelledby="landmarks-title">
<h2 id="landmarks-title">Landmarks</h2>
<p>Landmarks name the large regions of a page.</p>
</section>
<section aria-labelledby="headings-title">
<h2 id="headings-title">Headings</h2>
<h3>Levels</h3>
<p>Each level is one step deeper than its parent.</p>
<h3>Common errors</h3>
<p>Skipped levels and styling-driven choices.</p>
</section>
</main>
</body>
</html>The site name in the header is a paragraph, not a heading. The h1 belongs to the page's content, so it sits in main. Each section is labelled by its heading through aria-labelledby. A section with an accessible name is exposed as a region landmark, and one without a name is not. The next chapter covers when to choose section or article.
Choosing level versus appearance
When the default size of a heading is wrong for your design, change it with CSS and keep the element that matches the structure.
<style>
.card-title {
font-size: 1rem;
font-weight: 600;
}
</style>
<h2>Related articles</h2>
<article>
<h3 class="card-title">Why semantics matter</h3>
<p>Elements describe content; CSS controls appearance.</p>
</article>Here the card title is small, but it is still a level-3 heading under "Related articles". The reverse mistake is common: a designer wants small, bold text, so the author writes <h5> for a caption that is not a heading at all. If the text does not title a section of content, use a p, strong, or figcaption instead.
Subtitles and taglines with `hgroup`
A page title often has a subtitle or tagline. Putting the tagline in a second heading adds a false entry to the outline. The hgroup element groups a heading with one or more paragraphs that belong to it, so only the heading counts as a heading.
<hgroup>
<h1>Semantic HTML</h1>
<p>Markup that says what it means</p>
</hgroup>Support in assistive technology has been uneven. The markup is valid and harmless, but do not depend on hgroup to change how a heading is announced.
Checking your outline in practice
The fastest check is to print the outline from the page itself. Paste this into the browser console on any page.
document.querySelectorAll("h1, h2, h3, h4, h5, h6").forEach((h) => {
const level = Number(h.tagName[1]);
const text = h.textContent.trim() || "(EMPTY)";
console.log(" ".repeat(level - 1) + h.tagName + ": " + text);
});The indented output shows the tree. Look for these problems:
| Symptom in the output | Likely cause | Fix |
|---|---|---|
| More than one `H1` | Logo or widget marked as `h1` | Change the extra ones to `p` or a lower level |
| `H2` followed by `H4` | Level chosen for appearance | Use `h3`, then restyle with CSS |
| `(EMPTY)` entry | Heading holding only an icon or image with no alt | Add text or an `alt`, or remove the element |
| Headings for the footer or menus that are not titles | Used for styling | Use `p`, or a visually hidden label if one is needed |
| Output starts at `H3` | Fragment pasted without its parent | Add the missing higher levels |
Browser devtools show the same structure in the accessibility pane. In Chrome and Firefox, the accessibility tree lists each heading with the role "heading" and its level. Validators, including the W3C Nu HTML Checker and tools such as axe and Lighthouse, report skipped levels and empty headings.
When a native heading is not possible
Sometimes you cannot use an h1–h6 element, for example when a component library renders a div. You can add role="heading" and aria-level="3" to give it heading semantics. This is a repair, not a first choice. You must set the level yourself, and nothing keeps it in sync with the surrounding structure. Prefer the native element whenever you control the markup.
Summary of the working model
- The heading level number is the only thing that sets outline depth.
- Pick the level from the content structure, then style it with CSS.
- Use one
h1, go down one level at a time, and avoid empty or vague headings. - Check the result with a console script, the accessibility pane, and a validator.
Exercise
A page has an h1, then an h2 "Pricing", then an h4 "Student plan" that the author chose because it renders in a smaller font. Name the problem and give the fix.
Show answer
The h4 skips level 3, so the outline implies a missing subsection between "Pricing" and "Student plan". Change the element to h3 so it nests directly under the h2, then give it a CSS class (for example font-size: 1rem) to get the smaller appearance.
Exercise
Why does putting a tagline in a second h2 under the page's h1 harm the outline, and what markup can you use instead?
Show answer
The tagline is not the title of a section of content, but a second h2 adds a false entry to the headings list that screen reader users navigate. Keep the tagline as a paragraph, and group it with the h1 in an hgroup if you want to show they belong together.
Exercise
A component renders a section title as a div with the class "title". You cannot change the markup. What attributes make it a level-2 heading for assistive technology, and why is this a second choice?
Show answer
Add role="heading" and aria-level="2". It is a second choice because the level is hard-coded and nothing keeps it consistent with the rest of the outline, whereas a native h2 states both role and level and is supported everywhere.