Snippets Header

Bootstrap 5 Responsive Navbar with Offcanvas Menu

A floating, frosted header bar with brand, centred links and a call-to-action that collapses into a slide-in offcanvas menu below the large breakpoint. The active link is highlighted and the bar stays pinned on scroll.

Bootstrap 5 No jQuery Mar 2020 2.3K views

<div class="nv-page">
  <header class="nv-wrap sticky-top">
    <nav class="navbar navbar-expand-lg nv bg-body border rounded-4 shadow-sm mx-auto py-2" aria-label="Main navigation">
      <div class="container" style="max-width:1080px">
        <a class="navbar-brand d-flex align-items-center gap-2 fw-bold" href="#"><span class="wdw-icon" style="width:2.25rem;height:2.25rem;border-radius:.75rem"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.25" stroke-linecap="round" aria-hidden="true"><path d="M6 5v14M12 9v6M18 3v18"/></svg></span>Acme</a>
        <button class="navbar-toggler border-0 shadow-none" type="button" data-bs-toggle="offcanvas" data-bs-target="#nvMenu" aria-controls="nvMenu" aria-label="Open menu"><span class="navbar-toggler-icon"></span></button>
        <div class="offcanvas offcanvas-end" tabindex="-1" id="nvMenu" aria-labelledby="nvMenuLabel">
          <div class="offcanvas-header"><h2 class="offcanvas-title h5 fw-bold" id="nvMenuLabel">Menu</h2><button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button></div>
          <div class="offcanvas-body align-items-lg-center">
            <ul class="navbar-nav mx-lg-auto gap-lg-1">
              <li class="nav-item"><a class="nav-link active" aria-current="page" href="#">About</a></li>
              <li class="nav-item"><a class="nav-link" href="#">Products</a></li>
              <li class="nav-item"><a class="nav-link" href="#">Services</a></li>
              <li class="nav-item"><a class="nav-link" href="#">Work</a></li>
              <li class="nav-item"><a class="nav-link" href="#">Contact</a></li>
            </ul>
            <a href="#" class="btn btn-primary rounded-pill px-4 mt-3 mt-lg-0">Get started</a>
          </div>
        </div>
      </div>
    </nav>
  </header>
  <main class="container py-4 py-lg-5" style="max-width:1080px">
    <section class="wdw-aurora rounded-4 p-4 p-lg-5 mb-4 overflow-hidden">
      <div class="position-relative" style="max-width:36rem">
        <span class="small fw-bold text-uppercase opacity-75" style="letter-spacing:.08em">Responsive header</span>
        <h1 class="display-6 fw-bold mt-2">A navbar that collapses into a slide-in menu.</h1>
        <p class="opacity-75 mb-4">Links sit inline on wide screens and move into an offcanvas drawer below the large breakpoint. The header stays pinned while you scroll.</p>
        <a href="#" class="btn btn-light rounded-pill px-4">Get started</a>
      </div>
      <div class="row g-3 text-center position-relative mt-4 pt-4 border-top border-light border-opacity-25"><div class="col-4"><div class="fs-2 fw-bold lh-1">5</div><div class="small opacity-75 mt-1">links</div></div><div class="col-4"><div class="fs-2 fw-bold lh-1">1</div><div class="small opacity-75 mt-1">drawer</div></div><div class="col-4"><div class="fs-2 fw-bold lh-1">0</div><div class="small opacity-75 mt-1">scripts</div></div></div>
    </section>
    <div class="row g-4">
      <div class="col-md-4"><div class="wdw-card p-4 h-100"><h2 class="h6 fw-bold">Try it</h2><p class="wdw-muted small mb-0">Resize the window or open this on a phone to use the drawer.</p></div></div>
      <div class="col-md-4"><div class="wdw-card p-4 h-100"><h2 class="h6 fw-bold">Stays pinned</h2><p class="wdw-muted small mb-0">Scroll this page: the floating bar stays at the top.</p></div></div>
      <div class="col-md-4"><div class="wdw-card p-4 h-100"><h2 class="h6 fw-bold">Keyboard ready</h2><p class="wdw-muted small mb-0">Escape closes the drawer and focus returns to the menu button.</p></div></div>
    </div>
    <div class="wdw-card p-4 p-lg-5 mt-4"><h2 class="h5 fw-bold">Built on Bootstrap</h2><p class="wdw-muted mb-0">The bar is a standard navbar with an offcanvas menu. Only the pill link states use custom CSS.</p></div>
    <div class="wdw-card p-4 p-lg-5 mt-4"><h2 class="h5 fw-bold">Content below the fold</h2><p class="wdw-muted mb-0" style="min-height:14rem">Extra height so you can scroll and watch the header stay put.</p></div>
  </main>
</div>

Built on Bootstrap 5.3.8 with plain JavaScript — no jQuery. Supports light and dark mode via data-bs-theme.

Free to use in personal and commercial projects under the MIT license.