Web Accessibility (a11y)

Build sites that people using assistive technology, like screen readers or keyboard-only navigation, can use.

What is it?

People browse in different ways. Some can't see a screen and use software that reads the page aloud. Some can't use a mouse and navigate only with a keyboard. Some have low vision and need large, high-contrast text. A site that needs clear sight and precise mouse clicks fails a lot of people.

Accessibility, often written a11y (11 letters between the "a" and the "y"), means building sites that work with assistive technology, not just a mouse and monitor. It isn't a feature bolted on later. Mostly it's doing the basics right:

  • use real, semantic HTML elements instead of generic ones styled to look the part
  • give every image a meaningful text description (alt text)
  • make every interactive element reachable and usable with only a keyboard

A big part is focus management. As a keyboard user tabs through a page, the focused element should be clearly visible and move in a sensible order. Opening a modal dialog should move focus into it and trap it there, so the user isn't tabbing through content hidden behind it.

Explain like I'm 10

It's like adding ramps and clear signs next to the stairs. Most people barely notice them, but for someone in a wheelchair or who can't read small print, they decide whether they can get in the door at all.

Examples

Meaningful alt text

<!-- Bad: unhelpful or missing -->
<img src="chart.png" alt="image">

<!-- Good: describes the actual content -->
<img src="chart.png" alt="Bar chart showing sales rising 20% from January to March">

A screen reader reads the alt text in place of the image. 'image' tells a blind user nothing; a real description tells them what the chart shows.

Keyboard-reachable custom controls

<!-- Not focusable or announced as a button by default -->
<div onclick="submitForm()">Submit</div>

<!-- Reachable by keyboard, announced correctly by screen readers -->
<button onclick="submitForm()">Submit</button>

To keyboard navigation and screen readers, a div with a click handler is just generic text. A real button is focusable, works with the keyboard, and is announced as a button, all by default.

How it works

A screen reader doesn't see pixels. It reads the HTML structure (often through the DOM) and uses each element's role, name, and state to describe the page aloud or in braille.

Semantic elements have these roles built in: a button announces itself as "button" and responds to the keyboard automatically. A generic element styled to look like a button has none of that unless you add it back by hand.

Why does it exist?

The web is meant for everyone, not only people who see a screen clearly and use a mouse precisely. In many places it's also a legal requirement for certain kinds of sites. And accessible practices (clear structure, enough contrast, keyboard support) make a site better for everyone.

When to use it

Build accessibility into every project from the start. Semantic HTML, real alt text, and keyboard testing cost little as you build and far more to retrofit later.

When not to use it

There's basically no valid reason to skip accessibility on a public site. The only trade-off is depth: a small personal project may not need a full audit, but the basics (semantic tags, alt text, keyboard support) still cost little and help everyone.

Common mistakes

  • Styling generic elements to look interactive instead of using real buttons or links, which breaks keyboard and screen-reader support.

  • Writing unhelpful alt text like 'image' or 'photo1.jpg' instead of describing what the image conveys.

  • Getting focus wrong in dialogs: not moving focus into a newly opened dialog, or not letting it out when the dialog closes.

Practice exercises

  1. Easy:

    Add meaningful alt text to three images on a sample page, describing what each one conveys.

  2. Medium:

    Navigate a page using only the Tab and Enter keys. Note every interactive element you couldn't reach or activate.

  3. Hard:

    Rebuild a custom dropdown made of styled divs with proper semantic elements and keyboard support. Check that it works with Tab, Enter, and Escape.

Interview questions

What does web accessibility mean?

Building sites so people using assistive technology, such as screen readers or keyboard-only navigation, can perceive, understand, and use them.

Why does alt text matter, and what makes it good?

A screen reader reads it in place of the image, so it's often the only way a non-sighted user learns what the image conveys. Good alt text describes the image's actual content or purpose; it isn't generic or missing.

Why is using a real <button> element better than a styled <div> for something clickable?

A real button is focusable, works with the keyboard, and is announced correctly by screen readers, all automatically. A div has none of that unless you rebuild it by hand.

What does ARIA stand for, and what's the 'first rule of ARIA'?

Accessible Rich Internet Applications: HTML attributes that describe roles, states, and properties to assistive technology. The first rule: if a native HTML element or attribute already gives you the semantics and behavior you need, use it instead of adding ARIA to a generic element. Native elements come with correct behavior; ARIA only adds semantics, not behavior.

What's the difference between `aria-label` and `aria-labelledby`?

aria-label sets the accessible name directly as a string. aria-labelledby points to the id of another element on the page and uses its text as the name, which is handy when the label is already visible and shouldn't be duplicated.

What does `aria-hidden="true"` do, and what's a common mistake with it?

It removes an element and its descendants from the accessibility tree, so screen readers skip it entirely. A common mistake is using it on something still visible and focusable: a screen reader user's focus can land on a control that's never announced, which is confusing and effectively broken.

What is a 'landmark region', and why does it matter for accessibility?

A semantic sectioning element like <nav>, <main>, <header>, or <aside> (or an equivalent ARIA landmark role) that marks a major region of the page. Screen reader users often jump between landmarks instead of reading top to bottom, so landmarks help them reach what they want quickly.

What's the difference between `tabindex="0"`, `tabindex="-1"`, and a positive `tabindex` value?

tabindex="0" puts the element in the natural tab order, matching its DOM position. tabindex="-1" makes it focusable from JavaScript (e.g. after opening a dialog) but skips it when tabbing. A positive value pushes it ahead of the normal order; that's discouraged because the custom order easily drifts out of sync with the layout and is hard to maintain.

A custom dropdown is built from a `<div>` with `tabindex="0"` but no keyboard event handlers. What happens when a keyboard user tabs to it and presses Enter?

Nothing. tabindex="0" makes the element reachable by Tab, not activatable. A native <button> responds to Enter/Space automatically; a div needs JavaScript to listen for those keys and run the action. Without that, it's a dead end for keyboard users.

Why is focus trapping necessary in a modal dialog?

Without it, Tab can move focus out of the dialog onto content underneath that should be unreachable while it's open. Keyboard users end up tabbing through hidden content with no clear way back. Trapping keeps Tab cycling through the dialog's own focusable elements until it closes.

What contrast ratio does WCAG's AA level require for normal text versus large text?

At least 4.5:1 for normal text and 3:1 for large text (about 18pt/24px regular, or 14pt/18.66px bold, and up). Larger text is easier to read against its background even at lower contrast.

Why might you use a 'visually-hidden' CSS technique instead of `display: none` for some content?

display: none (and visibility: hidden) hide content from the accessibility tree too, so screen readers skip it. A visually-hidden class (clipping the element to a 1px box off-screen, without display:none) hides it from sighted users but keeps it available to assistive technology. It's used for skip links or extra context meant only for screen readers.

What is a 'skip link', and what problem does it solve?

A link, usually the first focusable element on the page, that jumps straight to the main content past repeated navigation. Without it, keyboard and screen reader users tab through the same nav links on every page before reaching the content.

Why is removing the default focus outline with `outline: none` a common accessibility trap?

The outline shows keyboard users which element is active. Removing it without a visible replacement (like a custom box-shadow or border on :focus-visible) leaves them unable to tell where they are, even though everything still technically works.

What does a `<label>` element's `for` attribute do, and why does it matter beyond just sighted mouse users?

It links the label to a form control by id. Clicking the label text then focuses or activates the input, a bigger target for anyone with limited motor precision. And a screen reader reads the label with the input when it gets focus, instead of announcing an unlabeled field.

What does the `role` attribute do when you're stuck using a non-semantic element for some custom widget?

It tells assistive technology what kind of control the element is (e.g. role="button", role="tablist") so it's announced correctly. It only adds semantics, not the keyboard behavior or focusability of a real element; you still have to build those yourself.

What is the accessibility tree, and how does it relate to the DOM?

A separate tree, built from the DOM, that holds each element's accessible role, name, state, and value. It's what screen readers and other assistive technology actually use. Purely presentational elements, or ones hidden with aria-hidden, can be in the DOM but left out of it, and ARIA can change how a node appears there without changing the DOM.

A form shows an invalid input by giving it a red border and nothing else — what's the accessibility problem, and how would you fix it?

Color alone doesn't reach colorblind users, and screen readers can't perceive a border color at all. Add a text error message linked to the field with aria-describedby, and mark the field aria-invalid="true" so assistive technology announces the invalid state.

A screen reader announces a form field's error message twice — what's a likely cause?

The same error text is probably exposed through two mechanisms at once, for example in content referenced by aria-describedby and also in an aria-live region showing the same message. The user hears it from both.

What's the difference between `aria-live="polite"` and `aria-live="assertive"`?

polite waits until the screen reader finishes its current announcement, so it doesn't interrupt. assertive interrupts immediately. Save assertive for urgent updates; overusing it is disorienting.