DEV Community

RAMO
RAMO

Posted on

Breadcrumbs on a product page: the markup, the accessibility bits, and the structured data

Breadcrumbs are one of those components nobody argues about. Everyone adds them, they look like a row of small grey links, and they get forgotten. But on a store with deep categories they do three separate jobs, and it's easy to get one of them wrong:

  1. They help a visitor who landed from search figure out where they are.
  2. They help screen reader users jump up a level without hunting through the menu.
  3. They give search engines a clear picture of how your pages are related.

Here's a version that handles all three without much code.

Start with plain HTML

<nav aria-label="Breadcrumb">
  <ol class="breadcrumbs">
    <li><a href="/">Home</a></li>
    <li><a href="/categories/electrical">Electrical</a></li>
    <li><a href="/categories/measuring-instruments">Measuring instruments</a></li>
    <li><a href="/products/digital-multimeter" aria-current="page">Digital multimeter</a></li>
  </ol>
</nav>
Enter fullscreen mode Exit fullscreen mode

A few choices in there are deliberate:

  • <nav> with an aria-label. A page usually has several nav regions already (header, footer). The label is what makes this one show up as "Breadcrumb navigation" in a screen reader's landmarks list instead of a third anonymous "navigation".
  • An ordered list. The order means something, so <ol> says so. Screen readers also announce "list, 4 items", which tells the user how deep they are before they start.
  • aria-current="page" on the last item. This is the part most implementations skip. It tells assistive tech which item is the current page. Whether the last item is a link or plain text is a design choice; both are fine as long as it's marked.

Keep the separators out of the content

The most common mistake I see is putting the separator in the HTML:

<li><a href="/">Home</a> &gt; </li>
Enter fullscreen mode Exit fullscreen mode

A screen reader then reads "Home, greater than, Electrical, greater than...". Draw the separators with CSS instead, so they're visual only:

.breadcrumbs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  list-style: none;
  padding: 0;
}

.breadcrumbs li + li::before {
  content: "/";
  margin-inline-end: 0.25rem;
  color: #888;
}
Enter fullscreen mode Exit fullscreen mode

Using margin-inline-end instead of margin-right means the spacing still works if the page is ever shown right-to-left. On an RTL page you'll probably also want the separator to point the other way if you use an arrow-like character, which is another reason to keep it in CSS where you can change it per direction.

Make it usable on a phone

Long category names wrap badly on small screens. Two options that both work:

  • Let it wrap (the flex-wrap above) and keep the font small but readable.
  • Or show only the parent level on mobile ("‹ Measuring instruments") as a back link.

What I'd avoid is horizontal scrolling with the current page cut off on the right. It looks tidy, but the visitor loses the one item that tells them where they are.

Also check the tap targets. Breadcrumb links tend to be small text with tiny gaps between them, which is exactly where people mis-tap. A bit of vertical padding on the links fixes most of it.

Add BreadcrumbList structured data

The visible breadcrumb helps people. For search engines you can describe the same trail with BreadcrumbList in JSON-LD:

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [
    { "@type": "ListItem", "position": 1, "name": "Home",
      "item": "https://www.example.com/" },
    { "@type": "ListItem", "position": 2, "name": "Electrical",
      "item": "https://www.example.com/categories/electrical" },
    { "@type": "ListItem", "position": 3, "name": "Measuring instruments",
      "item": "https://www.example.com/categories/measuring-instruments" },
    { "@type": "ListItem", "position": 4, "name": "Digital multimeter" }
  ]
}
</script>
Enter fullscreen mode Exit fullscreen mode

Things worth knowing:

  • Google's documentation allows the last item to omit item, since it's the current page.
  • Use absolute URLs, and the same canonical URLs you use everywhere else. A breadcrumb pointing at a URL with tracking parameters or a different trailing slash is just noise.
  • The JSON-LD should match what's on screen. If the visible trail says "Measuring instruments" and the structured data says something else, you've built two breadcrumbs and you'll eventually fix only one of them.

The easiest way to keep them in sync is to build both from the same array.

One source, two outputs

In React (or anything component-based) that looks something like this:

function Breadcrumbs({ trail, baseUrl }) {
  const jsonLd = {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    itemListElement: trail.map((crumb, i) => ({
      "@type": "ListItem",
      position: i + 1,
      name: crumb.name,
      ...(i < trail.length - 1 && { item: baseUrl + crumb.href }),
    })),
  };

  return (
    <>
      <nav aria-label="Breadcrumb">
        <ol className="breadcrumbs">
          {trail.map((crumb, i) => {
            const isLast = i === trail.length - 1;
            return (
              <li key={crumb.href}>
                <a href={crumb.href} aria-current={isLast ? "page" : undefined}>
                  {crumb.name}
                </a>
              </li>
            );
          })}
        </ol>
      </nav>
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
      />
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

Make sure this renders on the server, not after hydration. Crawlers that don't run JavaScript, and most link preview bots, only see what's in the initial HTML.

Products in more than one category

On a hardware store a product often lives in two places. A multimeter is a measuring instrument and also an electrician's tool. You don't need to show every path. Pick one primary category per product and use it consistently for the breadcrumb, the structured data and internal links. If someone arrived through the other category, the breadcrumb still showing the primary path is fine; it's better than a trail that changes depending on how you got there and confuses everyone, including crawlers.

A quick checklist

  • <nav aria-label="Breadcrumb"> with an <ol> inside
  • aria-current="page" on the last item
  • Separators drawn in CSS, not typed into the HTML
  • Wraps cleanly on mobile, links big enough to tap
  • BreadcrumbList JSON-LD with absolute canonical URLs
  • Visible trail and JSON-LD generated from the same data
  • Rendered on the server
  • One primary category per product

Then run the page through Google's Rich Results Test and tab through it with a screen reader once. Ten minutes, and you'll know whether all three jobs are actually being done.

I've been going through this on the product pages at RAMO, a hardware store where something like a digital multimeter sits a few levels deep under electrical measuring instruments, so the breadcrumb is often the fastest way back to the category.

Top comments (0)