ARIA (Accessible Rich Internet Applications) provides a list of attributes that lets you make the web accessible for screen readers.
There are three kinds of ARIA attributes.
- Roles
- Properties
- States
Aria-roles help screen readers define the type of element theyโre on while aria-properties and aria-states help screen readers understand the state of the role.
In practice, we donโt differentiate between aria-properties and aria-states. We use these two words interchangeably. Sometimes, we simply call them aria attributes.
You can find a list of roles, and properties in the specification.
Weโll talk about roles in this lesson.
Using aria-roles
Aria-roles define the type of element a screen reader is on.
There are two ways to declare roles:
- Use an HTML element with an implicit role
- Use the
roleattribute
HTML elements with implicit roles
Many HTML elements contain an implicit aria-role. If you use these elements, you donโt need to use the role attribute.
For example, the button element has a role set to button. If you use <button>, you donโt need to write role="button"
<button>This is a button</button>If you navigate to the button element on Voiceover, youโll hear โThis is a button, buttonโ.
- โThis is a buttonโ comes from the
textContentwithin the button element. - โbuttonโ comes from the
<button>element. It tells the user theyโre on a button.
If you navigate to the button element on NVDA, youโll hear โbutton, This is a buttonโ. The reading order differs, but the message remains the same.
Note: Iโll show you Voiceover videos most of the time since I use a Mac. Iโll supplement with NVDA videos when I need to make a specific point with NVDA.
Elements with implicit roles
Here are examples of elements with implicit roles. You should know most of them already ๐.
| Element | Role |
|---|---|
<a href="..."> |
link |
<article> |
article |
<aside> |
complementary |
<button> |
button |
<form> |
form |
<h1> to <h6> |
heading |
<img> |
img |
<input type=checkbox> |
checkbox |
<input type=button> |
button |
<input type=radio> |
radio |
<li> |
listitem |
<main> |
main |
<nav> |
navigation |
<ol> |
list |
<section> |
region |
<table> |
table |
<ul> |
list |
Using the role attribute
You can use the role attribute to tell screen readers what the element is. Screen readers will announce the role faithfully (whenever possible).
This means you can deceive screen readers into thinking a <div> is a <button>.
<button>Real button</button>
<!-- Don't do this! --><div role="button">Fake button</div>If you use a role attribute, you need to provide the users with functionality
that accompanies the role. For example, if you use role="button", you need to
make sure users can interact with your โbuttonโ with Space and Enter keys.
For this reason, you should always use HTML elements over roles. Only use the role attribute if no HTML elements can do the job.
If you use role on an element with an implicit role, the role property takes precedence.
<!-- Don't do this! --><button role="banner">Banner ๐</button>You should not overwrite a role unless it makes sense. Youโll see a real
example of what โmakes senseโ means when you make Tabby accessible.
Nested roles
If a role is nested in another role, screen readers will read both roles when you enter the element for the first time. They may omit the external role on subsequent elements.
<main> <button>Button 1</button> <button>Button 2</button> <button>Button 3</button></main>Thatโs it!
Next up
ARIA can be really confusing. You need to understand every aria-roles and aria-properties available. You also need to learn when to use each role and each property.
I found it useful to understand aria-roles first. So, Iโll share everything you need to know about aria-roles in the next 6 of lessons.
Iโll go in the following order:
- Landmark roles
- Document structure roles
- Live region roles
- Widget roles
- Window roles
- Abstract Roles
Have patience as you go through the next 6 lessons. Youโll realize you already know most aria-roles because theyโre built into HTML.
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