aria-labelledby: Referencing Existing Labels
You'll learn how to connect an element with its label by referencing another element's ID, making content accessible without duplicating text.
The Core Idea
Sometimes a visible label already exists on your page—maybe it's a heading, a button's text, or a paragraph. Instead of repeating that text with aria-label, you can use aria-labelledby to point the assistive technology to the existing element. You do this by referencing the ID of the element that contains the label text.
Think of it like giving directions: instead of saying "turn left at the coffee shop" every time, you just say "see that place?" and point to it. aria-labelledby is the "pointing" mechanism.
How It Works
You assign an id to the element that contains the label text, then add aria-labelledby to the element you want to label, setting its value to that ID:
<h2 id="settings-title">Account Settings</h2>
<section aria-labelledby="settings-title">
<!-- Settings form content -->
</section>
Now screen readers know the section is labeled "Account Settings" by referencing the heading.
Why Use aria-labelledby?
- Avoids duplication: The visible text serves as the accessible name
- Stays synchronized: If you update the heading text, the accessible name updates automatically
- Multiple references: You can reference multiple IDs (space-separated) to combine labels
<div id="username-label">Username</div>
<div id="username-hint">(must be unique)</div>
<input type="text" aria-labelledby="username-label username-hint">
The input is now labeled "Username (must be unique)" for screen readers.
Key Takeaway: Use aria-labelledby to connect an element with existing visible labels elsewhere in your document by referencing their IDs—it keeps your code DRY and ensures labels stay consistent.