← Back to Frontend, page 2

frontend-0039 · Medium mid

How does the browser work out the accessible name of a control, and how do you check that a control has the name you expect?

The accessible name comes from, in order of strength: aria-labelledby, aria-label, then native sources such as a label element, alt text or the element's text content, and finally title. Check it in the accessibility tree or with a test query such as getByRole with a name.

Full answer

A screen reader announces a control by role and name, for example "Search, edit text" or "Close, button". If the name is missing or wrong, the control is unusable.

The browser follows the accessible name computation. In simplified form, for most controls:

  1. aria-labelledby points to other elements by id, and their text is used. It wins over everything else.
  2. aria-label supplies a string directly.
  3. The native source: the <label> linked to an input, alt for an image, <legend> for a fieldset, <caption> for a table, or the text inside a button or link.
  4. title or placeholder as a last resort. They are weak: title shows only on hover, and placeholder disappears when typing.

Practical guidance:

  • Prefer visible text. A visible <label for> helps everybody, including people with cognitive or motor difficulties, and voice control users can say the label.
  • Use aria-label for icon-only buttons, such as a magnifier button: aria-label="Search". Remember that translated pages must translate it too.
  • The visible text and the accessible name should match, or contain the visible text, so that voice control commands like "click Search" work (WCAG 2.5.3, Label in Name).
  • Images that carry meaning need alt. Decorative ones need alt="".
  • Mark an icon inside a named button with aria-hidden="true" so that it does not add noise.

How to check:

  • The Accessibility panel in DevTools shows the computed name.
  • In tests, getByRole('button', { name: 'Search' }) fails when the name is wrong, so the test also guards accessibility.
  • Automated checkers find missing names, but not wrong names.

The test below builds four controls with different sources and uses Playwright's accessibility snapshot to read the names that Chrome computed.

html
<label for="email">Email address</label>
<input id="email" type="email">

<button type="button" aria-label="Search"><svg aria-hidden="true" width="12" height="12"><circle cx="6" cy="6" r="5"/></svg></button>

<h2 id="t">Newsletter</h2>
<input type="text" aria-labelledby="t">

<img src="/logo.png" alt="Schoolabe logo">

Run in headless Chrome 156 through Playwright. Two full runs gave the same results. The check read the accessibility snapshot in Chrome. All four controls had the expected names. No screen reader was run, and other browsers compute names slightly differently in rare cases.

Key points

  • The name comes from aria-labelledby, aria-label, native sources, then title.
  • Visible labels are best. Use aria-label for icon-only controls.
  • Match the visible text so that voice control works.
  • Decorative images use empty alt.
  • Check names in DevTools or with getByRole in tests.

Common mistakes

  • Using placeholder as the only label.
  • An aria-label that differs from the visible text.
  • Leaving an icon button with no name.
  • Adding aria-label to a div with no role, where it may be ignored.

Follow-ups

  • Which wins when both aria-label and a label element exist?
  • How does aria-describedby differ from aria-labelledby?
  • Why can a good test query double as an accessibility check?

Tips

  • List the sources in order of strength.
  • Give the icon button case as the main reason for aria-label.