--- url: "https://web.dev/articles/building/a-breadcrumbs-component" titulek: "Building a breadcrumbs component | Articles | web.dev" h1: "Building a breadcrumbs component Stay organized with collections Save and categorize content based on your preferences." popis: "A foundational overview of how to build a responsive and accessible breadcrumbs component for users to navigate your site." jazyk: "en" kanonicka: "https://web.dev/articles/building/a-breadcrumbs-component" drobecky: ["Articles", "Building a breadcrumbs component"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 22:27:46" znaku: 13187 slov: 2026 kodovani: "iso-8859-2" sha256: "e07faf1293b6d2bd08b75ec2178abafd156b6d7d22d7baf2ea4bc9459e31d2cf" --- Building a breadcrumbs component Stay organized with collections Save and categorize content based on your preferences. A foundational overview of how to build a responsive and accessible breadcrumbs component for users to navigate your site. Adam Argyle In this post I want to share thinking on a way to build breadcrumb components. [Try the demo](https://gui-challenges.web.app/breadcrumbs/dist/). [Demo](https://gui-challenges.web.app/breadcrumbs/dist/) If you prefer video, here's a YouTube version of this post: Overview A [breadcrumbs](https://codepen.io/search/pens?q=breadcrumbs) component shows where in the site hierarchy the user is. The name is from [Hansel and Gretel](https://en.wikipedia.org/wiki/Hansel_and_Gretel), who dropped breadcrumbs behind them in some dark woods and were able to find their way home by tracing crumbs backwards. The breadcrumbs in this post are not [standard breadcrumbs](https://www.w3.org/TR/wai-aria-practices-1.1/examples/breadcrumb/index.html), they're breadcrumb-like. They offer additional functionality by putting sibling pages right into the navigation with a ` ` A link and some options is nothing special but adds more functionality to a simple breadcrumb. Adding a `title` to the `` element. `.crumbicon { --crumbicon-size: 3ch; display: grid; grid: [stack] var(--crumbicon-size) / [stack] var(--crumbicon-size); place-items: center; & > * { grid-area: stack; } }` The `` element while maintaining the built-in functionality. `.disguised-select { inline-size: 100%; block-size: 100%; opacity: .01; font-size: min(100%, 16px); /* Defaults to 16px; fixes iOS zoom */ }` Overflow Breadcrumbs should be able to represent a very long trail. I'm a fan of allowing things to go offscreen horizontally, when appropriate, and I felt this breadcrumbs component qualified well. `.breadcrumbs { overflow-x: auto; overscroll-behavior-x: contain; scroll-snap-type: x proximity; scroll-padding-inline: calc(var(--nav-gap) / 2); & > .crumb:last-of-type { scroll-snap-align: end; } @supports (-webkit-hyphens:none) { & { scroll-snap-type: none; }} }` The overflow styles set up the following UX: Horizontal scroll with overscroll containment. Horizontal scroll padding. One snap point on the last crumb. This means that on page load the first crumb loads snapped and in view. Removes the snap point from Safari, which struggles with the horizontal scrolling and snap effect combinations. Media queries One subtle adjustment for smaller viewports is to hide the "Home" label, leaving just the icon: `@media (width <= 480px) { .breadcrumbs .home-label { display: none; } }` Accessibility Motion There isn't a whole lot of motion in this component, but by wrapping the transition in a `prefers-reduced-motion` check, we can prevent unwanted motion. `@media (prefers-reduced-motion: no-preference) { .crumbicon { transition: box-shadow .2s ease; } }` None of the other styles need to change, the hover and focus effects are great and meaningful without a `transition`, but if motion is okay, we'll add a subtle transition to the interaction. JavaScript First, regardless of the type of router you use in your site or application, when a user changes the breadcrumbs, the URL needs to be updated and the user shown the appropriate page. Second, to normalize the user experience, make sure no unexpected navigations happen when users are just browsing `` change event firing prevention. The eager event prevention is needed due to the use of a `` changed event `const crumbs = document.querySelectorAll('.breadcrumbs select') const allowedKeys = new Set(['Tab', 'Enter', ' ']) const preventedKeys = new Set(['ArrowUp', 'ArrowDown']) // watch crumbs for changes, // ensures it's a full value change, not a user exploring options via keyboard crumbs.forEach(nav => { let ignoreChange = false nav.addEventListener('change', e => { if (ignoreChange) return // it's actually changed! }) nav.addEventListener('keydown', ({ key }) => { if (preventedKeys.has(key)) ignoreChange = true else if (allowedKeys.has(key)) ignoreChange = false }) })` The strategy for this is to watch for keyboard down events on each `` element fires. Conclusion Now that you know how I did it, how would you‽ 🙂 Let's diversify our approaches and learn all the ways to build on the web. Create a demo, [tweet me](https://twitter.com/argyleink) links, and I'll add it to the community remixes section below! Community remixes [Tux Solbakk](https://github.com/tux-/) as a web component: [demo and code](https://tux-.github.io/breadcrumbs/public/index.html) Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2021-07-14 UTC.