ARIA labels
aria-label
or aria-labelledby
.
aria-label
Without the use of an ARIA label, the text content of an element is used to represent said element by a screen reader.
Without further modification, all elements (text, buttons, links, inputs, and more) will be read by the screen reader based on their text while also announcing the type of interaction if they are interactive. This, of course, can be great if your text content already provides all the details necessary to understand it, but our buttons are rather bare-bones.
Since the text "Image:" will not be announced when manually navigating between the buttons, as with tab navigation, the buttons are only known as "Save", "Save As", and "Load" to people reliant on screen readers.
To make them more accessible, we can simply add an ARIA label to each of the buttons with a more descriptive name, like "Save image". This way, any navigation between the buttons will result in the screen reader announcing the buttons with their intended purpose. It is very helpful when different parts of your website provide similar actions with the same visible text but operate in different context, like "Save image" in the image section and "Save audio" in the sound section of a video editing tool.
Do not create labels with entirely different content than visible text, rather expand on the text.
aria-labelledby
The aria-labelledby
attribute has similar use as aria-label
.
Rather than specifying an explicit name for an element, it references another element (or multiple elements) containing the name.
Keep in mind, this functionality can easily be provided with native label elements
As stated, it is possible to use multiple elements in the labelledby attribute. Simply write your target element IDs separated by a whitespace. The advantage of this is the ability to change individual parts of the message, like an item count, using JavaScript without rewriting the entire label.
It can be desirable to hide the elements used as labelledby to prevent unintended narration of the content.