HTML & CSSEasy
What is aria-label?
aria-label gives an element an accessible name as a string, which screen readers announce instead of (or in the absence of) visible text. Use it when an interactive element has no visible text label, such as an icon-only button, a search field without a visible label, or a second <nav> that needs distinguishing.
How accessible names are chosen (simplified priority)
aria-labelledby(references other elements' text)aria-label- Native labeling:
<label for>,alt,<caption>,<legend>, or the element's own text content title(last resort)
So aria-label overrides the visible text of a button, which can confuse voice-control users if the two differ.
Good uses
<button aria-label="Close dialog">✕</button><nav aria-label="Breadcrumb">and<nav aria-label="Primary">to tell landmarks apart.<input type="search" aria-label="Search products">when the design has no visible label (a visible<label>is still better).
Pitfalls
- Prefer visible text. Labels help everyone, not just screen reader users.
- If visible text exists, the accessible name should contain it (WCAG 2.5.3 Label in Name): visible "Buy" should not be labeled "Purchase item".
- It does not work reliably on non-interactive elements without a role, like a plain
<div>or<span>. - It is not translated by some auto-translation tools.
- Use
aria-labelledbywhen the label already exists on screen, andaria-describedbyfor extra hints.
Code example
html
1<style>2 body { font-family: system-ui; }3 .icon-btn { width: 40px; height: 40px; border-radius: 50%; border: 2px solid #0b1020; font-size: 18px; }4</style>56<nav aria-label="Primary"><a href="#">Home</a> · <a href="#">Docs</a></nav>7<nav aria-label="Breadcrumb"><a href="#">Docs</a> › <span aria-current="page">ARIA</span></nav>89<!-- Icon-only buttons need an accessible name -->10<button class="icon-btn" aria-label="Close dialog">✕</button>11<button class="icon-btn" aria-label="Add to favorites">♥</button>1213<!-- Prefer aria-labelledby when the label is already visible -->14<h2 id="billing">Billing address</h2>15<section aria-labelledby="billing">16 <input type="search" aria-label="Search addresses">17</section>1819<script>20 // The accessibility tree uses the aria-label, not the "✕" glyph21 for (const b of document.querySelectorAll(".icon-btn")) {22 console.log(b.textContent, "->", b.getAttribute("aria-label"));23 }24</script>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.