Reviewing aria-labelledby

Example 1: Using a child element

When aria-labelledby references a descendant element, the referenced element's text becomes the accessible name.

Accessible name for the <nav> should be: About us

Source:

HTML markup
<nav aria-labelledby="aardvark">
  <h3 id="aardvark">About us</h3>
</nav>

Example 2: Anywhere in the DOM

Buy Lawn Mower

Accessible name for the <button> should be: Buy Lawn Mower

Source:

HTML markup
<p id="bat">Buy Lawn Mower</p>
<button aria-labelledby="bat">Buy</button>

Example 3: More than one source

Buy Lawn Mower

On special

Accessible name for the <button> should be: Buy Lawn Mower On special

Source:

HTML markup
<p id="cheetah">Buy Lawn Mower</p>
<button aria-labelledby="cheetah zebra">Buy</button>
<p id="zebra">On special</p>

Example 4: Reference the element itself

Lawn Mower

On special

Accessible name for the <button> should be: Buy Lawn Mower On special

Source:

HTML markup
<p id="echidna">Lawn Mower</p>
<button id="dingo" aria-labelledby="dingo echidna ferret">Buy</button>
<p id="ferret">On special</p>

Example 5: Hidden elements via display: none

Accessible name for the <button> should be: Buy now

Source:

HTML markup
<button aria-labelledby="gazelle">Buy</button>
<p id="gazelle" style="display: none">Buy now</p>

Example 6: Hidden elements via CSS

Accessible name for the <button> should be: Buy now

Source:

HTML markup
<button aria-labelledby="hamster">Buy</button>
<p id="hamster" class="hidden">Buy now</p>

Example 7: Hidden element via aria-hidden="true"

Accessible name for the <button> should be: Goodbye world

Source:

HTML markup
<button aria-labelledby="ibex"></button>
<p id="ibex" aria-hidden="true">Goodbye world</p>

Example 8: Empty strings

If the element referenced by aria-labelledby is empty, it does not contribute an accessible name. The accessible name is then determined from any other available naming source, if one exists.

Accessible name for the <button> should be: Hello world

Source:

HTML markup
<button aria-labelledby="jackal">Hello world</button>
<p id="jackal"></p>