Sub topic 6
Showing the Current Page with aria-current
This chapter assumes you can write semantic HTML lists and links, you know basic CSS selectors, and you have read the earlier chapter on marking up navigation with nav, lists, and labels. By the end you will mark the current page in a navigation so that sighted users, screen reader users, and keyboard users can all tell where they are.
Why the current page needs a marker
Visitors who follow a link want to confirm that they landed where they expected. A highlighted item in the navigation answers that question at a glance. It also helps users who arrive from a search engine or a shared link, because they see where the page sits in the site.
Many sites show the current page only with color, for example a blue background on one item. That has two problems:
- Screen reader users do not hear color. Nothing in the markup says which link is the current one.
- Users with low vision or color-vision deficiency may not see the difference. WCAG 1.4.1 (Use of Color) says color must not be the only way to convey information.
The fix is to put the state in the markup with aria-current, then style that state in CSS using more than color.
The aria-current attribute
aria-current is an ARIA attribute that tells assistive technology that an element represents the current item within a set. A screen reader typically announces it as "current page" after the link name, so the user hears something like "Pricing, link, current page".
The attribute takes a token as its value:
| Value | Meaning | Typical use |
|---|---|---|
| `page` | The current page within a set of pages | Primary navigation, footer navigation |
| `step` | The current step in a process | Checkout or wizard progress |
| `location` | The current location in an environment | A highlighted spot in a flow or map |
| `date` | The current date in a calendar | Date picker |
| `time` | The current time in a timetable | Schedule |
| `true` | A generic current item | When no other value fits |
| `false` | Not current (same as omitting the attribute) | Rarely written by hand |
For site navigation, use page. Here is the primary navigation from the topic's example:
<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>Only the Home link has the attribute. The other links omit it. Do not write aria-current='false' on every other link. It is valid, but it adds noise and is easy to get wrong.
Styling from the attribute
You do not need an extra class such as .active. An attribute selector can read the same state that assistive technology reads. This keeps the visual state and the announced state tied to a single source of truth:
nav a[aria-current='page'] {
font-weight: 700;
text-decoration: underline;
}Bold weight and an underline are visible without relying on color. If you also want a color or a background change, add it on top.
nav a[aria-current='page'] {
font-weight: 700;
text-decoration: underline;
text-decoration-thickness: 3px;
text-underline-offset: 0.35em;
border-bottom: 3px solid currentColor;
}Two cautions about the visual design:
- Keep the current-page style distinct from the hover and focus styles. If all three look the same, a keyboard user cannot tell whether an item is current or merely focused.
- Check contrast. Any color you use to show the state must still meet the 4.5:1 contrast ratio for text, and non-text indicators such as a bar should meet 3:1 against the background.
Where to set the attribute
Something has to decide which link is current. There are three common approaches.
- Server-rendered pages. The template compares the link's URL to the request URL and prints the attribute for the match. This is the most reliable approach, because the correct HTML arrives in the first response.
- Framework routers. Many routers add the attribute automatically. React Router's
NavLink, for example, setsaria-current="page"on the active link. Check your framework's documentation and confirm in the browser's accessibility inspector. - Client-side script. A script compares
location.pathnameto each link and sets the attribute. This works, but the highlight appears only after the script runs, and it fails if JavaScript does not load.
If you must use a script, a minimal version looks like this:
const here = location.pathname.replace(/\/$/, '') || '/';
document.querySelectorAll('nav[aria-label="Primary"] a').forEach((link) => {
const target = new URL(link.href).pathname.replace(/\/$/, '') || '/';
if (target === here) {
link.setAttribute('aria-current', 'page');
} else {
link.removeAttribute('aria-current');
}
});The script strips a trailing slash from both sides so that /pricing and /pricing/ match. It falls back to / so the home page is not reduced to an empty string.
Edge cases
Section pages and child pages
Suppose the user is on /products/widgets, and the navigation contains only a "Products" link. The page is not the Products page itself, but it is inside that section. There are two defensible choices:
- Mark nothing, because the current page is not in the navigation. This is correct and simple.
- Mark "Products" to show the section. In this case use
aria-current='true'rather thanpage, because the link does not point to the current page.
Pick one rule and apply it everywhere. Mixing the two makes the navigation unpredictable.
The current item is a link to the same page
When the current page's link is still a working link, keep it as a link. Some sites remove the link or turn it into plain text. That removes an item from the tab order and can confuse users who expect the list to be consistent. Keeping the link and adding aria-current is the safer pattern.
Query strings and fragments
/products?sort=price and /products#reviews are the same page as /products for navigation purposes. Compare only the pathname, as the script above does. Whether /products?page=2 counts as current is a product decision. Treating it as the same page is usually right.
Multiple navigations on one page
A page can have a primary navigation and a footer navigation that both link to /support. If the user is on the support page, both links represent the current page, and both can carry aria-current='page'. Setting it in each navigation is consistent. Each navigation is its own set, so each can have its own current item.
Dropdowns and mega menus
When a submenu contains the current page, the trigger button is not a link and cannot be the current page. Mark the link inside the panel with aria-current='page'. Optionally add a visual cue on the trigger, such as an underline, while the panel is closed. That cue is visual only, since the button's accessible state already describes expansion, not location. The later chapters on disclosure buttons and mega menus build on this.
How the pieces connect
Verifying the result
Check the attribute in three ways:
- Inspect the link in the browser's developer tools and confirm that
aria-current='page'appears on exactly one link per navigation. More than one in the same list is a bug. - Open the accessibility pane in the developer tools and read the link's computed properties. The current state should appear there.
- Turn on a screen reader and move through the list. The current link should be announced with "current page" or an equivalent phrase.
Then test the visual side. Zoom to 200%, switch the display to grayscale, and confirm that you can still tell which item is current.
Common mistakes
- Using a CSS class like
.activeand noaria-current. Sighted users get the cue and screen reader users do not. - Using
aria-selectedon navigation links. That attribute belongs to widgets such as tabs and listboxes, not to site navigation. - Setting
aria-current='page'on a link that does not point to the current page. - Leaving the attribute on after a client-side route change. In a single-page app, update it on every navigation.
- Showing the current page with color alone.
Exercises
Exercise
A developer marks the current page with a class active that turns the link blue. The markup has no ARIA attributes. Name two problems and give the fix.
Show answer
First, screen reader users get no indication of the current page because the class is not exposed to assistive technology. Second, color alone is a weak cue for users with color-vision deficiency or low vision (WCAG 1.4.1). The fix is to add aria-current='page' to the matching link and style it with the selector nav a[aria-current='page'] using a non-color cue such as bold weight and an underline.
Exercise
The user is on /products/widgets. The navigation has a link to /products but no link to /products/widgets. Which aria-current value, if any, can you put on the Products link, and why not page?
Show answer
You can use aria-current='true' if you want to show that the user is in the Products section, or omit the attribute. You should not use page, because that value means the link points to the current page, and the Products link points to a different page.
Exercise
Write a CSS rule that styles the current-page link in the primary navigation with a bold weight and a 3px bottom border in the text color, without using any extra class.
Show answer
nav[aria-label='Primary'] a[aria-current='page'] { font-weight: 700; border-bottom: 3px solid currentColor; }