CSS Hamburger Menu: Icon, Animation and Accessible Toggle

By Marko Denic
Contents
  1. The hamburger icon
  2. Three bars with spans
  3. Or one element with ::before and ::after
  4. The accessible version
  5. HTML
  6. JavaScript
  7. CSS: style from aria-expanded, mobile only
  8. FAQ

A hamburger menu is the three-line button that opens the navigation on small screens. Building one takes three parts: the icon, the menu it toggles, and the code that connects them. The demo below does all of it in CSS; further down you’ll find the accessible version you should ship.

Demo:

Click the icon: the bars turn into an X and the menu opens below. No JavaScript involved.

The hamburger icon

There are two ways to draw the three bars: three separate elements, or one element plus its ::before and ::after pseudo-elements.

Three bars with spans

Three elements, stacked with gap, are the easiest version to animate, because each bar can move on its own. The math for the X: the bars are 3px tall with a 5px gap, so the top and bottom bars move 8px (3 + 5) to the middle, then rotate 45 degrees.

css
.hamburger {
  display: grid;
  gap: 5px;
}

.hamburger span {
  width: 24px;
  height: 3px;
  border-radius: 2px;
  background: currentColor;
  transition: transform 0.3s, opacity 0.3s;
}

/* Open state */
.is-open .hamburger span:nth-child(1) { transform: translateY(8px) rotate(45deg); }
.is-open .hamburger span:nth-child(2) { opacity: 0; }
.is-open .hamburger span:nth-child(3) { transform: translateY(-8px) rotate(-45deg); }

Or one element with ::before and ::after

If you only want one element in the HTML, draw the middle bar with the element itself and the outer bars with ::before and ::after.

css
.burger,
.burger::before,
.burger::after {
  display: block;
  width: 24px;
  height: 3px;
  border-radius: 2px;
  background: currentColor;
}

.burger {
  position: relative;
}

.burger::before,
.burger::after {
  content: "";
  position: absolute;
}

.burger::before { top: -8px; }
.burger::after  { top: 8px; }

The accessible version

The CSS-only toggle is fine for a demo, but a real menu button should be a <button> that tells screen readers whether the menu is open. That needs a few lines of JavaScript.

HTML

aria-expanded announces the state, aria-controls links the button to the menu, and the text inside is read aloud while the bars are hidden from screen readers.

html
<header class="site-header">
  <a href="/" class="logo">Logo</a>

  <button class="menu-toggle" aria-expanded="false" aria-controls="main-menu">
    <span class="visually-hidden">Menu</span>
    <span class="hamburger" aria-hidden="true">
      <span></span><span></span><span></span>
    </span>
  </button>

  <nav id="main-menu" class="main-menu">
    <a href="/blog/">Blog</a>
    <a href="/tools/">Tools</a>
    <a href="/contact/">Contact</a>
  </nav>
</header>

JavaScript

Toggle aria-expanded and style the open state from it, so the visual state and the announced state can never disagree. Closing on Escape is expected by keyboard users.

javascript
const button = document.querySelector('.menu-toggle');

function setOpen(open) {
  button.setAttribute('aria-expanded', open);
}

button.addEventListener('click', () => {
  setOpen(button.getAttribute('aria-expanded') !== 'true');
});

document.addEventListener('keydown', (e) => {
  if (e.key === 'Escape') setOpen(false);
});

CSS: style from aria-expanded, mobile only

Hide the button and show the full menu on wide screens; below the breakpoint, the menu is hidden until the button is expanded. The header layout is a simple flexbox row.

css
.site-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
}

.menu-toggle { display: none; }

@media (max-width: 768px) {
  .menu-toggle { display: block; }

  .main-menu {
    display: none;
    flex-basis: 100%;
  }

  .menu-toggle[aria-expanded="true"] + .main-menu {
    display: grid;
  }

  .menu-toggle[aria-expanded="true"] .hamburger span:nth-child(1) { transform: translateY(8px) rotate(45deg); }
  .menu-toggle[aria-expanded="true"] .hamburger span:nth-child(2) { opacity: 0; }
  .menu-toggle[aria-expanded="true"] .hamburger span:nth-child(3) { transform: translateY(-8px) rotate(-45deg); }
}

@media (prefers-reduced-motion: reduce) {
  .hamburger span { transition: none; }
}

/* Text for screen readers only */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

FAQ

Can I make a hamburger menu without JavaScript?

Yes, with a hidden checkbox and a <label> (like the demo) or with <details> and <summary>. Both work, but neither announces the menu state as clearly as a button with aria-expanded, so prefer the JavaScript version for production.

Why is it called a hamburger menu?

The three lines look like a burger: bun, patty, bun. The icon was designed by Norm Cox for the Xerox Star in 1981.

Should I use a hamburger menu on desktop?

Usually not. Hiding navigation behind an icon makes it less visible, and on wide screens there’s room to show the links. Use it where space is tight.

How do I add a label next to the icon?

Put visible text like “Menu” inside the button next to the bars. Usability studies have found that a visible label helps more people find the menu.