CSSmediumCSS

Animated Hamburger Menu Icon

CSS-only animated hamburger icon that transforms to X on click.

01

The problem

You need an animated hamburger menu icon for mobile navigation.

02

The solution

CSS
.hamburger {
  width: 30px;
  height: 24px;
  position: relative;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.hamburger span {
  display: block;
  height: 3px;
  width: 100%;
  background: #fff;
  border-radius: 3px;
  transition: all 0.3s ease;
}

/* Active state - X shape */
.hamburger.active span:nth-child(1) {
  transform: translateY(10.5px) rotate(45deg);
}

.hamburger.active span:nth-child(2) {
  opacity: 0;
  transform: translateX(-20px);
}

.hamburger.active span:nth-child(3) {
  transform: translateY(-10.5px) rotate(-45deg);
}

/* Alternative: Arrow transformation */
.hamburger.arrow.active span:nth-child(1) {
  transform: translateY(4px) rotate(45deg) scaleX(0.5);
  transform-origin: right;
}

.hamburger.arrow.active span:nth-child(3) {
  transform: translateY(-4px) rotate(-45deg) scaleX(0.5);
  transform-origin: right;
}

03

See it live

Isolated previewSandboxed
04

Put it to work

Example
<button class="hamburger" onclick="this.classList.toggle('active')">
  <span></span>
  <span></span>
  <span></span>
</button>

Worth knowing

Toggle the .active class with JavaScript. Works great with mobile slide-out navigation.