Astro is a static site generator that allows you to build fast, SEO-friendly websites. This article explains how to implement breadcrumb navigation using Astro.
What are breadcrumbs?
Breadcrumbs are a website navigation element consisting of a list of links that indicate the hierarchical structure of the current page. Typically, they display the path from the home page to the current page, making it easier for users to navigate the site.
Steps to implement breadcrumbs in Astro
- Create a
Breadcrumb.astrofile in thesrc/componentsdirectory. - Add the following code to the
Breadcrumb.astrofile:--- const { currentPage } = Astro.props; const pages = [ { name: 'Home', href: '/' }, { name: 'Category', href: '/category' }, { name: currentPage, href: '#' }, ]; --- <nav> <ol> {pages.map((page, index) => ( <li> {index === pages.length - 1 ? ( <span>{page.name}</span> ) : ( <a href={page.href}>{page.name}</a> )} </li> ))} </ol> </nav> - In the
Astrofile of the page where you want to display breadcrumbs, import theBreadcrumbcomponent and pass the current page name to thecurrentPageproperty.--- import Breadcrumb from '../components/Breadcrumb.astro'; --- <Breadcrumb currentPage="Current Page" /> - Style the breadcrumbs using CSS.
nav ol { display: flex; list-style: none; padding: 0; } nav li:not(:last-child)::after { content: '>'; margin: 0 0.5rem; }
With the steps above, you can implement breadcrumbs using Astro.
Explanation
- In the
Breadcrumb.astrofile,currentPageis retrieved fromAstro.props, and the breadcrumb hierarchy is defined in thepagesarray. - Map over the
pagesarray to display the links and page names. Because the last element is the current page, it is displayed as aspantag rather than a link. - On pages where you want to display breadcrumbs, import the
Breadcrumbcomponent and pass the current page name to thecurrentPageproperty. - In the CSS,
display: flexis used to arrange the list items horizontally, and the::afterpseudo-element adds a separator (>).
Introducing breadcrumbs makes it easier for users to grasp their current location within a site, improving navigational usability. With Astro, breadcrumbs can be implemented as a simple component, making it easy to add them while maintaining a consistent design across the entire site.







