Sub topic 6
Highlight the current page with aria-current
A navigation bar should show visitors which page they are on. Sighted users get this from a highlighted link. Screen reader users need the same information in a form their software can read. This chapter uses one HTML attribute, aria-current, to serve both groups.
This chapter assumes you have the semantic nav, ul, li and a markup from the earlier chapters. It also assumes the Flexbox row with padded block links, and that you know CSS attribute selectors such as a[href]. The JavaScript section uses querySelectorAll and URL.
Why a CSS class is not enough
The common approach is to add a class such as active to the matching link and style it. That works visually. Assistive technology ignores class names, so a screen reader user hears every link described the same way and cannot tell which page is open.
aria-current is a standard attribute that states "this element represents the current item within a set." Browsers expose it to assistive technology, and CSS can select on it. One attribute drives both the announcement and the visual style, so the two cannot drift apart.
The markup
Add aria-current="page" to the link whose destination is the page being shown. Every other link gets nothing.
<nav aria-label="Main">
<ul class="nav-list" id="nav-list">
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/products">Products</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>Keep href on the current link. Some sites remove the link or turn it into a plain span. That changes the tab order between pages and removes the link from the screen reader's list of links. A link to the page you are already on is harmless, and the attribute tells the user what it means.
The accepted values
The attribute takes a token, not a free-form string.
| Value | Meaning | Typical use |
|---|---|---|
| `page` | The current page within a set of pages | Site navigation, pagination |
| `step` | The current step in a process | Checkout or wizard progress |
| `location` | The current location in a flow or environment | A breadcrumb trail, a map |
| `date` | The current date in a calendar | Date pickers |
| `time` | The current time in a timetable | Schedules |
| `true` | The current item, when no other value fits | Fallback |
| `false` | Not the current item | Same as omitting the attribute |
For a site navigation bar you will almost always use page. Unrecognized values are treated as true, so a typo such as aria-current="pages" silently becomes a generic "current" and your CSS selector stops matching.
Style the current page from the attribute
The attribute selector lets the stylesheet react to the same attribute that assistive technology reads. Continue from the list reset and link box rules in the earlier chapters:
.nav-list a[aria-current="page"] {
background: #111827;
border-bottom: 3px solid #38bdf8;
}The rule has two visual cues. The background is darker than the other links, and a 3px bottom border is added.
A rule that sets only a background colour change fails people with colour vision deficiency. A change in shape or weight, such as a border, underline or bold text, does not depend on telling two colours apart. Treat the border as the primary cue and the background as reinforcement.
Layout shift
Adding a 3px border to one link makes that link 3px taller than its neighbours. In a Flexbox row with the default align-items: stretch, the neighbours stretch to match, so the bar does not jump. If you change the alignment, you can see a visible mismatch. A safer pattern is to give every link a transparent border and only change its colour:
.nav-list a {
display: block;
padding: 0.75rem 1rem;
color: #ffffff;
text-decoration: none;
border-bottom: 3px solid transparent;
}
.nav-list a[aria-current="page"] {
background: #111827;
border-bottom-color: #38bdf8;
}Now every link reserves the same space, and the current link only changes the border colour.
Specificity and rule order
The hover rule from the previous chapter is .nav-list a:hover. The current-page rule is .nav-list a[aria-current="page"]. A pseudo-class and an attribute selector have the same weight, so both rules score the same specificity: one class, one pseudo-class or attribute, and one element. When specificity ties, the later rule wins.
If the current-page rule comes after the hover rule, hovering over the current link does not change its background. That is usually what you want, because the highlight stays stable. If you put it first, hover overrides it. Decide on purpose and keep the order consistent.
Setting the attribute
Someone or something must decide which link is current. There are two reasonable approaches.
Server side or at build time
If a server template or static site generator renders each page, it knows the page being built. It can write aria-current="page" straight into the HTML. This is the best option. The attribute is present before any JavaScript runs, and it works if scripts fail to load or are blocked.
Client side
Sometimes you ship the same navigation fragment to every page, such as a shared include copied into static files. A small script can set the attribute at load time:
const normalize = (path) => (path.length > 1 ? path.replace(/\/+$/, '') : path);
const currentPath = normalize(window.location.pathname);
const links = document.querySelectorAll('.nav-list a');
links.forEach((link) => {
const linkPath = normalize(new URL(link.href, window.location.origin).pathname);
if (linkPath === currentPath) {
link.setAttribute('aria-current', 'page');
} else {
link.removeAttribute('aria-current');
}
});The script handles three details:
link.hrefreturns an absolute URL, so a relativehref="about"is resolved the same way the browser resolves it for navigation.pathnameleaves out the query string and the hash, so/products?sort=pricestill matches the link to/products.normalizeremoves trailing slashes, so/about/and/aboutcompare as equal. The root path/is left alone.
The script does not handle a server that serves the home page at both / and /index.html. If your site does that, either redirect one to the other or add a rule for it. The script also runs only once, so a single-page application that changes the URL without a page load must call it again after each route change.
Edge cases
Only one current link per set
Mark at most one link as page in a given navigation. If two links point to the same page, such as a logo link and a Home link in the same list, mark only the one inside the primary list, or neither of the duplicates. Several "current page" announcements in one landmark are confusing.
Pages inside a section
Suppose the reader is on /products/software and the bar has a Products item with a submenu. Which link is current? Only the exact page link, Software, gets aria-current="page". The Products link points to a different page, so marking it as current would be inaccurate.
You may still want the parent highlighted so sighted users see which section they are in. Use a class for that purely visual purpose, and keep the semantic attribute exact:
.nav-list .is-ancestor > a {
border-bottom-color: #38bdf8;
}Add is-ancestor to the parent li on the server or in your script. The class carries only presentation. The attribute carries meaning.
Do not use aria-selected or role="tab"
aria-selected belongs to widgets such as tabs and listboxes. A navigation link is not one of those, and using it on a link has no defined meaning. Use aria-current.
Do not duplicate the announcement
Some developers add visually hidden text such as "(current page)" inside the link. With aria-current="page" already present, screen readers that support it announce the state, so the hidden text can be read twice. Support for aria-current is good in current screen readers, so rely on the attribute.
Keyboard focus is separate
aria-current says nothing about keyboard focus. A user can tab to any link, including the current one. Keep the focus outline from the previous chapter visible on the current link. The :focus-visible outline and the current-page border must be distinguishable from each other. In the earlier styles, the outline sits inside the link with a negative offset, while the current marker is the bottom border, so both remain visible when they coincide.
Checking your work
- Load the page and confirm exactly one link has
aria-current="page"in the browser's developer tools element inspector. - Open the accessibility panel in the tools and check that the link exposes a "current" property.
- Turn on a screen reader and move through the links. The current link should be announced with its state, for example "Home, current page, link".
- Remove the background and border in the tools temporarily. The page should still be understandable, because the attribute carries the meaning.
- Press Tab through the bar and verify the focus ring is visible on the current link.
Exercise
A teammate writes .nav-list a[aria-current] { border-bottom: 3px solid #38bdf8; }, and the script sets aria-current="false" on every non-current link. What goes wrong, and how do you fix it?
Show answer
The selector [aria-current] matches any element that has the attribute, whatever its value, so every link with aria-current="false" gets the border as well. The fix is to select on the value, a[aria-current="page"], or to remove the attribute from non-current links instead of setting it to false.
Exercise
The reader is on /products/hardware. Which link gets aria-current="page": Products or Hardware? How do you still highlight Products visually without changing what screen readers announce?
Show answer
Hardware gets aria-current="page", because it is the link to the page being shown. Products points to a different page. To highlight Products for sighted users, add a class such as is-ancestor to its list item and style it with CSS, for example .nav-list .is-ancestor > a { border-bottom-color: #38bdf8; }. The class changes only the appearance.
Exercise
Why does the client-side script compare pathname values instead of comparing link.href with window.location.href?
Show answer
href values include the query string and hash, so /products?sort=price would not equal the link to /products. Comparing pathname ignores those parts. It also lets new URL(link.href, window.location.origin) resolve relative links, and the normalize helper removes trailing slashes so /about/ matches /about.