aria-label in accessibility, explained.
What is aria-label
is accessibility?
Let's explain
Suppose in your website you have a button:
<button></button>
This button appears when there's a popup window and its job is to close that window:
<button onclick="closeWindow()"></button>
To make the button as minimal as possible, you decide to give it a label "X", instead of "Close":
<button onclick="closeWindow()">X</button>
So the button will look like this:
Although the button looks nice, there's a problem.
A screen reader will announce the label of the button as is. So once the button has the focus, the screen reader will say:
Cap X, button
That doesn't really give an very good picture of what the button is supposed to do to the screen reader user. We need a clearer label, but at the same time, keep the "X" as our label.
What can we do?
We can use an attribute in our HTML, called aria-label
.
This is how it can be used in our button:
<button onclick="closeWindow()" aria-label="Close window">X</button>
So now the button will be announced like this:
Close window, button
In conclusion, if you need a screen reader to announce an element separately than its actual label, use aria-label
.
To learn more see this page on MDN
Dev, Explained (43 part series)
- Javascript Scopes, explained.
- Javascript Promises, explained.
- Accessibility, explained.
- React, explained
- Should I use forEach() or map()?
- Should I use Flexbox or CSS Grid?
- Docker, explained.
- Unit testing, explained
- Git, explained.
- Typescript, explained.
- async/await, explained.
- The DOM, explained.
- Regular expressions, explained
- GraphQL, explained.
- Vue, explained.
- Svelte, explained.
- API, explained.
- Javascript Hoisting, explained.
- Immediately Invoked Function Expressions (IIFE), explained.
- ARIA roles, explained.
- Test-driven Development, explained.
- ARIA live regions, explained.
- aria-label in accessibility, explained.
- Type coercion in Javascript, explained.
- Variables, explained.
- if statements, explained.
- Arrays, explained.
- Currying in Javascript, explained.
- Memoization, explained.
- For loops, explained.
- Javascript Prototypes, explained.
- React Hooks, explained.
- Graph databases, explained.
- MongoDB, explained.
- Serverless, explained.
- Javascript Callback functions, explained.
- HTML, explained.
- CSS, explained.
- Responsive design, explained.
- Javascript, explained.
- The CSS Box Model, explained.
- CSS Flexbox, explained.
- CSS Grid, explained.