Briefly
Widget property from WAI-ARIA, which sets an accessible name for an element.
The name from the aria
attribute is only visible to assistive technologies and hidden from other users.
Example
<button aria-label="Pause"> <svg viewBox="0 0 35 42" width="35" height="42" xmlns="http://www.w3.org/2000/svg" > <!-- Description of the shape --> </svg></button>
<button aria-label="Pause"> <svg viewBox="0 0 35 42" width="35" height="42" xmlns="http://www.w3.org/2000/svg" > <!-- Description of the shape --> </svg> </button>
How to write
Assign the aria
attribute to the tag with the desired text.
aria
can be assigned to all interactive elements that can be interacted with. The attribute can also be used for some non-interactive elements — tables, modal windows, landmarks, or groups of elements. Just keep in mind that browsers and screen readers poorly support aria
for static content.
There are tags and roles for which aria
cannot be used:
<caption>
and rolecaption
.<code>
and rolecode
.<dd>
and roledefinition
.<dt>
,<dfn>
and roleterm
.<del>
and roledeletion
.<em>
and roleemphasis
.<ins>
and roleinsertion
.<mark>
and rolemark
.<p>
and roleparagraph
.<strong>
and rolestrong
.<sub>
and rolesubscript
.<sup>
and rolesuperscript
.<time>
and roletime
.<span>
,<div>
and rolegeneric
.- roles
presentation
/none
andsuggestion
.
The name in aria
should be brief and not repeat the role of the element. For example, if it is the main navigation, name it "Main" instead of "Main navigation." In the latter case, the screen reader will say "Main navigation, navigation." Too many navigations! Also, try to give unique names to landmarks — parts of the page that users of screen readers can quickly jump to.
Names from aria
can be changed dynamically, but this is a potential problem for voice control users. Such users may find it difficult to guess how the name in aria
has changed, upon which voice commands depend. This also applies to situations where unpredictable names are used. For example, a button with a house icon is named not "Home," but "Unread messages."
Keep in mind that aria
overrides the text content of some tags and elements with ARIA roles, as well as from ARIA attributes. The exception is another attribute aria
. It always overrides aria
, so you shouldn't assign both to the same element.
If an element already has a visible name, it is better not to override it in aria
. This is redundant and may confuse sighted users of screen readers. For instance, a button may have the name "Agree," but the screen reader will read a completely different text from aria
"I accept the terms of the agreement."
<!-- ⛔ It's better not to do this --><button aria-label="I accept the terms of the agreement">Agree</button>
<!-- ⛔ It's better not to do this --> <button aria-label="I accept the terms of the agreement">Agree</button>
When a <table>
has both <caption>
and aria
, the table will get its name from the attribute, while the content of <caption>
will become its description.
How to understand
The accessible name or simply the name of an element is a brief title of the element that the screen reader vocalizes when it is focused or when it reads everything in succession.
Most often, it is sufficient to add text to the element, for example, in a <button>
or <a>
. In the case of an image, the source of the name is alt
. Other elements can get their name from title
as does <iframe>
.
If none of these methods can be used, the aria
attribute is one of the additional ways to set the name of the element. It will be useful in the following cases:
- Several landmarks on the page. For example,
<header>
or<footer>
. - A group of elements that needs to be conveyed to assistive technologies.
- Interactive elements without textual content. For example, a button with an icon.
Tips
💡 Don’t forget to translate the content of the attribute when supporting multiple languages.
💡 The content of aria
sometimes does not work well with automatic translation, and previously was not translated by any services at all.
💡 When an element needs to have a visible name for everyone, use aria
.
In practice
Advice 1
🛠 Try to use visible text labels for buttons with icons. This will help sighted users, as the meaning of icons is not always clear. It is especially difficult for voice control users and people with cognitive impairments to understand icons.
🛠 A good alternative to aria
is visually hidden content with the class .visually
or .vh
. This will avoid problems with automatic translation, and a screen reader will definitely read it in any navigation mode.
<button> <svg viewBox="0 0 35 42" width="35" height="42" xmlns="http://www.w3.org/2000/svg" > <!-- Description of the shape --> </svg> <span class="visually-hidden">Pause</span></button>
<button> <svg viewBox="0 0 35 42" width="35" height="42" xmlns="http://www.w3.org/2000/svg" > <!-- Description of the shape --> </svg> <span class="visually-hidden">Pause</span> </button>
.visually-hidden { position: absolute; width: 1px; height: 1px; clip: rect(0 0 0 0); clip-path: inset(50%); overflow: hidden; white-space: nowrap;}
.visually-hidden { position: absolute; width: 1px; height: 1px; clip: rect(0 0 0 0); clip-path: inset(50%); overflow: hidden; white-space: nowrap; }
If you only support new browsers, you can avoid using the already deprecated property clip
.