Sub topic 4
Headings: levels, order, and emphasis
This chapter assumes you know basic HTML elements, basic CSS selectors, and how font-size, font-weight, and color work. It covers how heading levels form an outline, how to avoid common ordering errors, and how to style headings so the visual emphasis matches that outline.
Headings form an outline
HTML has six heading elements, <h1> through <h6>. The number is a rank, not a size. An <h2> names a section inside the page's <h1>, and an <h3> names a subsection inside an <h2>.
Browsers and assistive technology read these ranks to build an outline of the page. Screen reader users often jump from heading to heading to scan a page, much as sighted readers scan large text. If the ranks are wrong, that shortcut gives them a misleading map.
Rules for levels and order
Follow these rules and the outline will stay sound.
- Use one
<h1>per page. It names the page's main subject, usually the same idea as the<title>. - Go down one level at a time. An
<h2>may be followed by an<h3>, but not directly by an<h4>. - Going back up can skip levels. After an
<h4>, a new<h2>starts a new top-level section. - Choose the level by the content's place in the outline, not by how large you want the text to look.
Here is the structure from the topic's example, with the rules applied:
<main>
<h1>Trail Guide</h1>
<p>A short introduction to planning a multi-day hike.</p>
<h2>Gear</h2>
<p>Start with footwear, then add layers.</p>
<h2>Food</h2>
<h3>Breakfast</h3>
<p>Oats keep well and cook quickly.</p>
</main>The levels are h1, h2, h2, h3. Nothing is skipped, and each heading has content under it.
Common errors
| Error | Why it hurts | Fix |
|---|---|---|
| Choosing `<h4>` because it is small | The outline now has a gap, and screen reader users may think a section is missing | Use the correct level, then shrink it with CSS |
| Using `<b>` or a styled `<div>` as a heading | It looks like a heading but is not in the outline | Use a real heading element |
| Using a heading for a tagline or a pull quote | It adds a false section to the outline | Use `<p>` and style it |
| Several `<h1>` elements for visual impact | The page has no single main subject | Keep one `<h1>` and use `<h2>` for the rest |
| Empty headings left in a template | Assistive technology announces a heading with no text | Remove them |
Emphasis comes from CSS, not from the level
Once the levels are right, you create emphasis with size, weight, spacing, and color. Larger headings should belong to higher levels so the visual order agrees with the outline. Reuse the type scale from the earlier chapter, with its custom properties in rem units.
:root {
--text-base: 1rem;
--text-lg: 1.25rem;
--text-xl: 1.5rem;
--text-2xl: 2rem;
--leading-heading: 1.2;
}
h1, h2, h3 {
line-height: var(--leading-heading);
font-weight: 700;
}
h1 { font-size: var(--text-2xl); margin-block: 0 1rem; }
h2 { font-size: var(--text-xl); margin-block: 2rem 0.5rem; }
h3 { font-size: var(--text-lg); margin-block: 1.5rem 0.5rem; }Three details do most of the work here.
- Line height. Headings are short and large, so a tight line height such as 1.2 keeps wrapped lines together. The body text keeps its looser 1.5.
- Spacing. A larger margin above a heading than below it ties the heading to the content it introduces. The reader sees which paragraph belongs to which heading.
- Weight and size together. Each step down in level should lose some size or weight, so the order is clear at a glance.
When a heading must look different from its level
Sometimes design calls for an <h2> that looks like an <h3>. Keep the correct element and add a class that overrides the style.
<h2 class="heading-minor">Packing checklist</h2>.heading-minor {
font-size: var(--text-lg);
}Use this sparingly. If many headings need overrides, the type scale probably does not fit the design, and fixing the scale is better than adding classes.
Checking your headings
You can check a page quickly with three steps.
- Read only the headings, in order. They should read like a table of contents.
- Look for skipped levels and for more than one
<h1>. - Turn off your CSS, or use your browser's accessibility tree view. The structure should still make sense without styling.
Exercise
A designer wants a small, light-gray line of text above the page title that says "Hiking guides". A developer marks it up as <h4> so it renders small. Name two problems with this and give a better approach.
Show answer
First, the <h4> appears in the outline before the <h1> and skips levels, which gives screen reader users a confusing map. Second, the text is a label or tagline, not a section title, so it should not be a heading at all. Use <p class="eyebrow">Hiking guides</p> and style it with a small font-size and a gray color that still meets contrast requirements.
Exercise
Given the heading order h1, h3, h2, h4, which headings break the rules from this chapter, and how would you correct the order?
Show answer
The h3 directly after the h1 skips a level. The h4 directly after the h2 also skips a level. The corrected sequence is h1, h2, h3, h2, h3, or any sequence that descends one level at a time. If the original text sizes are what you wanted, keep them by overriding the styles in CSS.