# Responsive Navbar

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

- Category: [CSS Navigation](https://copy-css.com/category/navigation)
- Tags: hamburger, mobile menu, header, checkbox, container query
- Live preview: https://copy-css.com/snippets/responsive-navbar
- License: Free for personal and commercial use. No signup, no attribution required.

## HTML

```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

```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;
  }
}
```

## Notes

- 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.
- container queries: Chrome 105+, Safari 16+, Firefox 110+.
- In JSX use `className` instead of `class`. In Tailwind projects put the CSS in the global stylesheet.
