Sub topic 15
Accessibility basics and validating your HTML
This chapter assumes you can write a full HTML document and know headings, images, tables and semantic layout elements from earlier chapters. Here you will apply a few habits that make pages usable for everyone, then check your work with tools.
Why accessibility starts in the markup
Screen readers, keyboard users and search engines read the structure of your HTML, not how it looks. Correct elements give them landmarks, labels and a reading order for free. Most basic accessibility work is choosing the right element and filling in the right attribute.
The lang attribute
The lang attribute on the html element names the page's language. Screen readers use it to choose pronunciation rules, and browsers use it for hyphenation and translation prompts.
<html lang="en">If a passage is in another language, put lang on that element too, for example <p lang="fr">Bonjour</p>.
Alt text
Every img needs an alt attribute. Choose its value by the image's job:
- Informative image: describe what matters, as in
alt="A grey cat asleep on a sofa". - Image inside a link: describe the destination or action.
- Decorative image: use an empty
alt=""so screen readers skip it.
Do not start with "image of" and do not omit the attribute. A missing alt may cause a screen reader to read out the file name.
A logical heading order
Headings let screen reader users jump through a page. Use one h1 for the page title and nest the rest without skipping levels: an h2 is followed by h3, not h4. Pick the level by structure, not by the size you want. Size is a job for CSS.
Other habits that cost nothing
- Use landmarks such as
header,nav,mainandfooterinstead of genericdivelements. Use onemainper page. - Write link text that makes sense alone. "Read the accessibility guide" beats "click here".
- Give table headers
thwithscope, and add acaption. - Tie every form control to a
labelwith matchingforandidvalues.
<label for="email">Email address</label>
<input id="email" name="email" type="email" required>Validating your HTML
A validator checks your document against the HTML rules. Browsers silently repair many mistakes, so a page can look fine while its structure is broken, and the repairs can differ between browsers.
The W3C Nu HTML Checker at validator.w3.org/nu accepts a URL, an uploaded file or pasted text. It lists errors, which break the rules, and warnings, which are suspicious but allowed.
Common problems it finds:
| Message | Likely cause |
|---|---|
| Element `img` is missing required attribute `alt` | You left out `alt`. |
| Stray end tag `p` | A closing tag has no matching start tag. |
| Element `li` not allowed as child of element `div` | List items must sit directly in `ul` or `ol`. |
| Duplicate ID `email` | Two elements share one `id`, which breaks label links. |
Browser developer tools
Open developer tools with F12 or Ctrl+Shift+I (Cmd+Option+I on macOS). The Elements panel shows the DOM, which is the tree the browser built after repairing your markup. Compare it with your source. If an element sits somewhere you did not put it, a nesting error is the usual cause. The Accessibility pane, found in the same panel or tab, shows the computed name and role of the selected element, so you can confirm that a label or alt text is exposed.
Exercise
A page has <img src="logo.png"> inside a link to the home page, and the validator reports an error. What is the error, and how do you fix it?
Show answer
The alt attribute is missing. Because the image is the only content of the link, add alt text that names the link's purpose, for example <img src="logo.png" alt="Garden Notes home">.
Exercise
A page goes from an h1 straight to an h3. Why is this a problem, and what is the fix?
Show answer
Screen reader users navigate by heading level, and a skipped level suggests missing content. Change the h3 to an h2, and use CSS if you want a different size.