Sub topic 3
Headings give the page an outline
This chapter assumes you know basic HTML tags and attributes, and that you can open a page in a browser and use its developer tools. You will learn how heading elements form an outline, how to keep that outline logical, and how to check it.
Why headings matter
Headings are the h1 to h6 elements. Each one names the content that follows it. Browsers, search engines, and screen readers all read them as a table of contents.
Screen reader users often move from heading to heading to scan a page, much as sighted users skim large text. A page with a clear outline is faster to scan for everyone.
The rules of a good outline
Follow these rules in each page:
- Use exactly one
h1. It names the page as a whole. On the bakery page it is the hero's headline. - Put each major section under an
h2. - Use
h3for subsections inside anh2section, and so on. - Never skip a level going down. An
h2should be followed by anh3, not anh4. - Choose a level for its meaning, not its size. Change the size with CSS.
Going back up is fine. After an h3 you can start a new h2.
An outline for the bakery page
The page from the skeleton in the earlier chapter has this structure:
h1: Fresh bread, baked every morningh2: Our specialtiesh3: Sourdoughh3: Ryeh2: Visit us
The markup that produces this outline looks like this:
<main>
<section class="hero" aria-labelledby="hero-title">
<h1 id="hero-title">Fresh bread, baked every morning</h1>
<p>Order ahead and pick up warm loaves on your way to work.</p>
</section>
<section aria-labelledby="specialties-title">
<h2 id="specialties-title">Our specialties</h2>
<article>
<h3>Sourdough</h3>
<p>Slow-fermented and baked until deeply golden.</p>
</article>
<article>
<h3>Rye</h3>
<p>Dense, dark, and good with butter.</p>
</article>
</section>
<section aria-labelledby="visit-title">
<h2 id="visit-title">Visit us</h2>
<p>Open every day from 6 a.m.</p>
</section>
</main>Each section has an aria-labelledby attribute that points to its heading's id. This gives the section an accessible name, so assistive technology can announce it as a region. The final chapters on accessibility return to this.
Style headings with CSS, not with levels
A common mistake is choosing h4 because it looks small enough. The fix is to keep the correct level and change its appearance:
h1 { font-size: 2.5rem; }
h2 { font-size: 1.75rem; }
h3 { font-size: 1.25rem; }
.card h2 {
font-size: 1.25rem;
}The last rule shrinks an h2 inside a card without changing its place in the outline.
Common errors
| Error | Why it hurts | Fix |
|---|---|---|
| Several `h1` elements | The page has no single main topic | Keep one `h1`, and demote the others to `h2` |
| Skipping from `h2` to `h4` | Readers think content is missing | Use `h3` and style it with CSS |
| Bold `p` text used as a heading | Assistive technology does not list it as a heading | Use a real heading element |
| Headings chosen for size | The outline no longer matches the content | Pick the level by meaning, then style it |
| Empty heading elements | Screen readers announce a heading with no text | Remove them or add text |
The site name in the header is usually a link or a logo, not a heading. The h1 belongs to the page's own topic. On pages other than the home page, the h1 should name that page, such as "Menu".
Checking your outline
Open the page and read only the headings, in order. If they make a sensible table of contents, the outline works. You can also list them in the browser console:
document.querySelectorAll("h1, h2, h3, h4, h5, h6").forEach((h) => {
const level = Number(h.tagName[1]);
console.log(" ".repeat(level - 1) + h.tagName + ": " + h.textContent.trim());
});The output is indented by level. Look for more than one h1, and for jumps of more than one level.
Exercise
A page has an h1, then an h2 called "Pricing", then an h4 called "Student plan" that was chosen because it looks small. What is wrong, and how do you fix it?
Show answer
The outline skips from h2 to h3, so the h4 implies a missing level. Change the h4 to an h3 so it is a direct subsection of "Pricing", then use a CSS rule such as h3 { font-size: 1rem; } to get the smaller look.
Exercise
Your page has an h1 in the header holding the site name and a second h1 in the hero holding the headline. What should you change?
Show answer
Keep one h1 only. Make the hero headline the h1, because it names the page's topic, and turn the site name into a plain link. Pages other than the home page should use the page's own title as the h1.