Sub topic 15
Accessibility basics and validating your HTML
This chapter assumes you can write a complete HTML document and know the basic elements covered earlier: head metadata, headings, links, images, semantic layout elements and tables. It pulls those pieces together into a short list of habits that make a page usable by more people, and it shows how to check your work with a validator and browser developer tools.
Why accessibility starts with markup
Accessibility means that people can use a page regardless of how they browse. Some use a screen reader, which reads the page aloud or sends it to a braille display. Some navigate only with the keyboard. Some zoom text to 400 percent, and some have a slow connection that stops images from loading.
Most of what these tools need comes from correct HTML. A screen reader does not look at your layout. It reads the document's structure: which text is a heading, which is a link, which is a list, and which cell belongs to which column. If you pick elements for what they mean, the browser builds an accessibility tree from them, and assistive technology reads that tree.
Declare the page language
The lang attribute on the html element states the language of the page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Garden Notes</title>
</head>
<body>
<p>Welcome to the garden.</p>
</body>
</html>Screen readers use it to pick the right pronunciation rules. Without it, a French page may be read with English rules. Browsers also use it for hyphenation, spell checking and translation offers.
If a passage is in another language, set lang on that element alone:
<p>The French phrase <span lang="fr">bon appétit</span> means "enjoy your meal".</p>Use a valid language tag such as en, fr, pt-BR or zh-Hans.
Give every page a useful title
The title element is the first thing a screen reader announces when a page loads. It is also the text in browser tabs, bookmarks and search results. Make each page title unique and descriptive, and put the specific part first.
- Weak:
Home,Untitled, or the same site name on every page. - Better:
Growing tomatoes - Garden Notes.
Keep a logical heading order
Screen reader users often skip through a page by jumping from heading to heading, much as sighted users scan. This only works if headings form an outline.
- Use one
h1that names the page. - Do not skip levels. An
h2is followed byh3, noth4. - Choose a heading level for its place in the outline, never for its size. Change the size with CSS.
- Do not use headings for text that is merely bold or large.
<h1>Garden Notes</h1>
<h2>Vegetables</h2>
<h3>Tomatoes</h3>
<h3>Beans</h3>
<h2>Flowers</h2>
<h3>Roses</h3>Going back up is fine. After h3 Roses you may start a new h2. The problem is jumping down by more than one level.
Write meaningful alt text
The alt attribute on img gives a text alternative. It is read aloud, shown if the image fails to load, and used by search engines. The right text depends on what the image does.
| Image role | What to write | Example |
|---|---|---|
| Informative | Describe the content that matters | `alt="A grey cat asleep on a sofa"` |
| Functional (inside a link or button) | Describe the action or destination | `alt="Garden Notes home"` |
| Decorative | Leave the value empty | `alt=""` |
| Complex (chart, diagram) | Short summary in `alt`, full detail in nearby text | `alt="Bar chart of study hours, described below"` |
An empty alt="" is valid and intentional. It tells assistive technology to skip the image. Leaving the attribute off entirely is different. The validator reports an error, and some screen readers then read out the file name.
<a href="/">
<img src="logo.svg" alt="Garden Notes home" width="120" height="40">
</a>
<img src="divider.png" alt="" width="600" height="8">Avoid starting with "image of" or "picture of". The screen reader already announces that it is an image.
Use semantic elements instead of generic ones
Landmarks are regions that assistive technology can list and jump to. The semantic layout elements create them for you.
| Element | Landmark role | Notes |
|---|---|---|
| `header` | banner (when a direct child of `body`) | Site-wide heading area |
| `nav` | navigation | Label it if there is more than one |
| `main` | main | Use exactly one visible `main` per page |
| `aside` | complementary | Related but secondary content |
| `footer` | contentinfo (when a direct child of `body`) | Site-wide footer |
| `section` | region (only if it has an accessible name) | Needs a heading or label |
When a page has two nav elements, give each a label so users can tell them apart:
<nav aria-label="Main">
<a href="/">Home</a>
<a href="/articles">Articles</a>
</nav>
<nav aria-label="Article pages">
<a href="?page=1">1</a>
<a href="?page=2">2</a>
</nav>The aria-label attribute comes from a separate specification called ARIA. A good rule is that native HTML comes first. A button element is focusable, responds to Enter and Space, and is announced as a button without extra work. A div with an onclick does none of that. Add ARIA only when no native element does the job.
Make links and buttons understandable
Screen readers can list every link on a page out of context. Link text such as "click here" or "read more" then means nothing.
<!-- Weak -->
<p>To learn about tomatoes, <a href="/tomatoes">click here</a>.</p>
<!-- Better -->
<p>Learn more in <a href="/tomatoes">our guide to growing tomatoes</a>.</p>Use a for navigation to a URL and button for actions on the current page. Both are keyboard-focusable by default. Do not remove the visible focus outline in CSS unless you replace it with something equally clear.
Table and form basics
Data tables need a caption and header cells with scope, so a screen reader can announce the column or row header for each cell. Do not use tables for page layout.
<table>
<caption>Weekly study hours</caption>
<thead>
<tr>
<th scope="col">Day</th>
<th scope="col">Hours</th>
</tr>
</thead>
<tbody>
<tr>
<td>Monday</td>
<td>2</td>
</tr>
</tbody>
</table>Every form control needs a visible label connected by matching for and id values. Clicking the label then focuses the input, which also enlarges the click target.
<label for="email">Email address</label>
<input id="email" name="email" type="email" required>A placeholder is not a label. It disappears when the user types, and it often has low contrast.
Other quick checks
- Contrast. Text should have enough contrast against its background. WCAG 2.2 level AA asks for at least 4.5:1 for normal text and 3:1 for large text.
- Keyboard. Put the mouse aside and press Tab. You should reach every link and control in a sensible order, and always see where focus is.
- Zoom. The
viewportmeta tag must not block zooming. Avoiduser-scalable=no. - Not color alone. Do not rely on color alone to carry meaning, such as red for errors with no text.
Validating your HTML
Validation checks your markup against the HTML rules. It catches mistakes that browsers quietly repair, which is why a broken page can still look fine on your machine and fail elsewhere.
The W3C Nu HTML Checker
The W3C Nu HTML Checker at validator.w3.org/nu/ accepts a public URL, an uploaded file, or pasted text. It reports each problem with a line number. Messages come in three kinds:
- Error. The markup breaks a rule, for example a missing
altor anlioutside a list. - Warning. Something is allowed but suspicious, for example a
sectionwithout a heading. - Info. A note that needs no action.
Common errors and their causes
| Message (paraphrased) | Likely cause | Fix |
|---|---|---|
| Element `title` must not be empty / missing | No `title` in `head` | Add a descriptive `title` |
| `img` element must have an `alt` attribute | `alt` omitted | Add text, or `alt=""` if decorative |
| Stray end tag `div` | An extra or misplaced `</div>` | Match each end tag to its start tag |
| Element `li` not allowed as child of `div` | List items without `ul` or `ol` | Wrap them in a list |
| Duplicate ID `email` | Two elements share one `id` | Make each `id` unique |
| Unclosed element `p` | Missing end tag | Close it, or check for block content nested inside |
| Bad value for attribute `href` | A space or illegal character in the URL | Encode it, for example `%20` |
Fix errors from the top of the list down. One early mistake, such as an unclosed element, often causes several later messages, and fixing it clears them all.
Browser developer tools
Validators check rules. Developer tools show what the browser actually built. Open them with F12, or Ctrl+Shift+I (Cmd+Option+I on macOS).
- Elements panel (Inspector). Shows the live DOM, not your source text. If the browser repaired your markup, such as by closing a
pearly, you see the repaired result here. Compare it with your file to find the mistake. - Accessibility pane. In Chrome, Edge and Firefox, select an element to see its computed name and role, such as
buttonorlink, and where it sits in the accessibility tree. - Console. Reports problems such as a failed image or script request.
- Network panel. Shows whether
cat.jpgreturned a 404, which explains why alt text appears instead of the picture. - Lighthouse or an accessibility audit. Runs automated checks for contrast, missing labels and missing
alttext, and links to the rule that failed.
A worked example
This page has four problems. Find them before reading on.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Garden Notes</h1>
<h3>Tomatoes</h3>
<img src="tomato.jpg">
<p>Tomatoes need sun. <a href="/tomatoes">Click here</a></p>
</body>
</html>The problems are:
- The
htmlelement has nolangattribute. - The
headhas notitle, which is an error in the validator. - The heading jumps from
h1toh3. - The
imghas noalt, and the link text "Click here" says nothing about its destination.
A corrected version:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Growing tomatoes - Garden Notes</title>
</head>
<body>
<h1>Garden Notes</h1>
<h2>Tomatoes</h2>
<img src="tomato.jpg" alt="A ripe red tomato on the vine" width="400" height="300">
<p>Tomatoes need sun. Read <a href="/tomatoes">our guide to growing tomatoes</a>.</p>
</body>
</html>Summary of habits
- Set
langonhtmland write a unique, descriptivetitle. - Keep headings in a logical order, with one
h1. - Write
alttext that matches the image's job, and usealt=""for decoration. - Use semantic elements and native controls before ARIA.
- Write link text that makes sense out of context.
- Validate, inspect the DOM, and test with the keyboard.
Exercise
A page's img elements for a decorative divider line have no alt attribute. The page looks fine in the browser, and a screen reader reads out "divider dot png" each time. What should you change, and why is the validator reporting an error?
Show answer
Add an empty alt attribute, alt="", to each decorative image. The HTML rules require every img to have an alt attribute, so the validator reports an error when it is missing. An empty value is valid and tells assistive technology to skip the image, whereas a missing attribute can cause the file name to be read out.
Exercise
A page has an h1, then an h4 for a subsection, then another h2. Which rule does this break, and how would you fix it? Name one tool that would help you spot problems like this.
Show answer
It skips heading levels, which breaks the outline that screen reader users navigate by. Change the h4 to an h3 if it sits under an h2 section (or add the missing h2 above it). If the heading looks too large or small, adjust its size with CSS rather than by changing the level. The W3C Nu HTML Checker, a browser accessibility audit such as Lighthouse, or a headings list in a screen reader can all reveal the problem.
Exercise
You validate a page and see eight errors. The first is "Unclosed element div". What should you do first, and why?
Show answer
Fix the first error and validate again. One unclosed element can make the parser misread everything after it, so several later errors are often side effects of the first. Fixing it may remove many of them at once.