Sub topic 14
Check accessibility: alt text, contrast, focus and landmarks
This chapter covers the checks to run on your finished page before you publish it. It assumes you have the page skeleton, a linked stylesheet, custom properties and the responsive layout from earlier chapters. You should also be able to open your browser's developer tools. No assistive technology is required, but a keyboard is.
Accessibility means people with different abilities can use the page. Some visitors cannot see the screen, some cannot use a mouse, some have low vision or color blindness, and some read in bright sunlight on a phone. Most fixes are small and cost nothing.
What to check
Four areas cover most of the problems on a one-page site.
| Area | The question | Typical failure |
|---|---|---|
| Text alternatives | Does every image have the right `alt` text? | Missing `alt`, or file names used as alt text |
| Contrast | Can people read the text against its background? | Light gray text on white |
| Focus | Can a keyboard user see where they are? | `outline: none` with no replacement |
| Landmarks | Can a screen reader user jump between page regions? | A page built from `<div>` elements only |
The checks follow the Web Content Accessibility Guidelines (WCAG). This chapter targets level AA, the usual goal for personal and business sites.
Landmarks
A landmark is a page region that assistive technology can list and jump to. Screen reader users often move from landmark to landmark instead of reading the page top to bottom. Semantic elements create landmarks automatically.
| Element | Landmark role | Notes |
|---|---|---|
| `<header>` | banner | Only when it is a direct child of `<body>` |
| `<nav>` | navigation | Add `aria-label` when there is more than one |
| `<main>` | main | Use exactly one per page |
| `<footer>` | contentinfo | Only when it is a direct child of `<body>` |
| `<section>` | region | Only when it has an accessible name, such as `aria-labelledby` |
The skeleton from the first chapter already follows these rules. Check these points on your own page:
- There is one
<main>, and all page-specific content is inside it. - The
<nav>has a label such asaria-label="Main". If you add a second navigation, for example in the footer, give it a different label. - Each
<section>begins with a heading. A section with anaria-labelledbythat points to its heading becomes a named region.
<section id="projects" aria-labelledby="projects-title">
<h2 id="projects-title">Projects</h2>
</section>Headings are also a navigation tool. Screen reader users list headings to scan a page. Use one <h1>, then <h2> for sections, then <h3> for subsections. Do not skip levels to get a smaller font. Choose the heading level for the structure and set the size in CSS.
Add a skip link
Keyboard users must press Tab through every navigation link before they reach your content. A skip link is the first focusable element on the page and jumps straight to <main>. It stays off-screen until it receives focus.
<body>
<a class="skip-link" href="#main">Skip to main content</a>
<header>
<!-- navigation -->
</header>
<main id="main">
<!-- page content -->
</main>
</body>.skip-link {
position: absolute;
left: 1rem;
top: 0;
padding: 0.5rem 1rem;
background: var(--color-bg);
transform: translateY(-120%);
}
.skip-link:focus {
transform: translateY(0);
}I moved it with transform instead of display: none. An element with display: none cannot receive focus, so the skip link would never appear.
Alt text
The alt attribute gives an image a text equivalent. Screen readers read it aloud. Browsers show it when the image fails to load. Search engines also use it.
The right text depends on what the image does on the page.
| Image role | What to write | Example |
|---|---|---|
| Informative photo | Describe what matters in context | `alt="Portrait of Jane Doe at her desk"` |
| Image that is a link | Describe the destination, not the picture | `alt="Jane's GitHub profile"` |
| Decorative flourish | Leave the value empty | `alt=""` |
| Chart or screenshot | Say what it shows, and put details in nearby text | `alt="Bar chart: page load time fell from 4 s to 1 s"` |
Follow these rules:
- Always include the attribute. An image with no
altat all may cause some screen readers to read out the file name. - Use
alt=""for purely decorative images. A screen reader then skips the image. Do not use a space or the word "decoration". - Skip phrases like "image of" or "picture of". The screen reader already announces that it is an image.
- Keep it short, usually under about 125 characters. Put longer explanations in the page text.
- Do not repeat a caption. If the text next to the image already says what it shows, a short
altis enough.
The responsive image from the previous chapter already follows this pattern:
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w"
sizes="(min-width: 48rem) 400px, 100vw"
width="800"
height="800"
loading="lazy"
alt="Portrait of Jane Doe at her desk">Text inside an image is a related trap. If your logo or heading is a PNG with words in it, the alt must contain those same words. People who zoom in on the page also see the image get blurry, so use real text when you can.
Contrast
Contrast ratio compares the brightness of text with the brightness of its background. It runs from 1:1 (no difference) to 21:1 (black on white). WCAG AA sets these minimums:
| Content | Minimum ratio |
|---|---|
| Normal text | 4.5:1 |
| Large text (18pt or larger, or 14pt bold) | 3:1 |
| Interface parts such as input borders and focus outlines | 3:1 |
The colors in the earlier chapter pass. The text color #1f2933 on white is far above 4.5:1. The link color #0b5cad on white is about 6.7:1. Problems usually appear when you add a muted gray for secondary text:
/* Fails: about 2.8:1 on white */
.meta {
color: #999999;
}
/* Passes: about 4.5:1 on white */
.meta {
color: #767676;
}Because your colors live in custom properties, you can fix a failing color in one place. Add a --color-muted variable, test it once, and reuse it.
Check contrast in Chrome or Edge developer tools. Select an element, then click the color swatch in the Styles panel. The color picker shows the ratio and marks it pass or fail. Firefox has an accessibility inspector that reports the same thing.
Contrast is also affected by state. Check hover and focus colors as well as the default look. Never use color as the only signal. A link that differs from body text only by color is invisible to some color-blind readers. The default underline on links solves this, so do not remove it without a replacement.
Keyboard focus
Focus is the element that currently receives keyboard input. Press Tab to move forward and Shift+Tab to move back. Links, buttons and form controls are focusable by default. The focus indicator shows where you are, and without it a keyboard user is lost.
The most common mistake is removing it:
/* Do not do this */
a:focus {
outline: none;
}Use the rule from the in-page navigation chapter instead. :focus-visible shows the outline for keyboard navigation but not usually after a mouse click.
a:focus-visible {
outline: 3px solid currentColor;
outline-offset: 2px;
}Test focus by putting your mouse aside and tabbing through the whole page.
- The first Tab press should reveal the skip link.
- Focus should move through the navigation in the same order it appears on screen.
- Every link should show a visible outline.
- Pressing Enter on an anchor link should scroll to its section.
- Nothing should trap you, and focus should never vanish.
Tab order follows the order of elements in the HTML, not their position on screen. If you reorder items visually with Flexbox order or Grid placement, keyboard order and visual order can disagree. Fix this by changing the HTML order. Avoid positive tabindex values such as tabindex="3". They override the natural order and are hard to maintain.
Anchor links bring up one more case. If a section is hidden under a sticky header after the jump, the scroll-margin-top rule from the previous chapter keeps it visible. Smooth scrolling should respect users who ask for less motion, which is what the prefers-reduced-motion media query does.
Run an automated audit
Automated tools find about a third of accessibility problems. They are good at missing alt attributes, low contrast and missing labels. They cannot judge whether your alt text is meaningful.
- Lighthouse is built into Chrome developer tools. Open the Lighthouse tab, select Accessibility, and run it.
- axe DevTools is a browser extension that lists each issue with the element and a suggested fix.
- The W3C validator at validator.w3.org checks your HTML for errors such as duplicate IDs. Duplicate IDs break anchor links and label associations.
Then do three manual checks:
- Zoom the browser to 200% and confirm that nothing overlaps or disappears.
- Run the page with only the keyboard, as described above.
- Turn on a screen reader briefly. VoiceOver on macOS (Cmd+F5) and NVDA on Windows (free) both work. Listen to the landmark and heading lists.
Link wording is a quick manual fix. A link that says "click here" or "read more" means nothing in a list of links. Write "Email Jane" or "Read the Weather App case study" instead.
Exercise
Your footer has a gray note, color: #aaaaaa on a white background, and a decorative divider image. What two changes should you make?
Show answer
Darken the gray text to at least 4.5:1 contrast, for example #767676 or darker, because #aaaaaa is below the AA minimum for normal text. Give the divider image alt="" so screen readers skip it.
Exercise
You wrote a:focus { outline: none; } because the outline looked ugly after mouse clicks. What should you use instead, and why does it help?
Show answer
Use a:focus-visible with a visible outline, for example outline: 3px solid currentColor; outline-offset: 2px;. It shows the focus indicator when the user navigates by keyboard, so keyboard users keep a clear indicator, and the mouse-click styling you disliked mostly disappears.
Exercise
Your page has <header>, <nav>, <main> and <footer>, but a screen reader user still has to listen through the entire menu on every visit. Which addition fixes this?
Show answer
A skip link as the first focusable element, such as <a class="skip-link" href="#main">Skip to main content</a>, with id="main" on the <main> element. It lets keyboard and screen reader users jump past the navigation.