Sub topic 6
Showing the current page with aria-current
This chapter assumes you can write semantic HTML lists and links, apply basic CSS selectors, and know what a landmark such as nav is. It covers how to tell users, both sighted and non-sighted, which page they are on.
Why color alone is not enough
Visitors use the current-page marker to answer "where am I?" A highlighted link answers that for sighted users. A screen reader user hears only the link text, so a visual style alone tells them nothing. Color-only cues also fail users with low vision or color blindness.
The aria-current attribute exposes the state to assistive technology. A screen reader typically announces it as "current page" after the link name.
Marking up the current page
Put aria-current="page" on the link that points to the page being shown. Give it to exactly one link per navigation list.
<nav aria-label="Primary">
<ul>
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/products">Products</a></li>
<li><a href="/pricing">Pricing</a></li>
<li><a href="/support">Support</a></li>
</ul>
</nav>Leave the attribute off the other links. Do not write aria-current="false" on every sibling. It is valid, but it adds noise and is easy to get wrong.
Styling from the attribute
Select on the attribute itself so the visual state and the announced state cannot drift apart. You need no extra class.
nav a[aria-current='page'] {
font-weight: 700;
text-decoration: underline;
}Use more than color. Weight and underline still show up in forced-colors mode and for color-blind users. Keep the contrast of the marker at least 3:1 against its surroundings.
Choosing the right value
aria-current accepts several tokens. For site navigation you mostly need two.
| Value | Use it when |
|---|---|
| `page` | The link points to the page the user is viewing. |
| `true` | The link is the current item in a set, and no more specific token fits. |
| `step` | The item is the current step in a process, such as a checkout. |
| `location` | The item is the current location in a flow or diagram. |
For primary navigation, use page.
Current page inside a section
Sometimes the user is on a child page, such as /products/widgets, while the navigation only lists /products. Two approaches work.
- Mark the parent link with
aria-current="true"to say it is the current section. Some teams keeppagestrictly for an exact match, so apply one rule across the site. - Show a breadcrumb trail on the child page, and put
aria-current="page"on the last breadcrumb.
Whichever you choose, the rule must be the same on every page.
Setting it on the server
The best place to set the attribute is the server template or the framework router, because the correct value is known when the HTML is generated. If you must do it in the browser, compare the link's path with the current one:
document.querySelectorAll('nav[aria-label="Primary"] a').forEach((link) => {
if (link.pathname === window.location.pathname) {
link.setAttribute('aria-current', 'page');
}
});This runs after load, so users may see a flash without the marker, and it fails when scripts are blocked. Prefer server rendering.
Common errors
- Using only a
.activeclass. Sighted users see it, and screen readers do not. - Marking several links as
pageat once. - Leaving the attribute on a link after a single-page app changes routes. Update it on every navigation.
- Removing the link's
hrefon the current page. Keep it as a working link so the keyboard order stays stable.
Exercise
A site's primary navigation highlights the current page with a .active class that only changes the background color. Name two problems with this and the fix.
Show answer
Screen reader users get no indication of the current page, and color alone can be missed by users with low vision or color blindness. Add aria-current="page" to the matching link, select on a[aria-current='page'] in CSS, and add a non-color cue such as bold weight or an underline.