My account

Accessibility (a11y)

Accessibility means everyone can buy from the shop, with a screen reader, with only a keyboard, or with low vision. Semantic HTML first, then ARIA only when needed. Every control has a name, focus is always visible, and messages reach the screen reader too. Test with the keyboard, a screen reader and an automated tool, because the automated tool finds only part of the problems.

Not reviewedWritten with AI helpReading time: 13 minOnline shop exampleHTML codeWCAG

Author: bezzad

Contents

The problem: a customer who cannot buy

A blind customer comes to our shop with a screen reader. This happens:

  1. The cart button is only an icon. The screen reader only says “button”. The user does not know what this button does.
  2. After adding a product, a green “Added to cart” message appears. The screen reader says nothing.
  3. The login window opens, but focus stays behind the window. The user tabs around in the page behind it.
  4. A form error is shown only with a red border. The user cannot tell which field is wrong.

All of this “works” with a mouse and good eyes. That is why the team does not see it.

Who needs it?

  1. Blind users use a screen reader. The screen reader reads the page aloud or shows it on a braille display.
  2. Users without a mouse, or who cannot move their hand precisely, use only a keyboard or similar tools.
  3. Users with low vision zoom the page and need good colour contrast.
  4. Deaf users need captions for videos.
  5. All of us, sometimes: with a broken arm, in strong sunlight, or holding a phone in one hand.
It is also the law. In the European Union, the European Accessibility Act applies to many online shops since June 2025. The common technical standard is WCAG, and most teams aim for its AA level.

Semantic HTML first

  1. A screen reader does not see the page. It reads the accessibility tree that the browser builds from the HTML.
  2. Each node in this tree has a role, a name and a state.
  3. The right tags fill in these three for free. A div with a click has none of them.
HTMLAccessibility treeThe screen reader says<button>Add to cart</button>role: buttonname: "Add to cart"focusable: yes"Add to cart, button"<div onclick>Add to cart</div>role: generictext: "Add to cart"focusable: no"Add to cart"Only text, no roleTab never reaches it
The browser builds an accessibility tree from the HTML. The screen reader reads the role and the name from this tree. A div has only text.

The first rule of ARIA

ARIA attributes (like role and aria-label) change only what the screen reader hears. They add no behaviour: no focus, no keys.

  1. The first rule: if an HTML tag already has this role and behaviour, use it, not ARIA.
  2. That is why the W3C ARIA guide says: “No ARIA is better than bad ARIA.”
  3. Bad ARIA lies. The screen reader announces something that does not work.
<!-- Bad: says "button", but Tab never reaches it -->
<div role="button">Add to cart</div>

<!-- Bad: hidden from screen readers, but still focusable -->
<button aria-hidden="true">Buy now</button>

<!-- Good: no ARIA needed -->
<button type="button">Add to cart</button>

Accessible name

Every button, link and field must have a name. The browser finds the name in this order:

  1. The aria-labelledby attribute, which points to the id of another text on the page.
  2. The aria-label attribute, which writes the name text directly.
  3. The HTML itself: the label tag for a field, the alt attribute for an image, and the text inside a button or link.

The cart button has only an icon, so it has no name:

<!-- Bad: no useful name -->
<button type="button" class="cart-btn">
  <img src="/icons/cart.svg">
</button>

<!-- Good: the button has a name, the icon is hidden -->
<button type="button" class="cart-btn" aria-label="Cart, 3 items">
  <img src="/icons/cart.svg" alt="">
</button>
Keep the name the same as the visible text. If a button has visible text, do not write a different aria-label. A user who controls the computer by voice says the text they see on the screen.

Keyboard focus

  1. A keyboard user goes forward with Tab and back with Shift and Tab. They activate with Enter and Space, and close with Esc.
  2. Focus must always be visible. Never remove the outline without a replacement.
  3. The focus-visible pseudo-class shows the focus ring only when the browser thinks it is needed, for example with the keyboard. With a mouse click on a button, it usually does not show.
/* Never do this without a replacement */
/* button:focus { outline: none; } */

.btn:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

Tab order

  1. The tab order is the order of the elements in the HTML, not the order you see on the screen.
  2. If you change the order with CSS (like the order property in Flexbox), focus jumps around the screen. Fix the HTML order instead.
  3. A tabindex of 0 adds a custom element to the tab order.
  4. A tabindex of -1 makes an element focusable only from code, not with Tab.
  5. A positive tabindex breaks the natural order. Do not use it.

A “skip to content” link at the start of the page helps a keyboard user skip a long menu:

<a class="skip-link" href="#main">Skip to content</a>
<!-- header and long menu -->
<main id="main">...</main>

Let us build the shop’s login window with the dialog tag:

<button type="button" id="open-login">Log in</button>

<dialog id="login-dialog" aria-labelledby="login-title">
  <h2 id="login-title">Log in</h2>
  <!-- login fields here -->
  <form method="dialog">
    <button>Close</button>
  </form>
