🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Components & Islands••By Komal Nakrani

Astro Active Link: Highlight the Current Nav Item

Highlight the active nav link in Astro with Astro.url.pathname: set aria-current=page, match section paths, handle trailing slashes and style it with CSS.

#Astro#Astro Active Link#Navigation#Components

To highlight the active link in Astro, compare each link’s href with Astro.url.pathname inside your navigation component and add aria-current="page" to the match. Style [aria-current='page'] in CSS. Because this runs at build time, every static page ships with the right link already marked, and no JavaScript is needed.

Key Takeaways

  • Astro.url.pathname is the current page’s path, available in any component.
  • Use aria-current="page" for the exact page; it’s both the accessible marker and a CSS hook.
  • Match sections with startsWith, so /blog/my-post/ keeps “Blog” highlighted.
  • Normalize trailing slashes so /blog and /blog/ compare equal.
---
// src/components/NavLink.astro
import type { HTMLAttributes } from 'astro/types';

type Props = HTMLAttributes<'a'> & { section?: boolean };

const { href, section = false, ...rest } = Astro.props;
const strip = (p: string) => (p.length > 1 ? p.replace(/\/$/, '') : p);

const current = strip(Astro.url.pathname);
const target = strip(String(href));
const isExact = current === target;
const isInSection = section && target !== '/' && current.startsWith(`${target}/`);
---
<a
  href={href}
  aria-current={isExact ? 'page' : isInSection ? 'true' : undefined}
  class:list={['nav-link', { active: isExact || isInSection }]}
  {...rest}
>
  <slot />
</a>

Use it in your header:

---
// src/components/Header.astro
import NavLink from './NavLink.astro';
---
<nav aria-label="Main">
  <NavLink href="/">Home</NavLink>
  <NavLink href="/blog/" section>Blog</NavLink>
  <NavLink href="/about/">About</NavLink>
  <NavLink href="/contact/">Contact</NavLink>
</nav>

On /blog/, “Blog” gets aria-current="page". On /blog/some-post/, it gets aria-current="true", which tells assistive technology the link is the current section without claiming it’s this exact page. Home is excluded from section matching, since every path starts with /.

Style it

/* src/styles/global.css */
.nav-link { text-decoration: none; padding: 0.5rem 0.75rem; border-radius: 0.375rem; }
.nav-link:hover { background: var(--color-surface-hover, #f3f4f6); }
.nav-link.active { font-weight: 600; box-shadow: inset 0 -2px currentColor; }

You could style [aria-current] directly instead of a class; the class just makes Tailwind or utility CSS easier. With Tailwind, class:list can apply utilities conditionally: { 'font-semibold underline': isExact || isInSection }.

Trailing slashes and base paths

Whether your URLs end in / depends on the trailingSlash and build.format settings, and links in content are written by hand. The strip() helper removes one trailing slash from both sides so the comparison works either way. See the trailing slash tip for choosing one format site-wide.

If the site is deployed under a base path (for example GitHub Pages at /repo/), Astro.url.pathname includes that base. Build links with import.meta.env.BASE_URL so both sides include it.

On-demand pages and view transitions

On server-rendered pages, Astro.url reflects the request, so the same component works unchanged.

With the <ClientRouter /> for view transitions, the header is still re-rendered on each navigation, because every page’s HTML comes from the server. The only exception is an element you persist with transition:persist: its markup isn’t swapped, so the active state stays stale. Don’t persist the nav, or update aria-current in an astro:page-load listener. Our view transitions guide explains persistence. For a matching mobile menu, see the mobile nav component, and the Pages & Routing docs cover how paths map to files. If you start from one of the Komal themes, add NavLink to its header component and swap the plain links for it.

FAQ

How do I get the current URL in an Astro component?

Use Astro.url, a URL object for the current page. Astro.url.pathname gives the path, such as /blog/my-post/. It’s available in pages, layouts and components rendered on the server or at build time.

Use aria-current="page" for accessibility, since it tells screen reader users which link is the current page. A class is optional styling convenience; you can also style [aria-current='page'] directly.

The paths usually differ by a trailing slash, a base path or letter case. Log Astro.url.pathname and the link’s href, then normalize both, for example by removing a trailing slash before comparing.