On a sign-up form, a wrong field only turns red. For most people that's enough. But a colour-blind user may not notice that anything is wrong, and then wonder why the form can't be sent.
Accessibility (often shortened to a11y) means everyone can use your page. That includes colour-blind users, blind users with a screen reader (a program that reads the screen aloud), and people who only use a keyboard. There are two ways to do it: use the right HTML tag, and give every image, button and input some text that can be read aloud.
Use the right tag
<button>Click Me</button>
<img src="photo.jpg" alt="User profile photo">
<nav aria-label="Main menu">
<a href="#home">Home</a>
<a href="#about">About</a>
</nav>
<button>is a real button. You can reach it with the Tab key, it works when you press Enter, and a screen reader reads it as "button". A<div>made to look like a button gets none of that.alt="User profile photo"is the image's replacement text. Screen readers read this text aloud. Write what the image shows, not the word "image" or its file name.<nav>marks a block of navigation, and each<a>inside it is a link you can reach with Tab.aria-label="Main menu"gives the<nav>a name, only for screen readers to read aloud.
A version made only of <div> tags can look exactly the same, but the version above works with a keyboard and screen readers read it correctly.
A name for icon buttons: aria-label
<button aria-label="Close menu">×</button>
- A button that only holds an icon has no clear name.
aria-labelgives it one, so a screen reader says "Close menu, button". - A screen reader reads the
aria-label, not the text you see on the button. So a button that already says "Save" doesn't needaria-label. - Attributes that start with
aria-, and theroleattribute, are called ARIA: additions only for screen readers. Use the right tag first. Use ARIA only when plain HTML isn't enough.
Labels, roles, and decoration
<nav role="navigation">...</nav>
<label for="name">Name:</label>
<input type="text" id="name">
<span aria-hidden="true">***</span>
roletells screen readers an element's role.<nav>already has the navigation role, sorole="navigation"only states it again. This example writes it only to showrole. On your own pages, do not repeat a role the tag already has: the ARIA rules of the W3C (the group that writes web standards) advise against it.roleis useful on an element that has no role yet, for example<div role="alert">for an important message that screen readers read out as soon as its text appears.<label for="name">connects to<input id="name">because theforvalue matches theid. A screen reader reads "Name" when that box is selected.aria-hidden="true"hides decoration from screen readers, such as a row of stars***that is only there to look nice. Don't put it on a button or link: keyboard users can still reach it with Tab, but a screen reader reads nothing for it.
Don't rely on colour alone
Colour-blind users may not notice a wrong field if the only sign is the colour red. Add a text message or an icon, such as "Email is empty". The contrast (the difference between light and dark) between text and its background should also be at least 4.5:1 (a contrast checker tool measures this number), so the text stays readable.
⚠️ What makes screen readers misread and keyboard users get lost
- Removing
altfrom a decorative image. Withoutalt, a screen reader may read the file name. A decorative image only needsalt="", so screen readers skip it. - A label that just sits next to the input. Without a
forthat matches theid, they aren't connected. - Removing the focus outline. The CSS rule
outline: noneremoves the line around the selected element. Keyboard users can no longer see where they are. If the default outline does not suit your design, change how it looks instead of removing it. For example,:focus-visible { outline: 3px solid orange; }gives a thick orange line when an element is selected with the keyboard, but not when a button is clicked with a mouse.
🔬 Try it yourself
The two tools below show what a screen reader reads from your HTML, and how a screen reader announces a message that has just appeared.
TL;DR: Accessibility means everyone can use your page. Use real tags (<button>, <a>, <nav>) before ARIA (extra attributes for screen readers). Images need alt, icon buttons need aria-label, and <label for> must match the input's id. role names an element's role, and aria-hidden="true" hides decoration from screen readers. Never mark an error with colour alone, and keep contrast at least 4.5:1.
⚡ Use the real tag
Write:
<button>Click Me</button>
<img src="photo.jpg" alt="User profile photo">
Meaning: A real <button> can be reached with Tab, works when you press Enter, and a screen reader reads it as "button". A <div> made to look like a button gets none of that. alt is read aloud in place of the image, so write what the image shows, not the word "image" or its file name.
⚡ A name for icon buttons
Write:
<button aria-label="Close menu">×</button>
Guess first: does a button that says "Save" need aria-label?
No. The word "Save" is already its name. With aria-label, a screen reader would read the aria-label and not that word.
Meaning: A button that only holds an icon has no clear name. aria-label gives it one, so a screen reader says "Close menu, button". Use the right tag first, and ARIA (attributes that start with aria-) only when plain HTML isn't enough.
⚡ Don't rely on colour alone
Meaning: Don't mark a wrong form field with red alone, because colour-blind users may not notice. Add a text message, such as "Email is empty", and keep the contrast (the difference between light and dark) between text and its background at least 4.5:1.