Sub topic 14
Check accessibility: alt text, contrast, focus and landmarks
This chapter assumes you have a page with the HTML skeleton, semantic elements and a linked stylesheet from earlier chapters. You should also know how to write a CSS rule. Accessibility means that people with different abilities and tools can use your page. That includes screen reader users, keyboard-only users and people with low vision. Most basic checks take a few minutes.
Alt text
The alt attribute gives an image a text equivalent. A screen reader reads it aloud, and the browser shows it if the image fails to load.
<img src="photo-800.jpg" width="800" height="800"
alt="Portrait of Jane Doe at her desk">Follow these rules:
- Describe what the image shows and why it is there. Do not start with "image of".
- If the image is purely decoration, use an empty attribute:
alt="". Screen readers skip it. - Never leave
altout. Without it, some screen readers read the file name instead. - If an image is the only content of a link, the alt text should describe the link's destination.
Color contrast
Contrast is the difference in brightness between text and its background. The WCAG guidelines (the Web Content Accessibility Guidelines) set the minimum ratios for level AA:
| Text | Minimum ratio |
|---|---|
| Normal text | 4.5:1 |
| Large text (about 24px, or 19px bold) | 3:1 |
The values in the earlier stylesheet pass easily. Dark #1f2933 on white is far above 4.5:1. Check any color pair you add, especially light gray text and accent colors. Browser developer tools show the ratio when you inspect a text element's color. Also, do not use color alone to carry meaning. A link that differs from the text around it only by color is hard to spot for some users, so keep the underline.
Keyboard focus
Many people navigate with the Tab key. The element that currently receives keyboard input has focus, and the browser draws an outline around it. Removing that outline makes the page unusable for them.
a:focus-visible {
outline: 3px solid currentColor;
outline-offset: 2px;
}The :focus-visible selector shows the outline for keyboard users and usually hides it for mouse clicks. Avoid outline: none unless you replace it with another clear style.
Test this yourself. Reload the page, press Tab repeatedly, and confirm that:
- Each link receives a visible outline.
- The focus order follows the visual order.
- You can activate every link with Enter.
Links and buttons are focusable by default. A div with a click handler is not, so use the right element.
Landmarks
Landmarks are page regions that assistive technology can list and jump between. The semantic elements from earlier chapters create them.
| Element | Landmark role |
|---|---|
| `header` (direct child of `body`) | banner |
| `nav` | navigation |
| `main` | main |
| `footer` (direct child of `body`) | contentinfo |
Use one main per page. If you have several nav elements, give each a distinct label, as in <nav aria-label="Main">. Keep headings in order, with one h1 and then h2 sections, because screen reader users often navigate by heading.
Common errors
- Vague alt text such as
alt="photo". - Light gray text on white that fails 4.5:1.
- Removing focus outlines in a reset stylesheet.
- Using headings for size rather than structure.
- Missing
langon thehtmlelement, which affects how screen readers pronounce text.
Exercise
A decorative divider image sits between two sections and adds no information. What should its alt attribute be, and why?
Show answer
Use an empty value, alt="". A screen reader then skips the image instead of reading out a useless description or the file name. Omitting the attribute entirely is wrong, because some screen readers would announce the file name.
Exercise
Your stylesheet contains a { outline: none; }. What problem does it cause, and how would you fix it?
Show answer
Keyboard users can no longer see which link has focus. Remove the rule, or replace it with a visible style such as a:focus-visible { outline: 3px solid currentColor; outline-offset: 2px; }.