Hamburger to Close Morph
A three-bar menu icon that morphs into an X while the menu is open.
AI prompt
Create a menu toggle button 44x44px containing three 2px tall, 22px wide bars with 6px spacing, plus a dropdown panel below with three links (Library, Request, About). Clicking the button toggles aria-expanded and an open class. In the open state the top bar translates 8px down and rotates 45deg, the bottom bar translates 8px up and rotates -45deg, and the middle bar fades to opacity 0 and scales to 0 horizontally, all over 260ms cubic-bezier(.3,.8,.2,1). The panel fades from opacity 0 and translateY(-6px) in 180ms and has hidden state removed from tab order. Escape closes the menu and returns focus to the button; clicking outside also closes. The button has aria-controls pointing at the panel and aria-label "Menu". Hover tints the button; focus-visible shows a 2px ring. Under prefers-reduced-motion remove transitions.About this pattern
Definition
A menu toggle whose three lines rotate and fade into a close cross when the menu opens, and back again when it closes.
Purpose & UX principles
- One control for both opening and closing keeps the interface simple.
- The icon change confirms the new state;
aria-expandedcommunicates it to assistive technology. - Built from three plain elements, so it needs no icon assets and stays crisp at any size.
Interaction states
- Closed, open, hover (background tint), focus-visible, pressed.
Motion specification
- Top and bottom bars move to the centre and rotate 45 degrees, the middle bar fades: 260ms cubic-bezier(.3,.8,.2,1).
- The panel fades and slides 6px in 180ms.
- Reduced motion: icon swaps and panel appears instantly.
Responsive behaviour
- 44px square hit area with a 22px icon.
- The panel opens below the button and is as wide as its content.
Code
<div class="wrap">
<button class="hb" id="hb" aria-label="Menu" aria-expanded="false" aria-controls="pn"><i></i><i></i><i></i></button>
<nav class="pn" id="pn" hidden><a href="#">Library</a><a href="#">Request</a><a href="#">About</a></nav>
</div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.wrap{position:relative}
.hb{position:relative;width:44px;height:44px;border-radius:12px;border:1px solid var(--line);background:var(--surface);cursor:pointer;transition:background .15s}
.hb:hover{background:var(--bg)}
.hb:focus-visible{outline:2px solid var(--accent);outline-offset:3px}
.hb i{position:absolute;left:11px;width:22px;height:2px;border-radius:1px;background:var(--text);transition:transform .26s cubic-bezier(.3,.8,.2,1),opacity .2s}
.hb i:nth-child(1){top:14px}.hb i:nth-child(2){top:21px}.hb i:nth-child(3){top:28px}
.hb[aria-expanded=true] i:nth-child(1){transform:translateY(7px) rotate(45deg)}
.hb[aria-expanded=true] i:nth-child(2){opacity:0;transform:scaleX(0)}
.hb[aria-expanded=true] i:nth-child(3){transform:translateY(-7px) rotate(-45deg)}
.pn{position:absolute;left:0;top:calc(100% + 8px);min-width:160px;padding:6px;background:var(--surface);border:1px solid var(--line);border-radius:12px;box-shadow:0 12px 28px rgba(0,0,0,.12);animation:in .18s ease-out}
.pn[hidden]{display:none}
.pn a{display:block;padding:9px 12px;border-radius:8px;font-size:13.5px;color:inherit;text-decoration:none}
.pn a:hover,.pn a:focus-visible{background:var(--bg);outline:none}
@keyframes in{from{opacity:0;transform:translateY(-6px)}}
@media (prefers-reduced-motion:reduce){.hb,.hb i{transition:none}.pn{animation:none}}const hb=document.getElementById('hb'),pn=document.getElementById('pn');
function set(o,f){hb.setAttribute('aria-expanded',String(o));pn.hidden=!o;if(!o&&f)hb.focus();}
hb.addEventListener('click',()=>set(pn.hidden,false));
document.addEventListener('keydown',e=>{if(e.key==='Escape'&&!pn.hidden)set(false,true);});
document.addEventListener('pointerdown',e=>{if(!pn.hidden&&!pn.contains(e.target)&&!hb.contains(e.target))set(false,false);});
pn.querySelectorAll('a').forEach(a=>a.addEventListener('click',e=>{e.preventDefault();set(false,true);}));One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Hamburger to Close Morph — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.wrap{position:relative}
.hb{position:relative;width:44px;height:44px;border-radius:12px;border:1px solid var(--line);background:var(--surface);cursor:pointer;transition:background .15s}
.hb:hover{background:var(--bg)}
.hb:focus-visible{outline:2px solid var(--accent);outline-offset:3px}
.hb i{position:absolute;left:11px;width:22px;height:2px;border-radius:1px;background:var(--text);transition:transform .26s cubic-bezier(.3,.8,.2,1),opacity .2s}
.hb i:nth-child(1){top:14px}.hb i:nth-child(2){top:21px}.hb i:nth-child(3){top:28px}
.hb[aria-expanded=true] i:nth-child(1){transform:translateY(7px) rotate(45deg)}
.hb[aria-expanded=true] i:nth-child(2){opacity:0;transform:scaleX(0)}
.hb[aria-expanded=true] i:nth-child(3){transform:translateY(-7px) rotate(-45deg)}
.pn{position:absolute;left:0;top:calc(100% + 8px);min-width:160px;padding:6px;background:var(--surface);border:1px solid var(--line);border-radius:12px;box-shadow:0 12px 28px rgba(0,0,0,.12);animation:in .18s ease-out}
.pn[hidden]{display:none}
.pn a{display:block;padding:9px 12px;border-radius:8px;font-size:13.5px;color:inherit;text-decoration:none}
.pn a:hover,.pn a:focus-visible{background:var(--bg);outline:none}
@keyframes in{from{opacity:0;transform:translateY(-6px)}}
@media (prefers-reduced-motion:reduce){.hb,.hb i{transition:none}.pn{animation:none}}
</style></head>
<body><div class="stage">
<div class="wrap">
<button class="hb" id="hb" aria-label="Menu" aria-expanded="false" aria-controls="pn"><i></i><i></i><i></i></button>
<nav class="pn" id="pn" hidden><a href="#">Library</a><a href="#">Request</a><a href="#">About</a></nav>
</div>
</div>
<script>
const hb=document.getElementById('hb'),pn=document.getElementById('pn');
function set(o,f){hb.setAttribute('aria-expanded',String(o));pn.hidden=!o;if(!o&&f)hb.focus();}
hb.addEventListener('click',()=>set(pn.hidden,false));
document.addEventListener('keydown',e=>{if(e.key==='Escape'&&!pn.hidden)set(false,true);});
document.addEventListener('pointerdown',e=>{if(!pn.hidden&&!pn.contains(e.target)&&!hb.contains(e.target))set(false,false);});
pn.querySelectorAll('a').forEach(a=>a.addEventListener('click',e=>{e.preventDefault();set(false,true);}));
</script>
</body></html>