CSS Responsive Navbar

A header nav that collapses into a hamburger menu on narrow widths, using a checkbox and a container query.

  • hamburger
  • mobile menu
  • header
  • checkbox
  • container query
HTML
<div class="rnav">
  <header class="rnav-bar">
    <a class="rnav-logo" href="#">Brand</a>
    <input class="rnav-toggle" id="rnav-toggle" type="checkbox" aria-label="Menu">
    <label class="rnav-burger" for="rnav-toggle" aria-hidden="true"><span></span></label>
    <nav class="rnav-links" aria-label="Main">
      <a href="#" aria-current="page">Home</a>
      <a href="#">Pricing</a>
      <a href="#">Blog</a>
      <a href="#">Contact</a>
    </nav>
  </header>
</div>
CSS
.rnav {
  container-type: inline-size;
  font: 500 15px system-ui, sans-serif;
}

.rnav-bar {
  position: relative;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 24px;
  padding: 10px 12px 10px 18px;
  border-radius: 14px;
  background: #ffffff;
  box-shadow: 0 1px 3px #0f172a1f;
}

.rnav-logo {
  color: #0f172a;
  font-weight: 800;
  text-decoration: none;
}

/* Hidden visually, still reachable with Tab and toggled with Space */
.rnav-toggle {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.rnav-burger {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  cursor: pointer;
}

.rnav-burger span,
.rnav-burger span::before,
.rnav-burger span::after {
  display: block;
  width: 18px;
  height: 2px;
  border-radius: 2px;
  background: #0f172a;
  transition: transform 0.25s ease, background-color 0.25s ease;
}

.rnav-burger span {
  position: relative;
}

.rnav-burger span::before,
.rnav-burger span::after {
  content: "";
  position: absolute;
  left: 0;
}

.rnav-burger span::before {
  top: -6px;
}

.rnav-burger span::after {
  top: 6px;
}

.rnav-toggle:checked + .rnav-burger span {
  background: transparent;
}

.rnav-toggle:checked + .rnav-burger span::before {
  transform: translateY(6px) rotate(45deg);
}

.rnav-toggle:checked + .rnav-burger span::after {
  transform: translateY(-6px) rotate(-45deg);
}

.rnav-toggle:focus-visible + .rnav-burger {
  outline: 2px solid #4f46e5;
  outline-offset: 2px;
}

.rnav-links {
  display: none;
  flex-basis: 100%;
  flex-direction: column;
  gap: 2px;
}

.rnav-toggle:checked ~ .rnav-links {
  display: flex;
}

.rnav-links a {
  padding: 10px 12px;
  border-radius: 8px;
  color: #475569;
  text-decoration: none;
}

.rnav-links a:hover,
.rnav-links a[aria-current="page"] {
  background: #f1f5f9;
  color: #0f172a;
}

.rnav-links a:focus-visible {
  outline: 2px solid #4f46e5;
  outline-offset: 1px;
}

/* Wide enough: links inline, no burger */
@container (min-width: 520px) {
  .rnav-toggle,
  .rnav-burger {
    display: none;
  }

  .rnav-links {
    display: flex;
    flex-basis: auto;
    flex-direction: row;
  }
}

About this CSS Responsive Navbar

This CSS Responsive Navbar is 131 lines of pure CSS (2.2 KB) with no JavaScript and no dependencies. It uses transitions, transforms, box shadows and CSS Grid. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.

CSS features used

Customize it

The colors in this snippet. Search and replace them to match your brand:

  • #ffffff
  • #0f172a1f
  • #0f172a
  • #4f46e5
  • #475569
  • #f1f5f9

How to use this responsive navbar

  1. Copy the HTML and paste it where you want the element to appear.
  2. Copy the CSS into your stylesheet, or inside a <style> tag.
  3. Keep the .rnav class name, or rename it in both places. Then tweak colors, sizes and timing to match your design.
  4. Using React or Next.js? Change class to className in the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).

Building with an AI tool?

“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.

Feedback