🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

CSS Navigation: Navbars, Sidebars, Breadcrumbs, and Tabs

Web StylesđŸŸĸ Free Lesson

CSS Navigation: Navbars, Sidebars, Breadcrumbs, and Tabs

Navigation is the one component every site ships and few sites get right. The failure modes are boring and predictable: a navbar that overflows at 900px, a sidebar that scrolls the whole page away with it, a mobile menu that traps keyboard focus, breadcrumb separators hacked into the DOM for no reason. Most of these are CSS problems wearing JavaScript costumes. This lesson covers the five patterns you will actually build — top navbar, sidebar, breadcrumb, tabs, and the collapsing mobile menu — plus the CSS decisions that separate a navbar that lasts three redesigns from one that breaks every time a product manager adds a link.

Match the pattern to the job

Before writing selectors, pick the pattern that fits the information architecture. The wrong pattern forces you into increasingly hostile CSS later.

PatternBest forCore CSS toolsClassic failure
Top navbar5–7 top-level destinationsFlexbox, position: stickyLinks overflow at tablet widths
SidebarDocs, dashboards, deep hierarchiesSticky column, overflow-y: autoSidebar scrolls off-screen with content
BreadcrumbHierarchical content, e-commerceOrdered list, ::before separatorsMore than five levels deep
TabsSwitching sibling views in placeButtons plus radio or :target hacksCSS-only versions break keyboard flow
PaginationLong lists, archivesFlex, aria-current for stateHit targets under 44 pixels

A marketing site almost always needs a navbar and breadcrumbs. A documentation site almost always needs a sidebar and breadcrumbs. Tabs are for switching views of the same entity — a product page's "Reviews / Specs / Q&A" — not for navigating between pages. If tab clicks change the URL to a different document, use links styled as tabs, not a tab widget.

A navbar that survives real content

The classic mistake is fixed pixel widths on the logo and nav list. Real content changes: long product names, new links, German compound nouns. Build the bar so wrapping is graceful rather than catastrophic.

.navbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem 2rem;
  padding: 0.75rem 1.5rem;
  background: #ffffff;
  border-bottom: 1px solid #e2e8f0;
}

.navbar__brand {
  font-size: 1.25rem;
  font-weight: 700;
  color: #0f172a;
  text-decoration: none;
  margin-right: auto;
}

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

.navbar__link {
  display: inline-block;
  padding: 0.375rem 0;
  color: #334155;
  text-decoration: none;
  font-weight: 500;
}

.navbar__link:hover,
.navbar__link:focus-visible {
  color: #0f172a;
}

.navbar__link[aria-current="page"] {
  color: #0f172a;
  box-shadow: inset 0 -2px 0 #2563eb;
}

@media (max-width: 640px) {
  .navbar {
    gap: 0.5rem 1rem;
    padding: 0.5rem 1rem;
  }
}

Three details carry this snippet. flex-wrap: wrap means an extra link pushes to a second row instead of clipping. margin-right: auto on the brand pins links to the right without a spacer element. aria-current="page" marks the active link in a way screen readers understand — styling :active (which means "being clicked") is a different, wrong state.

Sticky navigation also needs a stacking-context audit. position: sticky with a z-index creates a stacking context, so dropdown menus, search overlays, and cookie banners all need to sit above the bar's layer or they will be sliced through by it. Pick three tiers and stick to them: page content at the default level, the navbar in the middle tier, and modals or command palettes in the top tier. Mixing z-index: 9999 on every new component is how teams end up with a "nothing works anymore" afternoon.

Sidebar menus that stay put

Sidebars fail when they are not tall enough to stick or too tall to scroll. The fix is a sidebar container sized to the viewport with its own internal scroll, while the page scrolls normally.

.layout-with-sidebar {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  gap: 2rem;
  align-items: start;
}

.sidebar {
  position: sticky;
  top: 5rem;
  max-height: calc(100vh - 6rem);
  overflow-y: auto;
  padding-right: 0.5rem;
}

.sidebar__group {
  margin-bottom: 1.5rem;
}

.sidebar__heading {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #64748b;
  margin: 0 0 0.5rem;
}

.sidebar__link {
  display: block;
  padding: 0.5rem 0.75rem;
  border-left: 3px solid transparent;
  border-radius: 0 6px 6px 0;
  color: #475569;
  text-decoration: none;
}

.sidebar__link:hover {
  background: #f1f5f9;
}

.sidebar__link.is-active {
  border-left-color: #2563eb;
  background: #eff6ff;
  color: #1d4ed8;
  font-weight: 600;
}

minmax(0, 1fr) on the content column is deliberate: without the 0 minimum, long code blocks or URLs inflate the column and push the sidebar off-screen — the same trap as flex items refusing to shrink. On narrow viewports, collapse the grid to a single column and let the sidebar sit above the content or move into your mobile menu.

Breadcrumbs without JavaScript

Breadcrumbs are a hierarchy, so use an ordered list. Separators belong in CSS via generated content, not in the markup.

.breadcrumb {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  margin: 0 0 1.5rem;
  padding: 0;
  list-style: none;
  font-size: 0.875rem;
}

