When to use ARIA attributes in HTML
42 ARIA only where the HTML runs out
hardBuild a disclosure, a navigation with the current page marked, and a toggle button. Use ARIA for exactly one of them and no HTML element twice over.
Stuck? Three hints, smallest first
- 1.
<details>and<summary>are a disclosure widget already. They need no role and noaria-expanded. - 2.
<nav>is already a navigation landmark, sorole="navigation"on it says the same thing twice. - 3. A pressed state has no HTML equivalent, so
aria-pressedon a real<button>is the legitimate case.
<details class="drop">
<summary>Baggage rules</summary>
<p>One bag up to 20kg is included. Extra bags are charged at the window.</p>
</details>
<nav class="bar" aria-label="Fares">
<a href="#std" aria-current="page">Standard</a>
<a href="#exec">Executive</a>
</nav>
<button class="toggle" type="button" aria-pressed="false">Remind me</button>
.drop {
margin-bottom: 10px;
padding: 8px 10px;
border: 1px solid #c9cad1;
border-radius: 8px;
font-size: 0.85rem;
}
.drop summary { cursor: pointer; font-weight: 600; }
.drop summary:focus-visible {
outline: 2px solid #3055d8;
outline-offset: 2px;
}
.drop p { margin: 8px 0 0; color: #5c5c66; }
.bar {
display: flex;
gap: 14px;
margin-bottom: 10px;
font-size: 0.85rem;
}
.bar a { color: #3055d8; }
/* Styled from the ARIA attribute, so the mark and the announcement agree. */
.bar a[aria-current='page'] {
color: #17171a;
font-weight: 700;
text-decoration: none;
border-bottom: 2px solid #17171a;
}
.toggle {
padding: 8px 14px;
border: 1px solid #3055d8;
border-radius: 8px;
background: #ffffff;
color: #17171a;
font: inherit;
cursor: pointer;
}
.toggle[aria-pressed='true'] {
background: #3055d8;
color: #ffffff;
}
.toggle:focus-visible {
outline: 2px solid #17171a;
outline-offset: 2px;
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
test('the disclosure is native, with no role bolted on', hasTag(html, 'details') && /role=/.test(html) === false, true);
test('and needs no aria-expanded, because the element owns that state', /aria-expanded/.test(html), false);
test('the nav is a landmark already, so no redundant role', hasTag(html, 'nav') && /role="navigation"/.test(html) === false, true);
test('the current page is marked with aria-current', /aria-current="page"/.test(html), true);
test('and styled from that same attribute', /\[aria-current='page'\]/.test(css), true);
test('the toggle is a real button, not a div', /<button class="toggle"/.test(html), true);
test('carrying the one state HTML has no element for', /aria-pressed="false"/.test(html), true);
test('which the stylesheet also reads', /\[aria-pressed='true'\]/.test(css), true);
Output
The rule of thumb is that no ARIA beats bad ARIA, and a native element beats both. <details> already announces expanded and collapsed, so adding aria-expanded gives a screen reader two sources of truth that can disagree the moment your JavaScript slips. role="navigation" on a <nav> is merely redundant, which is harmless but tells the next reader you were guessing. aria-pressed is the honest case: HTML has no element for “this button is currently on”, so the attribute carries it — and styling from [aria-pressed='true'] keeps the visible and the announced state the same fact.