CSS Hamburger to X
A pure CSS hamburger menu icon that animates into an X when clicked, using a hidden checkbox.
<label class="burger">
<input class="burger-toggle" type="checkbox" aria-label="Menu">
<span class="burger-line"></span>
</label>
.burger {
position: relative;
display: inline-block;
width: 44px;
height: 44px;
border-radius: 8px;
cursor: pointer;
}
.burger-toggle {
position: absolute;
opacity: 0;
pointer-events: none;
}
.burger:has(.burger-toggle:focus-visible) {
outline: 2px solid #2563eb;
outline-offset: 2px;
}
.burger-line,
.burger-line::before,
.burger-line::after {
position: absolute;
width: 28px;
height: 3px;
border-radius: 3px;
background: #111;
transition: transform 0.3s ease, top 0.3s ease, background 0.3s ease;
}
.burger-line {
top: calc(50% - 1.5px);
left: 8px;
}
.burger-line::before,
.burger-line::after {
content: "";
left: 0;
}
.burger-line::before {
top: -9px;
}
.burger-line::after {
top: 9px;
}
.burger-toggle:checked + .burger-line {
background: transparent;
}
.burger-toggle:checked + .burger-line::before {
top: 0;
transform: rotate(45deg);
}
.burger-toggle:checked + .burger-line::after {
top: 0;
transform: rotate(-45deg);
}
@media (prefers-reduced-motion: reduce) {
.burger-line,
.burger-line::before,
.burger-line::after {
transition: none;
}
}
About this CSS Hamburger to X
This CSS Hamburger to X is 71 lines of pure CSS (1.1 KB) with no JavaScript and no dependencies. It uses transitions, transforms, the :has() selector and the :checked state (no JavaScript). It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.
CSS features used
- transitions
- transforms
- the :has() selectorChrome 105+, Safari 15.4+, Firefox 121+.
- the :checked state (no JavaScript)
- keyboard focus styles
- reduced-motion support
Customize it
The colors in this snippet. Search and replace them to match your brand:
#2563eb#111
How to use this hamburger to x
- Copy the HTML and paste it where you want the element to appear.
- Copy the CSS into your stylesheet, or inside a
<style>tag. - Keep the
.burgerclass name, or rename it in both places. Then tweak colors, sizes and timing to match your design. - Using React or Next.js? Change
classtoclassNamein the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).
Building with an AI tool?
“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.