.breadcrumb__item + .breadcrumb__item::before {
  content: "/";
  margin-right: 0.5rem;
  color: #94a3b8;
}

.breadcrumb__link {
  color: #2563eb;
  text-decoration: none;
}

.breadcrumb__link:hover {
  text-decoration: underline;
}

.breadcrumb__item[aria-current="page"] {
  color: #64748b;
  font-weight: 500;
}

Keep the last item as plain text with aria-current="page" — it is the current location, not a link. And cap visible depth: if users need more than four hops to understand where they are, the site hierarchy itself needs work, not more breadcrumbs.

Tabs: where pure CSS hits its limits

CSS-only tabs via hidden radio buttons or :target look clever in demos and hurt real users. Radio-group keyboard behavior is arrow-key navigation between radios, which does not match the expected Tab-then-arrows pattern of a proper tablist, and :target navigation moves focus nowhere useful. The durable approach is buttons styled by CSS, with state managed by a few lines of JavaScript and ARIA.

.tablist {
  display: flex;
  gap: 0.25rem;
  border-bottom: 2px solid #e2e8f0;
}

.tab {
  padding: 0.75rem 1.25rem;
  border: none;
  background: none;
  color: #64748b;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
}

.tab:hover {
  color: #0f172a;
}

.tab[aria-selected="true"] {
  color: #1d4ed8;
  border-bottom-color: #2563eb;
}

.tab:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 2px;
  border-radius: 4px;
}

.tabpanel {
  padding: 1.25rem 0;
}

The CSS owns appearance: selected state, hover, focus ring. The JavaScript owns behavior: toggling aria-selected, moving focus with arrow keys, showing the matching panel. Splitting responsibilities this way keeps the styling replaceable and the interaction correct.

The mobile menu decision

When links no longer fit, you have three realistic options. A pure CSS checkbox or :checked toggle works for tiny menus but cannot trap focus, lock body scroll, or close on Escape — all things a full-screen overlay needs. A native details and summary disclosure gives you free keyboard support and works without script, but styling the expanded panel as a fixed overlay takes care, and animating height on details is still awkward. A small JavaScript controller — button with aria-expanded, focus moved into the menu on open, returned to the button on close — is the right default for anything larger than five links.

Whatever you choose, do not use display: none on the toggle button at desktop widths if it is the thing that receives focus on load; hide the whole menu system instead, and make sure the close control is the first focusable item inside the open menu.

One more mobile detail: scroll locking. When a full-screen menu is open, the page behind it should not scroll on touch devices — a one-line overflow: hidden on body handles most cases, but iOS Safari needs the height: 100vh workaround or the scroll position jumps on close. And when the menu closes, return focus to the toggle button. Keyboard and screen-reader users who opened the menu, tabbed through six links, and pressed Escape should not be dumped back at the top of the document with no idea where they are.

Active states done right

Three pseudo-classes get confused constantly. :hover means the pointer is over the link. :active means the primary button is pressed down — it is the momentary click state, not "the current page". :focus-visible means keyboard (or script-assisted) focus deserves a visible ring. None of them tells you which page the user is on. That state lives in the URL, so reflect it with an aria-current attribute or an active class your router applies, and style that instead.

Common Mistakes

Hiding desktop links with display: none and revealing them inside a JS menu without moving focus, so keyboard users tab through invisible links.

Fix: Remove hidden links from the tab order entirely (display: none does this) and manage focus explicitly when the menu opens and closes.

Styling :active as the selected page, so the highlight only appears while the mouse button is held down.

Fix: Drive the selected style from aria-current or an .is-active class applied by your router.

Fixed-width nav containers that clip long link lists instead of wrapping or scrolling.

Fix: Use flex-wrap for short bars and overflow-x: auto with hidden scrollbars for long link rows.

Building breadcrumbs from nested divs with a â€ē character typed into the markup.

Fix: Use an ordered list and generate separators with the ::before pseudo-element so the visual order can never drift from the DOM order.

Best Practices

  • Mark the current page with aria-current="page" — it conveys state to assistive technology and gives you a styling hook for free.
  • Give every interactive nav control a visible :focus-visible ring with at least 2px outline and a 2px offset.
  • Size hit areas to at least 44 by 44 pixels on touch layouts; padding is cheaper than JavaScript hit-testing.
  • Sticky navbars need a z-index above page content but below modals, and a border or shadow so content scrolling underneath stays legible.
  • Test navigation at 320px, 768px, and 1024px widths with keyboard only before shipping.

Key Takeaways

  • Pick the navigation pattern from the information architecture first; CSS cannot rescue the wrong structure.
  • Flexbox with wrapping handles navbar overflow gracefully; sticky plus internal overflow keeps sidebars usable.
  • Breadcrumbs are an ordered list with CSS-generated separators and a non-link final item carrying aria-current.
  • CSS-only tabs break expected keyboard behavior — style tabs with CSS, control state with JavaScript and ARIA.
  • The three click-state pseudo-classes (hover, active, focus-visible) never indicate the current page; aria-current does.
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-10-12

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert Web Development Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement