Sub topic 11
Checking your hierarchy
This chapter assumes you have read the earlier chapters of this tutorial. You should know how to pick elements by role, build a type scale with custom properties, and set body text, captions, and labels. Here you will review a finished page and test whether the hierarchy holds up.
A hierarchy can look right and still fail. A heading may be styled well but be the wrong level. A label may look attached to its field but not be linked to it. Checking from several angles catches these problems before users do.
A review order
Start with what you can see, then check the structure underneath, then stress the page.
Step 1: The squint test
Blur your eyes or zoom the browser out until the words are unreadable. You should still see:
- one clear top-level title,
- section headings that stand out from body text,
- body text as an even, quiet block,
- captions and other secondary text visibly smaller or lighter than body text.
If two levels look the same, your type scale lacks contrast between them. Increase the size step or the weight difference. If everything is bold, nothing stands out.
Step 2: Check the heading outline
Visual order and document order can disagree. A browser's accessibility tools and screen readers use the heading outline, so check it directly. Run this in the browser console on your page:
const headings = [...document.querySelectorAll("h1, h2, h3, h4, h5, h6")];
let previous = 0;
for (const h of headings) {
const level = Number(h.tagName[1]);
const skipped = previous && level > previous + 1;
const indent = " ".repeat(level - 1);
console.log(`${indent}h${level}: ${h.textContent.trim()}${skipped ? " <-- skipped level" : ""}`);
previous = level;
}
const h1Count = document.querySelectorAll("h1").length;
if (h1Count !== 1) {
console.warn(`Expected one h1, found ${h1Count}`);
}The output is an indented outline. Read it as a table of contents. Does it make sense without the page around it? Look for these problems:
- A jump such as
h2toh4. Useh3, or change the level. - A heading chosen for its size rather than its role. Fix the level, then adjust the size in CSS.
- Bold paragraphs used as fake headings. Make them real headings.
Step 3: Check labels and captions
Every form control needs a label linked to it. This snippet lists controls that have none:
const controls = document.querySelectorAll("input, select, textarea");
controls.forEach((control) => {
if (["hidden", "submit", "button", "reset"].includes(control.type)) return;
const hasLabel = control.labels && control.labels.length > 0;
const hasAriaLabel = control.hasAttribute("aria-label");
if (!hasLabel && !hasAriaLabel) {
console.warn("No accessible name:", control);
}
});Then test by hand. Click each label. The matching field should receive focus. If it does not, the for value probably does not match the input's id.
For captions, confirm that each one sits inside a figure with its image, in a figcaption. A caption in a plain p below the image looks the same but has no tie to it in the markup.
Step 4: Stress the page
Two tests show whether your relative units and colors work.
- Zoom to 200%. Use the browser zoom. Text should reflow without horizontal scrolling and without overlapping.
- Raise the default font size. Set a larger default text size in browser settings. Text sized in
remshould grow. Text sized inpxstays fixed, which means it breaks this test.
Then check contrast with your browser's developer tools, which show the ratio for any selected text. WCAG AA asks for at least 4.5:1 for normal text and 3:1 for large text. Secondary text is where pages fail most often, because light gray captions are easy to make too pale.
Common errors summary
| Symptom | Likely cause | Fix |
|---|---|---|
| Outline jumps from `h2` to `h4` | Level chosen for size | Use `h3`, then restyle in CSS |
| More than one `h1` | Logo or section titles marked as `h1` | Keep one `h1` for the page title |
| Clicking a label does nothing | `for` and `id` do not match | Make the values identical |
| Text does not grow with browser settings | Sizes set in `px` | Use `rem` in your tokens |
| Captions hard to read | Gray too light | Darken until contrast reaches 4.5:1 |
| Headings and body look alike | Scale steps too small | Widen the size or weight gap |
Fixing what you find
Change the markup before the CSS. If a heading is the wrong level, edit the element. If a label is not linked, fix the id. Only then adjust the type tokens, so one change in :root updates every place that uses it. After each fix, run the same checks again. A fix for one problem, such as darkening caption color, can affect others.
Exercise
The console outline shows h1, h2, h4, h2. The h4 is a small subheading that the designer wanted at a small size. What is wrong, and how do you fix it?
Show answer
The h4 skips level 3 under an h2. Change the element to h3 so the outline is continuous, then set the small size on h3 in CSS, or with a dedicated class that uses a smaller size token. The element should follow the document structure, and the size should come from the stylesheet.
Exercise
You click a label and the input does not receive focus. Name two likely causes.
Show answer
The label's for attribute does not match the input's id, or the input has no id at all. Another cause is that the label and input are different elements with a duplicate id elsewhere on the page, so the browser links to the wrong one.