Navbar

header.navbar

Navbar — the site header bar: a brand, a navigation slot, an actions slot.

Examples

A site header that collapses

View “A site header that collapses” as
<header class="navbar navbar-sticky">
  <a class="navbar-brand" href="#home">Acme</a>

  <nav class="nav navbar-nav" aria-label="Site">
    <ul class="nav-list">
      <li><a class="nav-link" href="#home" aria-current="page">Overview</a></li>
      <li><a class="nav-link" href="#pricing">Pricing</a></li>
      <li><a class="nav-link" href="#docs">Docs</a></li>
    </ul>
  </nav>

  <div class="navbar-actions">
    <a class="btn btn-primary btn-sm" href="#signup">Sign up</a>
  </div>

  <button class="btn btn-quiet btn-icon navbar-toggle" type="button"
          command="show-modal" commandfor="site-menu" aria-label="Open menu"></button>

  <dialog class="drawer drawer-start" id="site-menu" aria-label="Site menu">
    <header class="drawer-header">
      <p class="drawer-title">Menu</p>
      <button class="btn btn-quiet drawer-close" type="button"
              command="close" commandfor="site-menu">Close</button>
    </header>
    <div class="drawer-body">
      <nav class="nav nav-vertical" aria-label="Site menu links">
        <ul class="nav-list">
          <li><a class="nav-link" href="#home" aria-current="page">Overview</a></li>
          <li><a class="nav-link" href="#pricing">Pricing</a></li>
          <li><a class="nav-link" href="#docs">Docs</a></li>
        </ul>
      </nav>
    </div>
  </dialog>
</header>

Narrow the window and the bar’s nav gives way to the toggle.

The panel is a modal <dialog> opened by an invoker command, so the focus trap, Escape and the inert page are the platform’s — and no script is involved anywhere.

Just a bar

View “Just a bar” as
<header class="navbar">
  <a class="navbar-brand" href="#home">Acme</a>
  <div class="navbar-actions">
    <a class="btn btn-quiet btn-sm" href="#account">Account</a>
  </div>
</header>

The nav slot is optional. There is no inner container: a full-bleed bar with a centred column is page layout, so put your own <div> inside with max-inline-size: var(--container).

Tokens 19

Level 2, declared on .navbar itself. Set any of them on that selector to restyle this component without touching the skin.

Level 2 tokens declared by navbar
TokenDefault
--navbar-actions-gapvar(--space-2)
--navbar-brand-colorvar(--color-on-surface)
--navbar-brand-font-familyvar(--font-primary)
--navbar-brand-font-sizevar(--font-size-lg)
--navbar-brand-font-weightvar(--font-weight-semibold)
--navbar-brand-gapvar(--space-2)
--navbar-gapvar(--space-4)
--navbar-min-block-size3.5rem
--navbar-padding-blockvar(--space-2)
--navbar-padding-inlinevar(--space-4)
--navbar-z-index50
--navbar-bgvar(--color-surface)
--navbar-border-colorvar(--color-border-subtle)
--navbar-border-widthvar(--border-width)
--navbar-colorvar(--color-on-surface)
--navbar-shadownone
--navbar-toggle-bar-block-size0.8125rem
--navbar-toggle-bar-inline-size1.375rem
--navbar-toggle-bar-thickness2px

Variants and states

Variants

  • .navbar-lg
  • .navbar-sm
  • .navbar-sticky

Inside it

  • .navbar-brand
  • .navbar-toggle
  • .navbar-nav
  • .navbar-split
  • .navbar-actions

State it reads

Read from the platform, never mirrored into a class that could disagree with it.

  • :focus
  • data-legacy

Before you ship it

What you have to do 5 requirements

  1. Name every landmark. aria-label on each <nav> and on the <dialog>.
  2. Name the toggle. It is icon-only and this file draws its glyph, so it has no text: aria-label on the button.
  3. Ship the drawer, or drop the toggle. A toggle whose commandfor points at nothing is a dead control.
  4. Mark the current page in both copies of the list with aria-current="page" — they are two elements and neither knows about the other.
  5. Raise --scroll-padding if the bar is sticky and tall. The reset sets scroll-padding-block-start: var(--scroll-padding, 6.25rem) on html, which is what stops an in-page anchor landing underneath a sticky header; a taller bar needs a bigger value.

src/css/components/navbar.css · npx mostlycss add navbar