Hamburger Icon That Morphs Into an X
A bakery header with the classic three-line menu icon. Pressing it rotates the top and bottom lines into an X while the middle line shrinks away, and the menu fades down under the header with four large serif links and a short note for each. The notes hide on phones so the links stay big enough to tap. It opens already expanded so you can see both states.
Code
import { useEffect, useRef, useState } from "react";
// Edit the menu here. The icon itself is three spans moved with CSS transforms.
const brand = "Fenwick Bakehouse";
const links = [
{ label: "Breads", note: "Sourdough, rye and the Saturday loaf", href: "#" },
{ label: "Pastries", note: "Laminated every morning from 4am", href: "#" },
{ label: "Order ahead", note: "Collect from 7:30 on weekdays", href: "#" },
{ label: "Visit", note: "14 Quay Street, open six days", href: "#" },
];
export default function HamburgerIconThatMorphsIntoAnX() {
const [open, setOpen] = useState(true);
const button = useRef<HTMLButtonElement>(null);
useEffect(() => {
if (!open) return;
const onKey = (e: KeyboardEvent) => {
if (e.key === "Escape") {
setOpen(false);
button.current?.focus();
}
};
document.addEventListener("keydown", onKey);
return () => document.removeEventListener("keydown", onKey);
}, [open]);
const bar = "absolute left-0 h-0.5 w-6 rounded-full bg-current transition duration-300 ease-out motion-reduce:transition-none";
return (
<div className="bg-amber-50 px-4 py-10 sm:px-8">
<div className="relative mx-auto h-[420px] max-w-3xl overflow-hidden rounded-2xl border border-amber-200 bg-white">
<header className="flex h-16 items-center justify-between border-b border-amber-100 px-5">
<a href="#" className="rounded font-serif text-lg font-semibold tracking-tight text-stone-900 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-amber-600">
{brand}
</a>
<button
ref={button}
type="button"
aria-expanded={open}
aria-controls="hmx-menu"
aria-label={open ? "Close menu" : "Open menu"}
onClick={() => setOpen((v) => !v)}
className="relative grid h-11 w-11 place-items-center rounded-full text-stone-900 transition-colors hover:bg-amber-100 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-amber-600"
>
<span className="relative block h-6 w-6" aria-hidden="true">
<span className={`${bar} top-[5px] ${open ? "translate-y-[6px] rotate-45" : ""}`} />
<span className={`${bar} top-[11px] ${open ? "scale-x-0 opacity-0" : ""}`} />
<span className={`${bar} top-[17px] ${open ? "-translate-y-[6px] -rotate-45" : ""}`} />
</span>
</button>
</header>
<nav
id="hmx-menu"
aria-label="Main"
className={`absolute inset-x-0 top-16 origin-top px-5 pb-6 pt-4 transition duration-300 ease-out motion-reduce:transition-none ${open ? "visible translate-y-0 opacity-100" : "invisible -translate-y-2 opacity-0"}`}
>
<ul className="divide-y divide-amber-100">
{links.map((l) => (
<li key={l.label}>
<a href="#" className="group flex items-baseline justify-between gap-4 rounded-lg px-2 py-4 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-amber-600">
<span className="font-serif text-2xl text-stone-900 group-hover:text-amber-700">{l.label}</span>
<span className="hidden text-sm text-stone-600 sm:block">{l.note}</span>
</a>
</li>
))}
</ul>
</nav>
{!open && (
<div className="grid h-[calc(100%-4rem)] place-items-center px-6 text-center">
<p className="max-w-xs text-stone-600">Menu closed. Press the button to see the three lines turn back into an X.</p>
</div>
)}
</div>
</div>
);
}
Install with one command: npx shadcn@latest add https://we.inc/r/hamburger-icon-that-morphs-into-an-x.json
When to use it
- A small business site needs one familiar menu button that works the same on every screen size.
- You want the open state to be obvious from the icon itself, without a separate close button inside the menu.
- The header is short and the menu has four to six top-level pages that fit in one column.
- You are replacing a checkbox hack menu with one that screen readers can understand.
How to customize it
- Change the line thickness with `h-0.5` on the bars in React or `--hmx-line` in the plain version; if you make them thicker, keep the 6px offsets in step with the gap between lines.
- Recolour the accent through `ring-amber-600` and `hover:text-amber-700`, or `--hmx-accent` in the CSS version.
- Edit the `links` array to change pages and notes; delete the `note` field and its span if you only want labels.
- Slow or speed the morph with `duration-300` or `--hmx-speed`; anything past half a second starts to feel sluggish on a menu.
Accessibility
- The icon is a real button with aria-expanded and aria-controls pointing at the nav, and its label switches between Open menu and Close menu.
- Escape closes the menu and sends focus back to the button, so keyboard users never lose their place.
- The closed menu is hidden with visibility as well as opacity, so its links leave the tab order when it is shut.
Questions
How do I turn a hamburger icon into an X with CSS?
Stack three thin bars inside a square, 6px apart. In the open state, move the top bar down 6px and rotate it 45 degrees, move the bottom bar up 6px and rotate it minus 45 degrees, and fade or scale the middle bar to nothing. A transition on transform does the rest.
Is the checkbox hack a good way to build a hamburger menu?
It works without script, but a checkbox is announced as a checkbox, not a menu button, and it cannot report aria-expanded. A button with a few lines of JavaScript is clearer for assistive technology and easier to close with Escape or a link click.
Why use the aria-expanded attribute to style the open state?
Styling from the same attribute that screen readers read keeps the visuals and the announced state from drifting apart. If the icon shows an X, aria-expanded is true, and there is no second class to forget to toggle.
Should the menu close when a link is clicked?
On a single-page site with anchor links, yes: add a click listener on the links that closes the menu. On a normal site each link loads a new page, so the menu resets on its own.
How big should the hamburger button be?
Keep the tap area at least 44 by 44 pixels even though the icon is only 24 pixels wide. The padding around the lines is what makes it easy to hit with a thumb.
Related: CSS Hamburger Menu Collection with 12 Animated Icons and an Accessible Mobile Nav, Navbar with Centered Links, CTA Button and Mobile Menu, Navy Circle That Grows From the Menu Button.
More navbars or all free UI components. Build your site free to have a full site built around it.
More in Navbars
- Navbar with Centered Links, CTA Button and Mobile Menu
- Navbar with Liquid Stretch Highlight and Inverting Labels
- Navbar with Search Field and Category Bar
- Ray-Marched Frosted Glass Pill Navbar on Steel Standoffs with a Bead of Light Inside over Colour-Block Photographs That Slide Past
- Responsive React Navbar with Mega Menu, Animated Hamburger and Sticky Shrink on Scroll
- Soft Silicone Pill Navbar with a Glowing Ball Rolling Under the Rubber
- Black Glass Pill Navbar with a Mercury Drop That Stretches to the Active Link
- CSS Hamburger Menu Collection with 12 Animated Icons and an Accessible Mobile Nav
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App