CSS Off-canvas Menu

A slide-in drawer menu with no JavaScript, built on <details> so it is keyboard and screen reader friendly.

  • drawer
  • sidebar
  • off-canvas
  • details
  • mobile menu
  • slide
HTML
<details class="offcanvas">
  <summary aria-label="Menu"></summary>
  <nav>
    <a href="#">Home</a>
    <a href="#">Projects</a>
    <a href="#">About</a>
    <a href="#">Contact</a>
  </nav>
</details>
CSS
.offcanvas summary {
  position: relative;
  z-index: 2;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: #0f172a;
  color: #fff;
  list-style: none;
  cursor: pointer;
}

.offcanvas summary::-webkit-details-marker {
  display: none;
}

/* Hamburger: three lines that fold into an X when open */
.offcanvas summary::before,
.offcanvas summary::after {
  content: "";
  grid-area: 1 / 1;
  width: 18px;
  height: 2px;
  border-radius: 2px;
  background: currentColor;
  transition: transform 0.25s ease, box-shadow 0.25s ease;
}

.offcanvas summary::before {
  box-shadow: 0 -6px currentColor, 0 6px currentColor;
}

.offcanvas[open] summary::before {
  box-shadow: 0 0 transparent, 0 0 transparent;
  transform: rotate(45deg);
}

.offcanvas[open] summary::after {
  transform: rotate(-45deg);
}

.offcanvas summary:focus-visible {
  outline: 2px solid #38bdf8;
  outline-offset: 2px;
}

.offcanvas nav {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 1;
  display: grid;
  align-content: start;
  gap: 4px;
  width: min(260px, 80vw);
  padding: 72px 12px 12px;
  background: #fff;
  box-shadow: 8px 0 32px -12px rgba(0, 0, 0, 0.35);
  animation: offcanvas-in 0.3s cubic-bezier(0.3, 0.7, 0.4, 1);
}

.offcanvas nav a {
  padding: 10px 12px;
  border-radius: 8px;
  color: #334155;
  font: 500 15px system-ui, sans-serif;
  text-decoration: none;
}

.offcanvas nav a:hover {
  background: #f1f5f9;
  color: #0f172a;
}

@keyframes offcanvas-in {
  from { transform: translateX(-100%); }
}

@media (prefers-reduced-motion: reduce) {
  .offcanvas nav {
    animation: none;
  }
}

About this CSS Off-canvas Menu

This CSS Off-canvas Menu is 84 lines of pure CSS (1.6 KB) with no JavaScript and no dependencies. It uses keyframe animations, transitions, transforms and box shadows. 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:

  • #0f172a
  • #fff
  • #38bdf8
  • #334155
  • #f1f5f9

How to use this off-canvas menu

  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 .offcanvas 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