← Back to HTML & CSS, page 10

html-css-0192 · Easy fresher

How do you mark and style the current page in a navigation menu so that everyone can tell where they are?

Mark the link of the current page with aria-current="page" and style that attribute with more than colour. It works for assistive technology and keeps the style tied to the meaning. Set it on the server if possible, and keep one current link in a list.

Full answer

Users need to know which page they are on. Two things are needed: a visual sign and a sign for assistive technology.

For the meaning, use aria-current. The value page marks the link that points to the current page. A screen reader announces it as "current page". Other values exist: step for a step in a process, location for a place in a map or flow, date and time, and true as a general value. Do not use the current class alone, because a class has no meaning for a screen reader.

For the look, style the attribute: a[aria-current="page"] { ... }. This keeps the style and the meaning in one place, so they cannot get out of step. Use more than colour alone to show the state, such as a bold weight, an underline or a border. Colour alone fails for users with colour vision problems.

Other points:

  • Put the menu in a nav element, with a label such as aria-label="Main" if the page has more than one nav.
  • The current page can still be a link. Some sites remove the link, but a working link is simple and predictable.
  • Set the attribute on the server when the HTML is built. This works without JavaScript and is ready on first paint. A script can do it for static sites, by comparing a.pathname with location.pathname, and it must also run on client-side navigation.
  • Only one link in a list should have aria-current="page". For a section with sub-pages, mark the current page, and style the parent as the active section with a different class.

The script marks the link whose path matches the current page. The CSS then shows it in bold with a bottom border. A screen reader announces the link as the current page. The script is a fallback for static files, and a server-built menu is better when you have one.

Test with a screen reader to hear how it announces the current link.

html
<style>
  nav a { color: #234; text-decoration: none; padding: 4px 8px; display: inline-block; }
  nav a[aria-current="page"] { font-weight: 700; border-bottom: 2px solid currentcolor; }
</style>
<nav aria-label="Main">
  <ul>
    <li><a id="home" href="/index.html">Home</a></li>
    <li><a id="courses" href="/courses">Courses</a></li>
  </ul>
</nav>
<script>
  for (const a of document.querySelectorAll("nav a")) {
    if (a.pathname === location.pathname) a.setAttribute("aria-current", "page");
  }
</script>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • aria-current="page" marks the current link.
  • Style a[aria-current] so meaning and look stay together.
  • Use more than colour to show the state.
  • Put the menu in a labelled nav.
  • Set it on the server when possible.

Common mistakes

  • Using only a CSS class, so assistive technology gets no information.
  • Showing the current page by colour alone.
  • Marking several links as the current page.

Follow-ups

  • What is the difference between aria-current="page" and aria-selected?
  • Why add a text sign besides colour?

Tips

  • Say that meaning and style come from the same attribute.
  • Name the values: page, step, location.