Accessible Names and Descriptions

Intermediate · 37 min

How the browser works out what a control is called: label, aria-labelledby, aria-label, content, title, and where descriptions from aria-describedby fit.

Before thisARIA Roles, States and PropertiesForms, Labels and SubmissionBuilt-in Form Validation

A11y Bytes · 37 min

Accessible Names and Descriptions

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by A11y BytesReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • The accessible name is what a screen reader says when focus lands on a control. It is computed from several sources in a fixed order, so you can find out which one won.
  • In a test the order was: `aria-labelledby`, then `aria-label`, then a native `<label>`, then the element's own content, then `title` or `placeholder` as a last resort.
  • A description is extra information after the name, not a replacement for it. `aria-describedby` can point at several elements and their text is joined.
  • An icon-only button has no name until you give it one. A visually hidden text span, `aria-label`, or an image with `alt` inside a link all work.
  • `title` becomes the name only when nothing else does. On a button that already has text, it became the description instead.

Every control needs a name

When a keyboard user tabs to a control, the screen reader announces something like “Email, edit text” or “Save, button”. The first word is the accessible name, and it’s what the person uses to decide whether this is the field they wanted. A control without one is announced as “button” or “edit text” and nothing else. Voice control users have the same problem: “click Save” only works if a control is called Save.

The browser computes the name for you, from clues in the markup. The trouble is that there are several clues, and when two disagree, one wins silently. Knowing the order is how you stop guessing.

The order, as tested

I put a set of controls through Chromium 159 and read the computed name and where it came from, using the DevTools protocol:

Markup Name Source
<label for> and aria-label “ARIA label” aria-label
<label for>, aria-label and aria-labelledby text of the referenced element aria-labelledby
Input with only title “Title only” title
Input with only placeholder “Placeholder only” placeholder
<a href><img alt="Home page"></a> “Home page” content
<button> containing a hidden-from-tree SVG and a visually hidden span “Close dialog” content
Button containing only an SVG (empty) none

That reads as: aria-labelledby first, then aria-label, then the native <label>, then content for elements that take their name from it (buttons, links, headings), and title or placeholder as a last resort. The W3C’s accname specification has the full algorithm with more steps. Two things follow. If you add aria-label to an input that already has a visible label, you have just hidden the visible label from assistive technology. And placeholder as a name is a fallback of last resort; the placeholder vanishes as soon as the user types.

A related slip from the same test: an input next to a <label> with no for and no nesting got no name at all. The forms lesson shows the two correct ways to connect them. Also, an aria-labelledby that points at an id which does not exist gave an empty name, so a typo there fails quietly.

Names from content, and the icon button

Buttons, links and headings are named from what is inside them. That is why <a href="/"><img src="logo.png" alt="Home page"></a> was named “Home page”: the link’s content is the image, and the image’s text alternative is its alt. It is also why a button holding only an icon gets nothing. The first fix is the visually hidden text span:

<button>
  <svg aria-hidden="true" width="10" height="10">…</svg>
  <span class="visually-hidden">Close dialog</span>
</button>

The second is aria-label="Close" on the button. Either works; the text span keeps the label in the page where it can be styled and found, and see hiding content for the class. If the visible text and the accessible name differ, a voice control user who says what they see will fail, so keep the visible words inside the name.

Descriptions come after the name

A description is a second, optional string. The name says what it is; the description says more: a format hint, an error message, a warning. In HTML you build one with aria-describedby, which takes a space-separated list of ids:

<label for="pw">Password</label>
<input id="pw" type="password" aria-describedby="hint hint2">
<p id="hint">At least 8 characters.</p>
<p id="hint2">Include a number.</p>

In the test the input’s name stayed “Password” and its description became “At least 8 characters. Include a number.” The same mechanism connects validation messages to the fields they describe. title takes part too: a button with text “Save” and title="Save the file" had that title as its description, not its name.

One caveat from the test: a plain div with aria-label still got a name in Chromium, even though the spec disallows naming a generic element. Don’t depend on it.

What the video covers

This is Russ Weakley’s talk “Everything you ever wanted to know about the accessibility tree” at A11y Bytes (November 2021, about 36 minutes). Its description says it is a deep dive into the accessibility tree, including how accessible names and descriptions are applied, how ARIA can affect the tree, and the order in which screen readers announce names, descriptions and more. I could not read a transcript, so I can’t point to specific demos. The table and the rules of precedence above are from my own run in one browser; other browsers, and screen readers themselves, can differ in what they announce.

Where this fits

This is the payoff of ARIA roles, states and properties: the attributes you will actually write most. It leans on forms and labels. Next, hiding content deals with the opposite question: what should leave the tree.

Check yourself

  1. 01An input has `<label for="a">Native label</label>` and `aria-label="ARIA label"`. What does the screen reader say, and why?

    "ARIA label". `aria-label` ranks above a native label in the name computation, and in a test it won. If both exist, the visible label is ignored by assistive technology, which can mismatch voice control users who say what they see.

  2. 02How would you give a close button containing only an SVG an accessible name?

    Mark the SVG `aria-hidden="true"` and add a visually hidden span with "Close dialog", or set `aria-label="Close"` on the button. In a test the SVG-only button had an empty name, and both fixes produced a name.

  3. 03A password field has hint text, "At least 8 characters.", in a paragraph. How do you connect them without changing the label?

    Give the paragraph an id and put `aria-describedby` with that id on the input. The tree's description for the field was "At least 8 characters. Include a number." when two ids were listed, while its name stayed "Password".

  4. 04What happens to a `title` attribute on a button that already says "Save"?

    It stops being the name and becomes the description: the test button had the name "Save" and the description "Save the file". On a control with no other name, `title` becomes the name, but that is a weak source.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark