Hamburger Button

CSS, Interactivity · Dec 30, 2020

Displays a hamburger menu which transitions to a cross button on hover.

  • Use a .hamburger-menu container div which contains the top, bottom, and middle bars.
  • Set the container to display: flex with flex-flow: column wrap.
  • Add distance between the bars using justify-content: space-between.
  • Use transform: rotate() to rotate the top and bottom bars by 45 degrees and opacity: 0 to fade the middle bar on hover.
  • Use transform-origin: left so that the bars rotate around the left point.
<div class="hamburger-menu">
  <div class="bar top"></div>
  <div class="bar middle"></div>
  <div class="bar bottom"></div>
</div>
.hamburger-menu {
  display: flex;
  flex-flow: column wrap;
  justify-content: space-between;
  height: 2.5rem;
  width: 2.5rem;
  cursor: pointer;
}

.hamburger-menu .bar {
  height: 5px;
  background: black;
  border-radius: 5px;
  margin: 3px 0px;
  transform-origin: left;
  transition: all 0.5s;
}

.hamburger-menu:hover .top {
  transform: rotate(45deg);
}

.hamburger-menu:hover .middle {
  opacity: 0;
}

.hamburger-menu:hover .bottom {
  transform: rotate(-45deg);
}

More like this

  • Popout menu

    Reveals an interactive popout menu on hover/focus.

    CSS, Interactivity · Dec 30, 2020

  • Tip: The perfect duration for CSS transitions

    Learn how to make your CSS transitions feel perfect when users interact with elements on the page with this simple tip.

    CSS, Interactivity · Jun 12, 2021

  • Sibling fade

    Fades out the siblings of a hovered item.

    CSS, Interactivity · Dec 30, 2020