About this snippet

Add delight to your mobile menu with this animated hamburger icon. We use a simple checkbox hack to toggle the state, which transforms the three lines into a sleek "X" without needing JavaScript.

Size
663 bytes
Uses
HTML, CSS
Loads
Nothing external
Views
551
<label class="hamburger">
  <input type="checkbox">
  <span class="bar top"></span>
  <span class="bar mid"></span>
  <span class="bar bot"></span>
</label>
.hamburger {
  display: flex;
  flex-direction: column;
  gap: 6px;
  cursor: pointer;
  width: 40px;
}
.hamburger input {
  display: none;
}
.bar {
  display: block;
  width: 100%;
  height: 4px;
  background-color: #1f2937;
  border-radius: 4px;
  transition: transform 0.4s, opacity 0.4s;
}
.hamburger input:checked ~ .top {
  transform: translateY(10px) rotate(45deg);
}
.hamburger input:checked ~ .mid {
  opacity: 0;
}
.hamburger input:checked ~ .bot {
  transform: translateY(-10px) rotate(-45deg);
}
Esc
↑↓ moveEnter select

Start from a template

Open an .html file, or HTML, CSS, JS, Markdown, Less or TypeScript files. You can also drop them here. Styles and scripts go into their panes.

Share

Source

Keyboard shortcuts

Save