All interactive elements must have an accessible name. Screen readers will announce this name to readers.
If you want to give users more information, you can add an accessible description.
Providing an accessible name
There are three ways to give an element an accessible name:
- Using the text content
aria-labelledbyaria-label
Using the text content
The text content of an element is an accessible name. If screen reader reads the element, itโll read the contents, plus the role of the element.
For example, if you write โsubmitโ in a <button>, submit is the accessible name.
<button>Submit</button>You should use text content to create accessible names whenever possible. If you cannot use text content, you can still use aria-labelledby or aria-label.
Using aria-labelledby
aria-labelledby lets you use another element as an accessible name. It takes in the id of the target element.
Hereโs an example. Letโs say you have an <article> element. You can provide screen readers with the articleโs name with aria-labelledby.
<article aria-labelledby="article-title"> <h2 id="article-title">The Story of Goldilocks and the Three Bears</h2></article>Note: NVDA doesnโt announce <article>.
Using aria-label
aria-label is a third way to give elements an accessible name. The accessible name is the value given to aria-label.
It should be used when:
- You canโt use
textContent - It is difficult to use
aria-labelledby
When you use aria-label, screen readers will read value in aria-label. They will not read the text content.
<button aria-label="Close menu">X</button>Providing an accessible description
Screen reader users may skip around content with keyboard shortcuts. For example, they may Tab into a password field without going through any hints provided to a sighted user.
<form> <label for="password">Password</label> <p id="hint">Passwords should have at least one uppercase character.</p> <input type="password" id="password" placeholder="โขโขโขโขโขโขโขโข" /></form>In this case, the screen reader user wonโt know the password needs an uppercase character.
You can provide this (important, but easily missed) information with aria-describedby.
aria-describedby works like aria-labelledby. You pass in the id of the description into aria-describedby.
<form> <label for="password">Password</label> <p id="hint">Passwords should have at least one uppercase character</p> <input type="password" id="password" placeholder="โขโขโขโขโขโขโขโข" aria-describedby="hint" /></form>Important note
aria-label, aria-labelledby, and aria-describedby can only be used on elements with landmark and widget roles.
Screen readers may not speak the accessible name or description if you use them on other roles.
Welcome! Unfortunately, you donโt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson