Sub topic 3
Headings give the page an outline
This chapter assumes you know basic HTML tags and attributes, can write simple CSS selectors, and can open a page in a browser and use its developer tools. It covers how heading elements turn a page into an outline, how to keep that outline logical, and which mistakes cause the most trouble.
Why headings matter
Headings are the h1 through h6 elements. They do more than make text big and bold. Browsers, search engines, and assistive technology read them as the outline of the page.
Sighted visitors scan headings to decide whether a page has what they need. Screen reader users do the same by ear. Screen readers can list every heading on a page and jump straight to one, and many people use this as their main way to move around. If the headings are missing or out of order, that shortcut stops working.
So a heading's level is a statement about structure, not about looks. Choose the level that matches where the text sits in the outline. Then use CSS to control size and weight.
The rules of a good outline
A sound heading outline follows a few rules.
- Use one
h1per page. It names what the whole page is about. - Do not skip levels. An
h2is followed by anh3, not anh4. - Nest by meaning. An
h3belongs to the nearesth2before it. - Make each heading describe the content that follows it.
- Do not choose a level for its default size.
The h1 is the top of the outline. On the bakery page from the skeleton, it is the hero message. Each major section of the page then gets an h2. If a section has subtopics, those get h3 elements, and so on.
Going back up is allowed. After an h3, you may start a new h2 for the next major section. The rule is about not jumping down by more than one level.
A worked outline
Here is the bakery page with only its headings shown, indented to reveal the outline.
<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>
<a href="/menu">See the menu</a>
</section>
<section aria-labelledby="specialties-title">
<h2 id="specialties-title">Our specialties</h2>
<h3>Breads</h3>
<p>Sourdough, rye, and seasonal loaves.</p>
<h3>Pastries</h3>
<p>Croissants, danishes, and cinnamon rolls.</p>
</section>
<section aria-labelledby="visit-title">
<h2 id="visit-title">Visit us</h2>
<p>Open every day from 6 a.m. to 3 p.m.</p>
</section>
</main>Read only the headings and you get this outline:
- Fresh bread, baked every morning (
h1) - Our specialties (
h2) - Breads (
h3) - Pastries (
h3) - Visit us (
h2)
The outline can be drawn as a tree.
If a reader scanned only this tree, they would still understand what the page offers. That is the test of a good outline.
Where headings sit in the page parts
The four parts of the page relate to headings in different ways.
| Part | Typical heading | Notes |
|---|---|---|
| Header | Usually none | The site name is often a link, not a heading. |
| Hero | The `h1` | States the main message of the page. |
| Content sections | `h2`, with `h3` inside | Each section names its topic. |
| Footer | Optional `h2` for link groups | Often unnecessary; use `aria-label` on a footer `nav` instead. |
The header usually has no heading. The site name there is a link back to the home page, and the page's real title is the hero h1. Some sites make the logo the h1 on every page. That makes every page claim the same main topic, which weakens the outline.
On a home page, the hero heading often works as the h1. On inner pages, such as a menu page, the h1 is the page's own title, for example "Our menu", and the hero may be shorter or absent.
Headings and the `section` element
A section groups related content. A section without a name does not do much for assistive technology, so give each one an accessible name. The simplest way is aria-labelledby, which points to the id of the section's heading.
<section aria-labelledby="specialties-title">
<h2 id="specialties-title">Our specialties</h2>
<p>Sourdough, rye, and seasonal pastries.</p>
</section>With this markup, browsers expose the section as a region named "Our specialties". Screen reader users can then jump to it as a landmark, and the name comes from text already on the page, so the two cannot drift apart.
Do not name every section. Landmarks are useful because there are few of them. If a page has twenty named regions, the list becomes noise. Name the main sections and leave small groupings as plain div or unnamed section elements.
Styling headings without changing the outline
A common cause of broken outlines is choosing a level for its default look. Suppose a card heading is too large as an h2, so someone uses an h5. The page now jumps from h2 to h5, and the outline is wrong.
Fix the size in CSS, not in the markup.
h1 {
font-size: 2.5rem;
line-height: 1.2;
}
h2 {
font-size: 1.75rem;
}
h3 {
font-size: 1.25rem;
}
.card h3 {
font-size: 1.1rem;
margin: 0 0 0.5rem;
}The reverse also happens. If you want a line of text to look like a heading but it is not one, do not wrap it in a heading element. Use a p or a span with a class.
<p class="eyebrow">Baked daily</p>
<h2>Our specialties</h2>.eyebrow {
font-size: 0.875rem;
text-transform: uppercase;
letter-spacing: 0.08em;
margin: 0;
}Here, "Baked daily" is a decorative label. It is not a title for any content, so it is not a heading.
Common errors
The following mistakes appear on real pages often.
- No
h1, or several unrelated ones. HTML allows more than oneh1, but the heading outline algorithm that was meant to make nestedh1elements work was never implemented by browsers or assistive technology. In practice, treat each page as having a singleh1. - Skipped levels. Going from
h2toh4leaves readers wondering whether they missed a section. - Headings used for style. Bold, large text in a
divthat is really a heading, or a heading that is really a caption. - Empty headings. An
h2that contains only an icon or nothing at all is still announced as a heading, with no name. - Headings inside components that ignore context. A reusable card that always uses
h3is wrong when it sits directly under anh1. Let the page decide the level, or accept the level as a parameter in your template. - Headings that are too long. A heading that is a full paragraph is hard to scan. Keep it short and put detail in the text below.
Checking your outline
You can check the outline without special tools.
- Open the page and open developer tools.
- In the Elements panel, search for
h1, h2, h3, h4, h5, h6or use the console. - Read the headings in order and check the levels.
This snippet prints the outline to the browser console, with indentation showing the level.
document.querySelectorAll("h1, h2, h3, h4, h5, h6").forEach((heading) => {
const level = Number(heading.tagName[1]);
const indent = " ".repeat(level - 1);
console.log(`${indent}h${level}: ${heading.textContent.trim()}`);
});For the bakery page it prints:
h1: Fresh bread, baked every morning
h2: Our specialties
h3: Breads
h3: Pastries
h2: Visit usLook for a missing h1, a level that jumps by two or more, and headings that make no sense when read alone. Browser accessibility tools and extensions can also list headings, and they show the same information.
Summary of decisions
When you are unsure about a heading, ask these questions in order.
- Does this text name the content that follows it? If not, it is not a heading.
- What is the nearest heading above it that this content belongs to? Use the next level down.
- Is it too big or too small? Change the CSS, not the level.
Exercises
Exercise
A page has an h1, then an h2 called "Pricing", then an h4 called "Monthly plan" because the designer wanted smaller text. What is wrong, and how do you fix it?
Show answer
The outline skips from h2 to h4, so assistive technology users will think a level is missing. Change the "Monthly plan" heading to an h3 and use CSS, for example h3 { font-size: 1.1rem; } or a class on that heading, to get the smaller size.
Exercise
A site puts its logo in an h1 on every page, and the hero on each page also has an h2 for its main message. Why is this a weak outline, and what would you change?
Show answer
Every page then has the same h1, so the h1 does not say what the individual page is about, and the real page message is demoted to an h2. Make the logo a plain link, and make the hero message the page's h1.
Exercise
Write a snippet of HTML for a section named "Our specialties" that has two subsections, "Breads" and "Pastries". Name the section so it is exposed as a landmark.
Show answer
<section aria-labelledby="specialties-title"><h2 id="specialties-title">Our specialties</h2><h3>Breads</h3><p>Sourdough and rye.</p><h3>Pastries</h3><p>Croissants and danishes.</p></section>