Navbar
The header of a website: logo, links with dropdowns, and buttons. One piece of markup works on every screen: when there isn't room, the links fold into a panel that slides in from the side, opened by a ☰ button.
Navbar
Wide enough (768px of its own width, not the screen's), everything sits in one row. The current page's link gets aria-current="page" and an underline.
<header class="navbar">
<div class="navbar-inner">
<a class="navbar-brand" href="#"><span class="navbar-mark"><i class="mi mi-letter-m mi-stroke-2.5" aria-hidden="true"></i></span>Momo</a>
<button class="navbar-toggle" type="button" aria-label="Menu"><i class="mi mi-menu" aria-hidden="true"></i><i class="mi mi-x" aria-hidden="true"></i></button>
<div class="navbar-menu">
<nav class="navbar-nav" aria-label="Main">
<a class="navbar-link" href="#" aria-current="page">Home</a>
<details class="navbar-dropdown">
<summary class="navbar-link">Product <i class="mi mi-chevron-down navbar-caret" aria-hidden="true"></i></summary>
<div class="navbar-dropdown-menu">
<a href="#"><i class="mi mi-layout-dashboard" aria-hidden="true"></i><span><b>Dashboards</b><small>Stats, charts and tables</small></span></a>
<a href="#"><i class="mi mi-layout-kanban" aria-hidden="true"></i><span><b>Boards</b><small>Kanban with drag and drop</small></span></a>
<a href="#"><i class="mi mi-calendar" aria-hidden="true"></i><span><b>Calendar</b><small>Months, weeks and ranges</small></span></a>
</div>
</details>
<a class="navbar-link" href="#">Pricing</a>
<a class="navbar-link" href="#">Blog</a>
</nav>
<div class="navbar-actions">
<a class="btn btn-ghost" href="#">Sign in</a>
<a class="btn btn-brand" href="#">Get started</a>
</div>
</div>
</div>
</header>On a phone
Here the same navbar sits in a narrow box, so it folds. Press ☰: the panel slides in, Esc or a tap outside closes it, and so does picking a link. Dropdowns open in place inside the panel.
<div class="max-w-sm mx-auto box p-0 overflow-hidden">
<header class="navbar">
<div class="navbar-inner">
<a class="navbar-brand" href="#"><span class="navbar-mark"><i class="mi mi-letter-m mi-stroke-2.5" aria-hidden="true"></i></span>Momo</a>
<button class="navbar-toggle" type="button" aria-label="Menu"><i class="mi mi-menu" aria-hidden="true"></i><i class="mi mi-x" aria-hidden="true"></i></button>
<div class="navbar-menu">
<div class="navbar-menu-head"><b>Menu</b><button class="btn btn-icon btn-sm btn-ghost" type="button" data-momo-close aria-label="Close menu"><i class="mi mi-x" aria-hidden="true"></i></button></div>
<nav class="navbar-nav" aria-label="Main (phone)">
<a class="navbar-link" href="#" aria-current="page">Home</a>
<details class="navbar-dropdown">
<summary class="navbar-link">Product <i class="mi mi-chevron-down navbar-caret" aria-hidden="true"></i></summary>
<div class="navbar-dropdown-menu">
<a href="#"><i class="mi mi-layout-dashboard" aria-hidden="true"></i><span><b>Dashboards</b><small>Stats, charts and tables</small></span></a>
<a href="#"><i class="mi mi-layout-kanban" aria-hidden="true"></i><span><b>Boards</b><small>Kanban with drag and drop</small></span></a>
</div>
</details>
<a class="navbar-link" href="#">Pricing</a>
<a class="navbar-link" href="#">Blog</a>
</nav>
<div class="navbar-actions">
<a class="btn btn-ghost" href="#">Sign in</a>
<a class="btn btn-brand" href="#">Get started</a>
</div>
</div>
</div>
</header>
<div class="p-6 text-sm text-muted">Page content…</div>
</div>Give the panel a .navbar-menu-head: a top row with a title and a close button (data-momo-close). The panel covers the ☰ button, so this is the visible way to close it on a phone. The row is hidden when the navbar is wide. A tap outside the panel only closes it; it never clicks the page underneath.
Floating
.navbar-float turns the navbar into a card: a gap around it, rounded corners and a line all the way round. With .navbar-sticky it stays floating at the top while the page scrolls, and its mega menus float under it at the same width. Add .shadow or .shadow-md for a hard shadow.
<header class="navbar navbar-float">
<div class="navbar-inner">
<a class="navbar-brand" href="#"><span class="navbar-mark"><i class="mi mi-letter-m mi-stroke-2.5" aria-hidden="true"></i></span>Momo</a>
<button class="navbar-toggle" type="button" aria-label="Menu"><i class="mi mi-menu" aria-hidden="true"></i><i class="mi mi-x" aria-hidden="true"></i></button>
<div class="navbar-menu">
<div class="navbar-menu-head"><b>Menu</b><button class="btn btn-icon btn-sm btn-ghost" type="button" data-momo-close aria-label="Close menu"><i class="mi mi-x" aria-hidden="true"></i></button></div>
<nav class="navbar-nav" aria-label="Main (floating)">
<a class="navbar-link" href="#" aria-current="page">Home</a>
<a class="navbar-link" href="#">Features</a>
<a class="navbar-link" href="#">Pricing</a>
</nav>
<div class="navbar-actions"><a class="btn btn-brand" href="#">Get started</a></div>
</div>
</div>
</header>
<header class="navbar navbar-float navbar-ink navbar-center shadow-md mt-6">
<div class="navbar-inner">
<a class="navbar-brand" href="#"><span class="navbar-mark"><i class="mi mi-letter-m mi-stroke-2.5" aria-hidden="true"></i></span>Momo</a>
<button class="navbar-toggle" type="button" aria-label="Menu"><i class="mi mi-menu" aria-hidden="true"></i><i class="mi mi-x" aria-hidden="true"></i></button>
<div class="navbar-menu">
<div class="navbar-menu-head"><b>Menu</b><button class="btn btn-icon btn-sm btn-ghost" type="button" data-momo-close aria-label="Close menu"><i class="mi mi-x" aria-hidden="true"></i></button></div>
<nav class="navbar-nav" aria-label="Main (floating, dark)">
<a class="navbar-link" href="#">Work</a>
<a class="navbar-link" href="#" aria-current="page">About</a>
<a class="navbar-link" href="#">Contact</a>
</nav>
<div class="navbar-actions"><a class="btn btn-sm btn-brand" href="#">Hire me</a></div>
</div>
</div>
</header>Change the gap, the widest size and the corners with --navbar-float-gap (12px), --navbar-float-max (1216px) and --navbar-float-radius.
Sticky, see-through, hiding
See them on the landing example (it uses .navbar-sticky):
| Class | Does |
|---|---|
.navbar-sticky | Stays at the top while the page scrolls; gets .is-scrolled once the page has moved. Anchor links (#features) land below it. |
.navbar-transparent | See-through at the top of the page (over a hero), solid once scrolled |
.navbar-autohide | Slides away while scrolling down, comes back when scrolling up |
.navbar-fixed | Fixed at the top over the page: give the page padding-top: var(--momo-navbar-h) |
Momo publishes the height of a sticky or fixed navbar as --momo-navbar-h on <html>. A full-screen hero under it uses it: .hero-full { --hero-offset: var(--momo-navbar-h); }.
Looks and folding
<header class="navbar navbar-ink navbar-center">
<div class="navbar-inner">
<a class="navbar-brand" href="#"><span class="navbar-mark"><i class="mi mi-letter-m mi-stroke-2.5" aria-hidden="true"></i></span>Momo</a>
<button class="navbar-toggle" type="button" aria-label="Menu"><i class="mi mi-menu" aria-hidden="true"></i><i class="mi mi-x" aria-hidden="true"></i></button>
<div class="navbar-menu">
<nav class="navbar-nav" aria-label="Main (ink)">
<a class="navbar-link" href="#">Features</a>
<a class="navbar-link" href="#" aria-current="page">Pricing</a>
<a class="navbar-link" href="#">Customers</a>
</nav>
<div class="navbar-actions"><a class="btn btn-sm btn-brand" href="#">Get started</a></div>
</div>
</div>
</header>| Class | Does |
|---|---|
.navbar-surface / .navbar-ink | White / dark background (the page color by default) |
.navbar-plain | No line under it |
.navbar-float | A floating card with a gap around it |
.navbar-center | Links in the middle |
.navbar-expand-lg | Folds below 1024px instead of 768px (many links) |
.navbar-expand-never | Never folds (two or three short links) |
.navbar-dropdown-menu.is-wide | A two-column dropdown; .is-end lines it up on the right |
Put the content in .navbar-inner.container to line the navbar up with the page's container.
Accessibility
- Links live in a
<nav>with anaria-label; the ☰ button needs a name (aria-label="Menu") and tells screen readers whether the panel is open. - The panel takes focus when it opens and gives it back when it closes; the page behind can't scroll.
- Dropdowns are native
<details>: they work with Enter, Space and Esc, and even without JavaScript.