Lesson 7 of 12 · 12 min · Accessibility

Accessibility: keyboard, colour contrast and screen readers

Covers: WCAG 2.2 and current screen readers (NVDA, VoiceOver, TalkBack). Updated 13 Nov 2026. Part of the Frontend Developer roadmap.

What you will learn

  • Make the whole page usable with a keyboard
  • Provide visible focus, a logical focus order and skip links
  • Use tabindex correctly and manage focus in dynamic interfaces
  • Meet colour contrast ratios and avoid colour-only meaning
  • Test with a screen reader and a quick accessibility checklist

In simple words

Many people never touch a mouse: people with motor disabilities, people who use voice control or switches, and plenty of fast keyboard users. They move with the Tab key and press Enter. Others cannot easily read pale grey text on white. And blind users hear the page through a screen reader. This lesson covers three checks that catch most real problems: can I use it by keyboard, can I read it, and does it make sense when spoken?

Words to know

  • Focus: the element that currently receives keyboard input.
  • Tab order: the order in which elements receive focus as you press Tab.
  • Contrast ratio: a measure of how different the text colour is from its background, from 1:1 to 21:1.
  • Skip link: a hidden-until-focused link that jumps past repeated navigation to the main content.

Keyboard navigation

Everything you can do with a mouse must be possible with a keyboard (WCAG 2.1.1). The basic keys: Tab and Shift+Tab move between interactive elements, Enter activates links and buttons, Space activates buttons and checkboxes, arrow keys move within groups (radio buttons, tabs, menus, selects), Escape closes dialogs and menus.

Native elements (a[href], button, input, select, textarea, summary, dialog) are focusable and operable by default. This is the best reason to use them. Elements that are not interactive (div, span) are not in the tab order and ignore Enter.

Try it now on any site: put the mouse aside and press Tab repeatedly. Questions to ask:

  • Can I reach every link, button and field?
  • Can I see where I am?
  • Does the order make sense (it should follow the visual and reading order)?
  • Can I operate dropdowns, menus, carousels, dialogs and date pickers?
  • Am I ever trapped (cannot tab out)?

Visible focus

A keyboard user needs to see where focus is. Never remove the focus outline without replacing it. outline: none with nothing in its place is one of the most common accessibility failures.

:focus-visible {
  outline: 3px solid #1e3093;
  outline-offset: 2px;
}

:focus-visible shows the ring for keyboard focus (and not for mouse clicks on buttons), the best of both. WCAG 2.2 strengthens this: the focus indicator must be clearly visible with enough contrast against its surroundings, and focused elements must not be hidden behind sticky headers or cookie banners (use scroll-margin-top or scroll-padding-top to keep focused items visible).

Logical order

Focus follows the DOM order, so write the HTML in a sensible reading order. Pitfalls:

  • Using CSS (order, flex-direction: row-reverse, absolute positioning, grid placement) to rearrange things visually but not in the DOM makes the keyboard order jump around.
  • Avoid positive tabindex values (tabindex="5"): they override the natural order and create chaos.

The tabindex rules:

Value Meaning
tabindex="0" put a non-interactive element in the tab order (only for custom widgets that really are interactive)
tabindex="-1" focusable by script (element.focus()) but not in the tab order, for moving focus to a heading, a dialog or an error summary
positive numbers do not use

Managing focus in dynamic interfaces

When the page changes without a reload, you must move focus intentionally:

  • Opening a dialog: move focus into it (the native <dialog> does this); trap Tab inside while it is open; on close, return focus to the button that opened it.
  • Single-page app route change: move focus to the new page's <h1> (with tabindex="-1") or announce the change, since the browser does not.
  • Form errors: focus the first invalid field or an error summary at the top.
  • Removing the focused element (deleting a list item): move focus to a sensible neighbour, or it is lost to the top of the page.
  • Infinite scroll and carousels: provide pause controls, and do not move focus unexpectedly.

WCAG 2.2 also requires that a keyboard trap never exists and that drag-and-drop has a non-dragging alternative (buttons to move an item).

Colour contrast

Text must be readable against its background. WCAG measures the contrast ratio (from 1:1, identical, to 21:1, black on white):

Content Minimum (level AA) Level AAA
Normal text 4.5:1 7:1
Large text (18pt or 14pt bold, about 24px or 19px bold) 3:1 4.5:1
UI components (borders of inputs, icons that carry meaning) and graphical objects (chart parts) 3:1
Focus indicators 3:1 against adjacent colours
Logos, disabled controls and decorative text exempt

Examples: black text on white is 21:1; #767676 on white is exactly 4.5:1 (the lightest grey that passes for body text); #999 on white is only about 2.8:1 and fails. Placeholder text, light-grey captions and text on photographs are the usual culprits. Check with the browser DevTools colour picker (it shows the ratio), the WebAIM Contrast Checker, or Lighthouse and axe. Test your dark mode too, and text over images or gradients at the worst point.

Do not rely on colour alone

About 1 in 12 men has some colour vision deficiency, and colour is invisible on a monochrome printout or in bright sunlight. If colour carries meaning, add a second signal:

  • Errors: red border plus an icon plus text ("Email is required").
  • Links in body text: underline them (colour contrast of 3:1 against surrounding text is the alternative).
  • Charts: different patterns, shapes or direct labels, not just hues.
  • Status chips: "Paid", "Pending", "Failed" as words, not only green, amber and red.
  • Form requirements and required fields: text, not just colour.

Also support zoom and text size: content must work at 200 percent zoom (and reflow at 400 percent zoom, equivalent to a 320px-wide viewport) without horizontal scrolling; do not disable zoom; use relative units. Respect prefers-reduced-motion and avoid anything that flashes more than three times a second.

Screen readers

A screen reader turns the accessibility tree into speech or braille. The common ones:

Screen reader Platform Notes
NVDA Windows free, popular, good for testing
JAWS Windows paid, widely used in workplaces
VoiceOver macOS, iOS built in (Cmd+F5 on Mac)
TalkBack Android built in; vital because most Indian users are on Android
Narrator Windows built in

What users do: jump by headings (the most common way to scan a page), landmarks, links and form controls lists; read line by line; use "forms mode" for inputs. So:

  • Provide a good heading structure and landmarks.
  • Give links and buttons meaningful names ("Download the brochure (PDF, 2 MB)", not "Click here").
  • Give images alt text (empty for decoration), and captions or transcripts for audio and video.
  • Label every form field; announce errors and status changes with live regions.
  • Set the page lang (and lang on passages in another language: <span lang="hi">नमस्ते</span>) so the voice pronounces them correctly.
  • Make tables with <th scope> and <caption>.
  • Do not hide meaning in CSS content (::before text) or only in images of text.

A 10-minute screen reader test

  1. Turn on NVDA, VoiceOver or TalkBack.
  2. Listen to the page title and the first few lines. Does the page announce what it is?
  3. Open the headings list (NVDA: Insert+F7) and check it reads like a table of contents.
  4. Tab through the form: is every field announced with its label, type, required state and instructions?
  5. Trigger an error and a dynamic update: are they announced?
  6. Open and close a dialog or menu: where does focus go?
  7. Try the main task (sign up, search, buy) with the screen off.

A quick accessibility checklist

  • Every interactive element works with the keyboard; focus is visible and logical; no traps.
  • A skip link and landmarks exist; headings are in order.
  • Text contrast is at least 4.5:1 (3:1 for large text and UI parts); colour is never the only signal.
  • Images have appropriate alt; media has captions or transcripts.
  • Form fields have labels, helpful errors and autocomplete.
  • The page works at 200 percent zoom and with larger text; touch targets are large enough (WCAG 2.2: at least 24 by 24 CSS pixels, bigger is better).
  • Motion respects prefers-reduced-motion; nothing flashes.
  • The page has a lang, a unique <title> and passes Lighthouse and axe, and you tried it with a keyboard and a screen reader.

Common mistakes

  • outline: none with no replacement.
  • Positive tabindex values.
  • Reordering visually with CSS so the focus order jumps.
  • Pale grey text and placeholder text that fails contrast.
  • Colour-only errors or status.
  • Dialogs that do not trap or return focus.
  • Custom dropdowns and date pickers that are keyboard-inaccessible.
  • Disabling zoom with the viewport tag.

Try it yourself

  1. Unplug the mouse and complete the main task on a site you built, noting every problem.
  2. Add a visible :focus-visible style and a skip link to your page.
  3. Check your colour palette with a contrast checker and fix any pair under 4.5:1.
  4. Add icons and text to every colour-only status in your design.
  5. Run NVDA or TalkBack on your page for ten minutes using the test above, and write down five findings.

Check yourself

Why must you not remove the focus outline?

Keyboard users rely on it to see which element is active; without it they cannot tell where they are. If you restyle it, make it clearly visible.

What is the minimum contrast ratio for normal text at WCAG level AA?

4.5:1 (3:1 for large text and for UI components and graphics).

What does tabindex="-1" do?

It makes an element focusable by script but removes it from the Tab order, useful for moving focus to a heading or dialog.

Why is colour alone not enough to show an error?

Some users cannot distinguish the colours or see the screen; add text, an icon or another non-colour cue.

Interview questions

How do you make a custom component keyboard accessible?

Prefer a native element; otherwise make it focusable (tabindex="0"), handle Enter, Space and arrow keys as the ARIA pattern specifies, keep a visible focus style, manage focus (open, close, return), and provide roles, names and states.

How do you test a page for accessibility?

Run automated checks (Lighthouse, axe), navigate by keyboard only, test with a screen reader such as NVDA or TalkBack, zoom to 200 percent, check colour contrast, and ideally test with users who have disabilities.

What is a skip link and why is it needed?

A link at the top of the page that jumps to the main content, so keyboard and screen reader users can bypass repeated navigation without tabbing through every link.

Summary

  • Everything must work by keyboard: use native elements, keep a logical order and visible focus, add a skip link, manage focus in dynamic UI, avoid positive tabindex.
  • Meet contrast ratios (4.5:1 text, 3:1 large text and UI), never rely on colour alone, and support zoom and larger text.
  • Screen reader users navigate by headings, landmarks and labels; test with NVDA, VoiceOver or TalkBack.
  • Combine automated tools with manual checks against WCAG 2.2 AA.

Going deeper

See how much of this you can explain

A free mock interview asks you new questions on HTML, CSS and how the web works for Frontend Developer and scores every answer, with what to fix.