Hover Mega Menu
A header whose items open a two-column panel on hover, focus or tap, with Escape to close.
AI prompt
Create a header navigation bar with three items: Products, Resources and Company. Each item is a button with aria-expanded and aria-controls that opens a 300 pixel wide panel directly beneath it containing two columns of links with small uppercase headings (for example Build, Ship for Products). Open on mouse hover with a 150 millisecond close delay, and also on focus, Enter, Space and tap; Escape closes and returns focus to the trigger; Arrow Down moves into the panel. Only one panel is open at a time. Light minimal style, white panel, 12 pixel radius, soft shadow, 160 millisecond fade-drop with a prefers-reduced-motion fallback. Plain HTML, CSS and JavaScript.About this pattern
Definition
A mega menu is a large dropdown panel under a navigation item that groups many links into labelled columns, so people can scan a whole section at once.
Purpose & UX principles
- Best for sites with many sections; group links under short headings and keep each panel to about six links.
- Open on hover with a small delay, but also on focus, Enter and tap so keyboard and touch users are not excluded.
- Close on Escape, on moving away, or when another item opens; keep a gap-free hover path between trigger and panel.
Interaction states
- Closed, open (one panel at a time), keyboard focus on trigger, focus inside the panel.
Motion specification
- Panel fades and drops 6px over 160ms; closing is delayed 150ms to forgive small pointer slips.
- Reduced motion: instant show and hide.
Responsive behaviour
- Under about 800px replace it with a hamburger and an accordion list.
- Do not open on hover on touch devices; use tap to toggle.
Code
<div class="bx"><nav class="nv" id="nv" aria-label="Main"><button data-i="0" aria-expanded="false">Products</button><button data-i="1" aria-expanded="false">Resources</button><button data-i="2" aria-expanded="false">Company</button><div class="pn" id="pn" hidden></div></nav><p class="out" id="out" aria-live="polite">Hover, focus or tap an item.</p></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}
.bx{width:min(340px,100%);height:210px;position:relative}
.nv{position:relative;display:flex;gap:2px;padding:6px;border-radius:99px;background:var(--surface);border:1px solid var(--line);width:max-content}
.nv>button{height:30px;padding:0 14px;border:0;background:none;border-radius:99px;font-size:13px;cursor:pointer}
.nv>button[aria-expanded=true],.nv>button:hover{background:#eeeeea}
.pn{position:absolute;top:calc(100% + 8px);left:0;width:300px;padding:14px;border-radius:12px;background:var(--surface);border:1px solid var(--line);box-shadow:0 12px 32px rgba(0,0,0,.12);display:grid;grid-template-columns:1fr 1fr;gap:14px;animation:dr .16s ease-out;z-index:3}
.pn[hidden]{display:none}
@keyframes dr{from{opacity:0;transform:translateY(-6px)}}
.pn h3{font-size:10.5px;letter-spacing:.08em;text-transform:uppercase;color:var(--muted);margin-bottom:6px}
.pn a{display:block;padding:5px 6px;margin:0 -6px;border-radius:6px;font-size:13px;color:var(--text);text-decoration:none}.pn a:hover{background:#f1f1ee}
.out{margin-top:calc(100% - 26px);font-size:12px;color:var(--muted)}
button:focus-visible,a:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.pn{animation:none}}const M=[[['Build',['Components','Templates','Icons']],['Ship',['Deploy','Monitor','Billing']]],[['Learn',['Guides','Articles','Videos']],['Help',['Docs','Community','Status']]],[['About',['Story','Team','Careers']],['Contact',['Press','Support','Sales']]]];
const nv=document.getElementById('nv'),pn=document.getElementById('pn'),bs=[...nv.querySelectorAll('button')],out=document.getElementById('out');let cur=-1,tm;
function open(i){clearTimeout(tm);if(cur===i)return;cur=i;bs.forEach((b,k)=>b.setAttribute('aria-expanded',String(k===i)));pn.hidden=false;pn.id='pn';pn.innerHTML=M[i].map(c=>'<div><h3>'+c[0]+'</h3>'+c[1].map(l=>'<a href="#" onclick="return false">'+l+'</a>').join('')+'</div>').join('');bs[i].setAttribute('aria-controls','pn');}
function close(){clearTimeout(tm);cur=-1;pn.hidden=true;bs.forEach(b=>b.setAttribute('aria-expanded','false'));}
const canHover=matchMedia('(hover:hover)').matches;
bs.forEach((b,i)=>{b.addEventListener('click',()=>{cur===i?close():open(i);});b.addEventListener('focus',()=>{if(canHover)open(i);});if(canHover)b.addEventListener('pointerenter',()=>open(i));
b.addEventListener('keydown',e=>{if(e.key==='ArrowDown'&&cur===i){e.preventDefault();const a=pn.querySelector('a');a&&a.focus();}});});
nv.addEventListener('pointerleave',()=>{if(canHover){tm=setTimeout(close,150);}});nv.addEventListener('pointerenter',()=>clearTimeout(tm));
nv.addEventListener('focusout',e=>{if(!nv.contains(e.relatedTarget))close();});
document.addEventListener('keydown',e=>{if(e.key==='Escape'&&cur>=0){const b=bs[cur];close();if(nv.contains(document.activeElement))b.focus();}});
pn.addEventListener('click',e=>{const a=e.target.closest('a');if(a){out.textContent='Go to: '+a.textContent;close();}});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>Hover Mega Menu — 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}}
.bx{width:min(340px,100%);height:210px;position:relative}
.nv{position:relative;display:flex;gap:2px;padding:6px;border-radius:99px;background:var(--surface);border:1px solid var(--line);width:max-content}
.nv>button{height:30px;padding:0 14px;border:0;background:none;border-radius:99px;font-size:13px;cursor:pointer}
.nv>button[aria-expanded=true],.nv>button:hover{background:#eeeeea}
.pn{position:absolute;top:calc(100% + 8px);left:0;width:300px;padding:14px;border-radius:12px;background:var(--surface);border:1px solid var(--line);box-shadow:0 12px 32px rgba(0,0,0,.12);display:grid;grid-template-columns:1fr 1fr;gap:14px;animation:dr .16s ease-out;z-index:3}
.pn[hidden]{display:none}
@keyframes dr{from{opacity:0;transform:translateY(-6px)}}
.pn h3{font-size:10.5px;letter-spacing:.08em;text-transform:uppercase;color:var(--muted);margin-bottom:6px}
.pn a{display:block;padding:5px 6px;margin:0 -6px;border-radius:6px;font-size:13px;color:var(--text);text-decoration:none}.pn a:hover{background:#f1f1ee}
.out{margin-top:calc(100% - 26px);font-size:12px;color:var(--muted)}
button:focus-visible,a:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.pn{animation:none}}
</style></head>
<body><div class="stage">
<div class="bx"><nav class="nv" id="nv" aria-label="Main"><button data-i="0" aria-expanded="false">Products</button><button data-i="1" aria-expanded="false">Resources</button><button data-i="2" aria-expanded="false">Company</button><div class="pn" id="pn" hidden></div></nav><p class="out" id="out" aria-live="polite">Hover, focus or tap an item.</p></div>
</div>
<script>
const M=[[['Build',['Components','Templates','Icons']],['Ship',['Deploy','Monitor','Billing']]],[['Learn',['Guides','Articles','Videos']],['Help',['Docs','Community','Status']]],[['About',['Story','Team','Careers']],['Contact',['Press','Support','Sales']]]];
const nv=document.getElementById('nv'),pn=document.getElementById('pn'),bs=[...nv.querySelectorAll('button')],out=document.getElementById('out');let cur=-1,tm;
function open(i){clearTimeout(tm);if(cur===i)return;cur=i;bs.forEach((b,k)=>b.setAttribute('aria-expanded',String(k===i)));pn.hidden=false;pn.id='pn';pn.innerHTML=M[i].map(c=>'<div><h3>'+c[0]+'</h3>'+c[1].map(l=>'<a href="#" onclick="return false">'+l+'</a>').join('')+'</div>').join('');bs[i].setAttribute('aria-controls','pn');}
function close(){clearTimeout(tm);cur=-1;pn.hidden=true;bs.forEach(b=>b.setAttribute('aria-expanded','false'));}
const canHover=matchMedia('(hover:hover)').matches;
bs.forEach((b,i)=>{b.addEventListener('click',()=>{cur===i?close():open(i);});b.addEventListener('focus',()=>{if(canHover)open(i);});if(canHover)b.addEventListener('pointerenter',()=>open(i));
b.addEventListener('keydown',e=>{if(e.key==='ArrowDown'&&cur===i){e.preventDefault();const a=pn.querySelector('a');a&&a.focus();}});});
nv.addEventListener('pointerleave',()=>{if(canHover){tm=setTimeout(close,150);}});nv.addEventListener('pointerenter',()=>clearTimeout(tm));
nv.addEventListener('focusout',e=>{if(!nv.contains(e.relatedTarget))close();});
document.addEventListener('keydown',e=>{if(e.key==='Escape'&&cur>=0){const b=bs[cur];close();if(nv.contains(document.activeElement))b.focus();}});
pn.addEventListener('click',e=>{const a=e.target.closest('a');if(a){out.textContent='Go to: '+a.textContent;close();}});
</script>
</body></html>