Web Accessibility - ARIA Attributes
What is ARIA?
ARIA stands for Accessible Rich Internet Applications, and is a set of HTML attributes that give extra information to screen readers.
Note: ARIA does not change how a page looks. It only changes what assistive technology hears.
The First Rule of ARIA
First rule: Use a real HTML element whenever you can. A
<button> already works with the keyboard and screen readers.
A <div> with ARIA does not, unless you add a lot of extra code.
Example
<!-- Good -->
<button>Save</button>
<!-- Avoid -->
<div role="button">Save</div>
The aria-label Attribute
The aria-label attribute gives an element a name when there is no visible text. This is common for icon buttons:
Example
<button aria-label="Close">X</button>
A screen reader says "Close, button" instead of reading the X symbol.
If the name is already visible somewhere else on the page, use aria-labelledby to point to it:
Example
<h2 id="cartTitle">Shopping cart</h2>
<ul
aria-labelledby="cartTitle">...</ul>
The role Attribute
The role attribute tells assistive technology what an element is.
Most HTML elements already have a built-in role, so you only need role when no HTML element fits:
Example
<div role="alert">Your session will end in 1 minute.</div>
The role attribute can have one of the following values:
alert- indicates an important message that is read out right awaystatus- indicates a less urgent message, like "Saved"tab,tablist,tabpanel- indicates a set of tabsimg- indicates a group of elements that together form one image
The aria-live Attribute
When content changes without a page reload, screen readers may not notice!
The aria-live attribute tells them to read the change out loud:
Example
<p aria-live="polite" id="message"></p>
<script>
document.getElementById("message").textContent = "3 items added to your cart";
</script>
The aria-live attribute can have one of the following values:
polite- waits until the screen reader is done speaking. Use this most of the time.assertive- interrupts right away. Use only for urgent messages, like errors.
Tip: The live element must already be on the page before the text changes, or the update may not be read.
Common ARIA Attributes
The following table lists some common ARIA attributes:
| Attribute | Description |
|---|---|
| aria-label | Gives an element a name when there is no visible text |
| aria-labelledby | Points to the id of another element to use as the name label |
| aria-describedby | Points to the id of another element that provides extra description |
| aria-hidden="true" | Hides decorative/non-essential elements from assistive technologies |
| aria-expanded="true|false" | Indicates whether a collapsible section, dropdown, or accordion is open or closed |
| aria-live="polite|assertive" | Announces dynamic content updates without moving user focus |
| aria-current="page|step|..." | Marks the active item in a menu, breadcrumb, or multi-step process |
| aria-invalid="true|false" | Indicates that a form's input value fails validation |