</dialog>

<script>
  const dialog = document.getElementById('login-dialog');
  document.getElementById('open-login').addEventListener('click', () => {
    dialog.showModal();
  });
</script>

The showModal method does the hard work that we used to write with a lot of code:

  1. Focus moves into the window.
  2. The rest of the page becomes inert. The user cannot reach it with Tab or a click, and the screen reader does not read it.
  3. The Esc key closes the window.
  4. A form with method set to dialog closes the window with the Close button.
  5. After closing, focus goes back to the button that opened the window. The user does not lose their place.
  6. The window is placed in the top layer, above everything else. It needs no z-index.
Log ininertdialogEmailCloseEscLog in✓1. Focus on Log in2. Focus inside the windowThe page behind is inert3. The user closes it4. Focus comes backto the same Log in button
When the window opens, focus moves into it. After closing, focus goes back to the button that opened it.

Live regions

The “Added to cart” message appears on the screen, but focus does not move. The screen reader reads it only if it is inside a live region.

<div id="cart-status" role="status"></div>

<script>
  function showAdded(productName) {
    const status = document.getElementById('cart-status');
    status.textContent = `${productName} was added to the cart`;
  }
</script>
  1. The status role is “polite”: the screen reader waits until it finishes what it is saying, then reads the message.
  2. The alert role is urgent and interrupts what it is saying. Use it only for important errors.
  3. The message container must be on the page from the start. If you create the container together with the message, some screen readers do not read it.

Colour contrast

WCAG at level AA sets a minimum contrast ratio between text and background:

  1. Normal text: at least 4.5 to 1.
  2. Large text (about 24 pixels, or about 19 pixels bold): at least 3 to 1.
  3. Interface parts like a field border and the focus ring: at least 3 to 1.
  4. Colour alone is not enough. A user who cannot tell colours apart does not see a red border. Add text or an icon next to the colour.
1:13:14.5:17:121:1No differenceLarge text, interface partsAANormal textAANormal textAAABlack on whiteColour alone is not enough: also say the error in text.
The contrast ratio goes from 1 to 1 (no difference) to 21 to 1 (black on white). The AA and AAA minimums are marked on the line.

Form errors

<label for="email">Email</label>
<input id="email" name="email" type="email" required
       aria-invalid="true" aria-describedby="email-error">
<p id="email-error">Enter an email like name@example.com</p>
  1. The error message is text, not only colour. Say what is wrong and how to fix it.
  2. The aria-describedby attribute connects the message to the field. The screen reader reads the message after the field name.
  3. The aria-invalid attribute with the value true says the field value is wrong. Remove it when the error is fixed.
  4. After a failed submit, move focus to the first wrong field, or to an error summary at the top of the form.

How to test

  1. Keyboard only: put the mouse away and do a full purchase. Can you reach everything? Is focus always visible? Does Esc close the window?
  2. With a screen reader: NVDA on Windows is free. VoiceOver on macOS and iOS and TalkBack on Android come built in. Move around the page by headings and landmarks.
  3. With zoom: zoom the page to 200 percent. Text must not be cut off.
  4. With an automated tool: axe and Lighthouse find things like low contrast, fields without a label and images without alt. You can also run axe inside Playwright tests.
  5. But an automated tool finds only part of the problems. It cannot tell if the alt text makes sense or if the focus order is logical. Manual testing is needed.

Common mistakes

Mistake Result Better way
An icon button with no name The screen reader says only “button”. An aria-label on the button and an empty alt on the icon.
The button role on a div It is announced, but Tab and Enter do not work. The button tag.
Removing the outline A keyboard user does not know where they are. A clear focus-visible style.
A positive tabindex The tab order is broken. The right HTML order.
A modal made of a div Focus stays behind the window. The dialog tag with the showModal method.
A success message that is only visual The screen reader says nothing. A container with the status role.
Creating the live region with the message The message is not read. Put the container on the page from the start.
Light grey text Users with low vision cannot read it. A ratio of 4.5 to 1 for normal text.
An error shown only in red The user cannot tell what is wrong. Error text and aria-describedby.
Only automated tests Many problems are not found. Test with the keyboard and a screen reader.

Summary in seven lines

  1. Accessibility is for screen reader, keyboard and low vision users, and sometimes for all of us.
  2. Semantic HTML first. ARIA only changes what is heard, and bad ARIA is worse than none.
  3. Every control has a name: from its text, a label, alt or aria-label.
  4. Focus is always visible, and the tab order is the HTML order.
  5. Build modal windows with the dialog tag and the showModal method, so focus moves in and comes back.
  6. Put messages in a live region. Normal text needs a contrast of 4.5 to 1, and large text 3 to 1.
  7. Test with the keyboard, a screen reader and an automated tool. The automated tool alone is not enough.

Done reading this lesson?Mark it as finished so it shows in your lessons list and report card.