Labels and text alternatives
In order for a screen reader to present a spoken UI to the user, meaningful elements must have proper labels or text alternatives. A label or text alternative gives an element its accessible name, one of the key properties for expressing element semantics in the accessibility tree.
When an element's name is combined with the element's role, it gives the user context so they can understand what type of element they're interacting with and how it is represented on the page. If a name is not present, then a screen reader only announces the element's role. Imagine trying to navigate a page and hearing, "button," "checkbox," "image" without any additional context. This is why labeling and text alternatives are crucial to a good, accessible experience.
Inspect an element's name
It's easy to check an element's accessible name using Chrome's DevTools:
- Right-click on an element and choose Inspect. This opens the DevTools Elements panel.
- In the Elements panel, look for the Accessibility pane. It may be hidden
- In the Computed Properties dropdown, look for the Name property.
Whether you're looking at an
alt text or an
input with a
label, all of these scenarios result in the same outcome: giving an
element its accessible name.
Check for missing names
There are a number of accessibility and SEO audits in Lighthouse that test for missing accessible names. Each of the fixes listed in the following table is also explained further below.
|Failed audit||How to fix|
|Document has a <title> element||Label documents and frames|
|<frame> or <iframe> elements have a title||Label documents and frames|
|Image elements have [alt] attributes||Include text alternatives for images and objects|
|<input type="image"> elements have [alt] text||Include text alternatives for images and objects|
|<object> elements have [alt] text||Include text alternatives for images and objects|
|Buttons have an accessible name||Label buttons and links|
|Links have a discernible name||Label buttons and links|
|Form elements have associated labels||Label form elements|
Label documents and frames
Every page should have a
element that briefly explains what the page is about. The
title element gives
the page its accessible name. When a screen reader enters the page, this is the
first text that is announced.
For example, the page below has the title "Mary's Maple Bar Fast-Baking Recipe":
<!doctype html> <html lang="en"> <head> <title>Mary's Maple Bar Fast-Baking Recipe</title> </head> <body> … </body> </html>
iframe elements should have
<iframe title="An interactive map of San Francisco" src="…"></iframe>
iframe's contents may contain their own internal
title element, a
screen reader usually stops at the frame boundary and announces the element's
role—"frame"—and its accessible name, provided by the
title attribute. This
lets the user decide if they wish to enter the frame or bypass it.
Include text alternatives for images and objects
img should always be accompanied by an
attribute to give the image its accessible name. If the image fails to load,
alt text is used as a placeholder so users have a sense of what the
image was trying to convey.
alt text is a bit of an art, but there are a couple of guidelines
you can follow:
- Determine if the image provides content that would otherwise be difficult to attain from reading the surrounding text.
- If so, convey the content as succinctly as possible.
If the image acts as decoration and does not provide any useful content,
you can give it an empty
alt="" attribute to remove it from the accessibility
alttext by checking out WebAIM's guide to Alternative Text.
Images as links and inputs
An image wrapped in a link should use the
alt attribute to describe
where the user will navigate to if they click on the link:
<a href="https://en.wikipedia.org/wiki/Google"> <img alt="Google's wikipedia page" src="google-logo.jpg"> </a>
Similarly, if an
<input type="image"> element is used to create an image
button, it should contain
alt text that describes the action that occurs when
the user clicks on the button:
<form> <label> Username: <input type="text"> </label> <input type="image" alt="Sign in" src="./sign-in-button.png"> </form>
<object> elements, which are typically used for embeds like Flash, PDFs, or
ActiveX, should also contain alternative text. Similar to images, this text is
displayed if the element fails to render. The alternative text goes inside the
object element as regular text, like "Annual report" below:
<object type="application/pdf" data="/report.pdf" width="600" height="400"> Annual report. </object>
Label buttons and links
Buttons and links are often crucial to the experience of a site, and it is important that both have good accessible names.
button element always attempts to compute its accessible name using its
text content. For buttons that are not part of a
form, writing a clear
action as the text content may be all you need to create a good accessible
One common exception to this rule is icon buttons. An icon button may use an image or an icon font to provide the text content for the button. For example, the buttons used in a What You See Is What You Get (WYSIWYG) editor to format text are typically just graphic symbols:
When working with icon buttons, it can be helpful to give them an explicit
accessible name using the
aria-label overrides any
text content inside the button, letting you clearly describe the action to
anyone using a screen reader.
<button aria-label="Left align"></button>
Similar to buttons, links primarily get their accessible name from their text content. A nice trick when creating a link is to put the most meaningful piece of text into the link itself, rather than filler words like "Here" or "Read More."
Not recommended — not descriptive:
Check out our guide to web performance <a href="…">here</a>.
Recommended — useful content:
Check out <a href="…">our guide to web performance</a>.
This is especially helpful for screen readers that offer shortcuts to list all of the links on the page. If links are full of repetitive filler text, these shortcuts become much less useful:
Label form elements
There are two ways to associate a label with a form element such as a checkbox. Either of the methods causes the label text to also become a click target for the checkbox, which is also helpful for mouse or touchscreen users. To associate a label with an element, either:
- Place the input element inside a label element
<label> <input type="checkbox">Receive promotional offers?</input> </label>
- Or use the label's
forattribute and refer to the element's
<input id="promo" type="checkbox"></input> <label for="promo">Receive promotional offers?</label>
When the checkbox has been labeled correctly, the screen reader can report that the element has a role of checkbox, is in a checked state, and is named "Receive promotional offers?" like in the VoiceOver example below: