Sub topic 16
Checking your markup with devtools and validators
This chapter assumes you know the landmark, heading, form and interactive elements from earlier chapters. It also assumes you can open your browser's developer tools. You will use three kinds of checks: a validator, the accessibility tools in devtools, and a keyboard test.
What each check catches
No single tool finds every semantic problem. Each one sees a different layer.
| Check | Finds | Misses |
|---|---|---|
| HTML validator | Invalid nesting, duplicate ids, bad attributes, missing required attributes such as `alt` | Wrong but legal element choices, such as a `div` used as a button |
| Accessibility pane in devtools | The role and accessible name the browser computes for each element | Whether the content makes sense in context |
| Automated audit (Lighthouse, axe) | Missing labels, low contrast, empty links, skipped heading levels in some rules | Most judgment calls |
| Keyboard and screen reader test | Focus order, missing focus styles, confusing announcements | Nothing in its area, but it is slow |
Step 1: Validate the markup
Paste your HTML into the W3C Nu Html Checker at validator.w3.org/nu, or give it a public URL. It reports errors and warnings with line numbers. Fix errors first. Many semantic faults show up here, for example a div inside a p, a ul containing something other than li, or two elements sharing one id.
A valid page can still be badly structured. The validator checks syntax against the spec, not whether you chose the best element.
Step 2: Inspect the accessibility tree
In Chrome, open the Elements panel and select the Accessibility tab. In Firefox, open the Accessibility panel. Select an element and read its computed role, name and state.
Check these things:
- Landmarks (
banner,navigation,main,complementary,contentinfo) appear once each, apart from repeated landmarks you have labeled. - Each
inputhas a name that matches its visible label. - Buttons have the role
buttonand links have the rolelink. - A control with no name, or a name of just "click here", needs a fix.
Step 3: Review the heading outline
Run this snippet in the console to list the headings in document order:
document.querySelectorAll("h1, h2, h3, h4, h5, h6").forEach((heading) => {
const level = Number(heading.tagName[1]);
console.log(" ".repeat(level - 1) + heading.tagName + ": " + heading.textContent.trim());
});The indented output is your page's outline. Look for a missing h1, more than one h1 without a reason, and jumps such as h2 straight to h4.
Step 4: Run an audit and a keyboard test
Open the Lighthouse panel, choose Accessibility, and run it. Treat the score as a prompt for investigation, not a pass mark. Then put the mouse aside. Press Tab through the page and confirm that:
- Every interactive element receives focus in a logical order.
- The focused element has a visible outline.
- Enter activates links and buttons, and Space activates buttons.
A div with a click handler fails this test immediately, even though a validator accepts it.
<!-- The validator accepts this, but Tab skips it -->
<div class="btn" onclick="save()">Save</div>
<!-- Fix -->
<button type="button" onclick="save()">Save</button>Common errors when checking
- Fixing validator warnings by adding ARIA instead of changing the element. Replace the element first.
- Validating only the source file. Pages built by scripts need validation of the rendered DOM, which you can copy from the Elements panel.
- Ignoring accessible names. A role with no name is often as unusable as a missing element.
Exercise
A validator reports no errors for a page, but pressing Tab never reaches the "Buy" control, which is a span with a click handler. Which check found the problem, and what is the fix?
Show answer
The keyboard test found it. The validator only checks syntax, and a span with onclick is legal HTML. Replace the span with <button type="button">Buy</button>, which is focusable, works with Enter and Space, and has the role button in the accessibility tree.