Sub topic 6
Highlight the current page with aria-current
This chapter assumes you can write basic HTML and CSS, know how attribute selectors work, and understand the box model. You should already have the navigation bar from the earlier chapters: a nav element holding a list of links, laid out in a row with Flexbox, with padded block links and hover and focus styles.
Why mark the current page
A reader who lands on a page from a search result or a shared link needs to know where they are. A highlighted link answers that at a glance. Screen reader users need the same answer, but they can't see a color change.
The usual shortcut is a class such as class="active". A class only helps the visual styling, because assistive technology ignores it. The aria-current attribute fixes this. It is part of the WAI-ARIA specification, and browsers pass it to assistive technology. A screen reader typically announces the link as "current page".
Set aria-current in the markup
Add aria-current="page" to the one link that points to the page being viewed.
<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>The value page means "this link points to the page the reader is on". The attribute belongs on the link itself, not on the li or the nav. Only one link in the bar should carry it.
Style it with an attribute selector
You don't need a second class for the visual state. An attribute selector reads the same attribute that assistive technology reads.
.nav-list a[aria-current="page"] {
background: #111827;
border-bottom: 3px solid #38bdf8;
}The selector matches any link inside .nav-list whose aria-current is exactly page. This has two benefits:
- The visual state and the announced state can't drift apart, because both come from one attribute.
- A mistake shows up for everyone. If the attribute is missing, the highlight is missing too.
Avoid layout shift from the border
The 3px bottom border adds 3px to the link's height. Only the current link has it, so that link would be taller than its neighbors. Because the list is a Flexbox row, the other links stretch to match, but the current link's content can sit slightly differently.
A simple fix is to give every link a transparent border of the same width. The current page then only changes the color.
.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;
}Don't rely on color alone
The highlight here differs from the other links in background and in the underline. A reader with low vision or color blindness may not tell the dark backgrounds apart. The underline is a shape cue that survives, which is why the example uses both. If you ever style the current page with color only, add a second cue such as an underline, a heavier font weight, or an icon.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Using only `class="active"` | Screen readers get no information | Use `aria-current="page"` and select on it |
| Setting it on the `li` | Most screen readers don't announce it | Put it on the `a` element |
| Leaving it on every page's link in a shared template | Every page claims the same current page | Set it per page |
| Writing `aria-current="page "` with a stray space | The selector doesn't match | Use the exact value `page` |
Setting it per page
On a site with a handful of static pages, you edit the attribute by hand in each file. On a site built from a shared template, the template or server code should add it. If you can't change the server output, a small script can set it by comparing the link to the current path.
const links = document.querySelectorAll('.nav-list a');
links.forEach((link) => {
if (link.pathname === window.location.pathname) {
link.setAttribute('aria-current', 'page');
} else {
link.removeAttribute('aria-current');
}
});The pathname property of a link gives only the path part of its URL, so /products is compared with /products and the host is ignored. Setting the attribute in the server-rendered HTML is better when you can, because it works before any script has run.
Exercise
A teammate highlights the current page with <li class="active"> and .active a { background: #111827; }. Name two problems with this approach and show the markup and selector you would use instead.
Show answer
The highlight is visual only, so screen reader users aren't told which page is current. The class is also on the li, which means the state is not attached to the link the reader would focus. Use <li><a href="/products" aria-current="page">Products</a></li> with the selector .nav-list a[aria-current="page"] { background: #111827; }.
Exercise
The current link has border-bottom: 3px solid #38bdf8; and the other links have no border. What visual glitch can this cause, and how do you prevent it?
Show answer
Only the current link gains 3px of height, so its content can sit differently from its neighbors and the bar can look uneven. Give every link border-bottom: 3px solid transparent; and change only border-bottom-color for the aria-current="page" link.