4 hamburger menu animations in CSS

Three lines that turn into a close button when the menu opens. Four ways to do it, each a few lines of CSS on the same button.

UI loop

4 menu icons that turn into an X

Cross
Squeeze
Arrow
Spin
Sundry Millsundrymill.com/library
4 hamburger menu animations in CSS

Everything in the preview is live. Try it.

The code, one by one

Shared: the button and its three lines
.hm {
  display: inline-grid;
  place-items: center;
  width: 48px;
  height: 48px;
  padding: 0;
  border: 0;
  border-radius: 12px;
  background: #FFFFFF;
  color: #17202C;
  cursor: pointer;
  box-shadow: inset 0 0 0 2px #E5DED3;
}
.hm:focus-visible { outline: 3px solid #E8631A; outline-offset: 3px; }
.hm-box { position: relative; width: 22px; height: 16px; }
.hm-box i {
  position: absolute;
  left: 0;
  width: 100%;
  height: 2.5px;
  border-radius: 2px;
  background: currentColor;
  transition: translate .3s cubic-bezier(.33, 1, .68, 1), rotate .3s cubic-bezier(.33, 1, .68, 1), scale .3s cubic-bezier(.33, 1, .68, 1), opacity .2s;
}
.hm-box i:nth-child(1) { top: 0; }
.hm-box i:nth-child(2) { top: calc(50% - 1.25px); }
.hm-box i:nth-child(3) { bottom: 0; }
@media (prefers-reduced-motion: reduce) {
  .hm-box, .hm-box i { transition: none !important; }
}

Cross

The top and bottom lines meet in the middle and turn into an X while the middle line shrinks away.

HTML
<button class="hm hm-cross" type="button" aria-expanded="false" aria-label="Menu" data-burger><span class="hm-box" aria-hidden="true"><i></i><i></i><i></i></span></button>
CSS
.hm-cross[aria-expanded="true"] i:nth-child(1) { translate: 0 6.75px; rotate: 45deg; }
.hm-cross[aria-expanded="true"] i:nth-child(2) { opacity: 0; scale: 0 1; }
.hm-cross[aria-expanded="true"] i:nth-child(3) { translate: 0 -6.75px; rotate: -45deg; }
JavaScript
document.querySelectorAll('[data-burger]').forEach((btn) => {
  const menu = document.getElementById(btn.getAttribute('aria-controls'));
  btn.addEventListener('click', () => {
    const open = btn.getAttribute('aria-expanded') !== 'true';
    btn.setAttribute('aria-expanded', String(open));
    if (menu) menu.hidden = !open;
  });
});

Squeeze

Two steps: the lines slide together first, then turn. Closing runs the same steps backwards.

HTML
<button class="hm hm-squeeze" type="button" aria-expanded="false" aria-label="Menu" data-burger><span class="hm-box" aria-hidden="true"><i></i><i></i><i></i></span></button>
CSS
.hm-squeeze i { transition: rotate .15s cubic-bezier(.32, 0, .67, 0), translate .15s cubic-bezier(.33, 1, .68, 1) .15s, opacity .1s .15s; }
.hm-squeeze[aria-expanded="true"] i { transition: translate .15s cubic-bezier(.32, 0, .67, 0), rotate .15s cubic-bezier(.33, 1, .68, 1) .15s, opacity .1s; }
.hm-squeeze[aria-expanded="true"] i:nth-child(1) { translate: 0 6.75px; rotate: 45deg; }
.hm-squeeze[aria-expanded="true"] i:nth-child(2) { opacity: 0; }
.hm-squeeze[aria-expanded="true"] i:nth-child(3) { translate: 0 -6.75px; rotate: -45deg; }

Arrow

The outer lines fold into an arrow head, for a menu that slides in from the side and goes back the way it came.

HTML
<button class="hm hm-arrow" type="button" aria-expanded="false" aria-label="Menu" data-burger><span class="hm-box" aria-hidden="true"><i></i><i></i><i></i></span></button>
CSS
.hm-arrow i { transform-origin: left center; }
.hm-arrow[aria-expanded="true"] i:nth-child(1) { translate: 0 6.75px; rotate: -40deg; scale: .55 1; }
.hm-arrow[aria-expanded="true"] i:nth-child(3) { translate: 0 -6.75px; rotate: 40deg; scale: .55 1; }

Spin

The whole icon turns half a circle while it becomes an X. The most playful of the four.

HTML
<button class="hm hm-spin" type="button" aria-expanded="false" aria-label="Menu" data-burger><span class="hm-box" aria-hidden="true"><i></i><i></i><i></i></span></button>
CSS
.hm-spin .hm-box { transition: rotate .4s cubic-bezier(.33, 1, .68, 1); }
.hm-spin[aria-expanded="true"] .hm-box { rotate: 180deg; }
.hm-spin[aria-expanded="true"] i:nth-child(1) { translate: 0 6.75px; rotate: 45deg; }
.hm-spin[aria-expanded="true"] i:nth-child(2) { opacity: 0; scale: 0 1; }
.hm-spin[aria-expanded="true"] i:nth-child(3) { translate: 0 -6.75px; rotate: -45deg; }

How to use it

  • Each icon needs the shared CSS plus its own block. The lines move with the separate translate, rotate and scale properties, which is what lets the squeeze slide first and turn second.
  • The button keeps one name, "Menu", and aria-expanded says whether it is open. Point aria-controls at your menu and the script shows and hides it for you.
  • The opening moves use an ease out curve and last 300 milliseconds (400 for the spin), inside the 150 to 400 we use for every UI animation.
  • It draws its own orange focus ring, 3 pixels out, so keyboard users can find it on any header.

When to use it

  • Phone layouts, where the links do not fit across the header and a menu has to slide in.
  • Apps and tools with a side panel that people open and close often. The X tells them how to get out again.
  • Not on a wide screen with five links or fewer. Show the links themselves there: a hidden menu is one more thing to find.

A worked example: a phone menu for a small site

A header with the Cross icon that opens a full width menu on phones and disappears on wider screens.

  1. Give the menu an id, set hidden on it, and point the button's aria-controls at that id. The script flips hidden and aria-expanded together.
  2. Hide the button and show the links inline from 760 px up with a media query, and drop hidden from the menu there.
  3. Close the menu when a link inside it is clicked, so people land on the new section with the page in view.
  4. Let Escape close it too, and move focus back to the button when it does.
Close on Escape and on a link
const btn = document.querySelector('[data-burger]');
const menu = document.getElementById(btn.getAttribute('aria-controls'));
const close = () => { if (btn.getAttribute('aria-expanded') === 'true') btn.click(); };
menu.addEventListener('click', (e) => { if (e.target.closest('a')) close(); });
document.addEventListener('keydown', (e) => {
  if (e.key === 'Escape' && !menu.hidden) { close(); btn.focus(); }
});

Showing an interaction like this to a client or in a tutorial? Framok records your screen and zooms in on every click for you.

Free to use in personal and commercial work. No credit needed, though a link back is always nice.

More from the library

See the whole library