Lay out the menu bar
The bar is a row of items. Each item is
position: relativeso its panel can hang from it, andalign-self: stretchmakes the item as tall as the bar. That second line matters later: without it there is a strip of bar under each item where the pointer is over nothing, and the panel closes.
Stretching each item removes the dead strip under it. mega-menu.cssCSS .mega { display: flex; gap: 28px; height: 64px; } .mega-item { position: relative; display: flex; align-items: center; align-self: stretch; }Hide each panel, with a delayed close
Each panel sits under its item, hidden with
opacity: 0andvisibility: hiddenand nudged up a few pixels. The transitions carry the timing: when a panel closes, the fade waits 0.15s andvisibilityonly switches off at 0.5s. Those numbers are what forgive a pointer that slips off the edge for a moment.mega-menu.cssCSS .mega-panel { position: absolute; top: 100%; left: 0; width: min(560px, 86vw); opacity: 0; visibility: hidden; transform: translateY(-10px) scale(0.985); transform-origin: 50% 0; transition: opacity 0.2s ease 0.15s, transform 0.45s cubic-bezier(0.16, 1, 0.3, 1) 0.15s, visibility 0s linear 0.5s; }After step 2: a plain bar. Both panels are hidden and nothing opens yet; step 3 adds the hover. The W3C's accessibility tutorial on fly-out menus recommends the same idea:
to slightly delay the immediate closing of submenu items
W3C Web Accessibility Initiative, Fly-out Menus tutorial Their tutorial does it with a script timer; here the delay lives in
transitionvalues, so it needs no JavaScript at all. The effect for the visitor is the one the W3C describes: a pointer that strays for a fraction of a second does not lose the menu.Open it on hover and keyboard focus
:hoveropens a panel for a pointer and:focus-withinopens it for a keyboard, because tabbing to the item's link focuses something inside the item. Settingtransition-delay: 0shere means opening is instant even though closing waits.mega-menu.cssCSS .mega-item:hover > .mega-panel, .mega-item:focus-within > .mega-panel { opacity: 1; visibility: visible; transform: none; transition-delay: 0s; }After step 3: point at Lab or Elements and the panel opens. Bridge the gap between item and panel
While an item is hovered, its
::afterpseudo-element covers the 40px below it, so the pointer is still over the item while it crosses the gap. When I first tested this menu, it closed before I could get my pointer into a panel. The first bridge sat inside the panel, and the panel'soverflowclipped it. Moving it onto the item, and making items full height in step 1, is what made the path work, and it was checked point by point with a real pointer, not keyboard focus.
The bridge keeps the pointer inside the item on its way down to the panel. mega-menu.cssCSS .mega-item:hover::after { content: ""; position: absolute; left: 0; right: 0; top: 100%; height: 40px; }After step 4: move from Lab down into the panel; it stays open all the way. Stagger the panel contents in
Each direct child of the panel starts slightly lower and transparent, and arrives when the panel opens. Small delays on the second and third children give the reveal its rhythm. Only
opacityandtransformanimate, which keeps it smooth.mega-menu.cssCSS .mega-panel > * { opacity: 0; transform: translateY(10px); transition: opacity 0.4s ease, transform 0.5s cubic-bezier(0.16, 1, 0.3, 1); } .mega-item:hover .mega-panel > *, .mega-item:focus-within .mega-panel > * { opacity: 1; transform: none; } .mega-panel > *:nth-child(2) { transition-delay: 0.06s; } .mega-panel > *:nth-child(3) { transition-delay: 0.12s; }After step 5: the links rise in one after another as the panel opens. Add Escape and aria-expanded with a short script
CSS cannot close a panel that has keyboard focus inside it, and it cannot tell a screen reader whether a panel is open. This script does both:
aria-expandedfollows the panel, and Escape closes the panel and puts focus back on its top-level link. It runs withdefer, so it never delays the page.
The script handles Escape and tells screen readers the state. mega-menu.jsJavaScript document.querySelectorAll('.mega-item').forEach((item) => { const link = item.querySelector('.mega-link'); if (!link) return; const expanded = (open) => link.setAttribute('aria-expanded', String(open)); expanded(false); item.addEventListener('mouseenter', () => expanded(true)); item.addEventListener('mouseleave', () => { item.classList.remove('is-closed'); expanded(item.contains(document.activeElement)); }); item.addEventListener('focusin', () => { if (!item.classList.contains('is-closed')) expanded(true); }); item.addEventListener('focusout', (event) => { if (item.contains(event.relatedTarget)) return; item.classList.remove('is-closed'); expanded(false); }); }); document.addEventListener('keydown', (event) => { if (event.key !== 'Escape') return; const item = document.activeElement && document.activeElement.closest('.mega-item'); if (!item) return; item.classList.add('is-closed'); const link = item.querySelector('.mega-link'); if (link) { link.focus({ preventScroll: true }); link.setAttribute('aria-expanded', 'false'); } });Adrian Roselli, who has written about menus and accessibility for years, puts the reason for keeping it simple bluntly:
A typical web site is not an application.
Adrian Roselli, "Don't Use ARIA Menu Roles for Site Nav" That is why this menu is plain links with
aria-expandedand nothing else: norole="menu", no arrow-key trapping. Writing this guide made me check the live site against it, and I removed anaria-haspopupattribute from this site's own menu script, which would have told screen readers to expect an application menu.Close on Escape, hide on phones, calm the motion
The
is-closedclass from the script overrides:focus-withinuntil focus leaves the item. Below 768px the panels are hidden, because a hover panel is the wrong pattern for a thumb, and reduced motion switches off every transition.mega-menu.cssCSS .mega-item.is-closed > .mega-panel { opacity: 0 !important; visibility: hidden !important; } @media (max-width: 767px) { .mega-panel { display: none; } } @media (prefers-reduced-motion: reduce) { .mega-panel, .mega-panel > * { transition: none; } }The finished menu: point at Lab or Elements and its panel opens. On a phone-width screen the panels stay hidden. Build the structure in Elementor 4
In the Elementor Pro Theme Builder header, the menu bar is a Flexbox with the class
mega. Each item is a Div Block with the classmega-item, holding a Paragraph with a link (classmega-link) and a Div Block for the panel (classmega-panel) with the links inside. Elementor 4's style panel cannot target::afteror child selectors, so the CSS above goes in one Custom Code snippet in<head>and the script in another at the end of<body>.The frosted look comes from the frosted header guide. The markup outside Elementor is this:

One div block per menu item, holding its link and its panel. index.htmlHTML <nav class="mega" aria-label="Main"> <div class="mega-item"> <a class="mega-link" href="/lab/">Lab</a> <div class="mega-panel"> <a href="/lab/">All build guides</a> <a href="/lab/type/css/">CSS guides</a> <a href="/lab/type/elementor/">Elementor guides</a> </div> </div> <div class="mega-item"> <a class="mega-link" href="/elements/">Elements</a> <div class="mega-panel"> <a href="/elements/">The element gallery</a> <a href="/design-system/">Design system</a> </div> </div> </nav>
What this CSS mega menu does not do
It is a desktop pattern. On phones the panels are hidden and the top-level links go straight to their pages; a phone menu needs its own design.
Keyboard users open a panel by tabbing to its link, and the panel's links follow in the tab order. It does not add arrow-key navigation, which suits site navigation but not an application menu.
Large panels with many elements add to the page's DOM. This site's five panels are 378 elements, which is why its pages carry a large header even when the menu is closed.
All the CSS mega menu code
Each file below is complete. Copy it, or download it under the name shown.
.mega {
display: flex;
gap: 28px;
height: 64px;
}
.mega-item {
position: relative;
display: flex;
align-items: center;
align-self: stretch;
}
.mega-panel {
position: absolute;
top: 100%;
left: 0;
width: min(560px, 86vw);
opacity: 0;
visibility: hidden;
transform: translateY(-10px) scale(0.985);
transform-origin: 50% 0;
transition:
opacity 0.2s ease 0.15s,
transform 0.45s cubic-bezier(0.16, 1, 0.3, 1) 0.15s,
visibility 0s linear 0.5s;
}
.mega-item:hover > .mega-panel,
.mega-item:focus-within > .mega-panel {
opacity: 1;
visibility: visible;
transform: none;
transition-delay: 0s;
}
.mega-item:hover::after {
content: "";
position: absolute;
left: 0;
right: 0;
top: 100%;
height: 40px;
}
.mega-panel > * {
opacity: 0;
transform: translateY(10px);
transition: opacity 0.4s ease, transform 0.5s cubic-bezier(0.16, 1, 0.3, 1);
}
.mega-item:hover .mega-panel > *,
.mega-item:focus-within .mega-panel > * {
opacity: 1;
transform: none;
}
.mega-panel > *:nth-child(2) { transition-delay: 0.06s; }
.mega-panel > *:nth-child(3) { transition-delay: 0.12s; }
.mega-item.is-closed > .mega-panel {
opacity: 0 !important;
visibility: hidden !important;
}
@media (max-width: 767px) {
.mega-panel { display: none; }
}
@media (prefers-reduced-motion: reduce) {
.mega-panel,
.mega-panel > * { transition: none; }
}document.querySelectorAll('.mega-item').forEach((item) => {
const link = item.querySelector('.mega-link');
if (!link) return;
const expanded = (open) => link.setAttribute('aria-expanded', String(open));
expanded(false);
item.addEventListener('mouseenter', () => expanded(true));
item.addEventListener('mouseleave', () => {
item.classList.remove('is-closed');
expanded(item.contains(document.activeElement));
});
item.addEventListener('focusin', () => {
if (!item.classList.contains('is-closed')) expanded(true);
});
item.addEventListener('focusout', (event) => {
if (item.contains(event.relatedTarget)) return;
item.classList.remove('is-closed');
expanded(false);
});
});
document.addEventListener('keydown', (event) => {
if (event.key !== 'Escape') return;
const item = document.activeElement && document.activeElement.closest('.mega-item');
if (!item) return;
item.classList.add('is-closed');
const link = item.querySelector('.mega-link');
if (link) {
link.focus({ preventScroll: true });
link.setAttribute('aria-expanded', 'false');
}
});<nav class="mega" aria-label="Main">
<div class="mega-item">
<a class="mega-link" href="/lab/">Lab</a>
<div class="mega-panel">
<a href="/lab/">All build guides</a>
<a href="/lab/type/css/">CSS guides</a>
<a href="/lab/type/elementor/">Elementor guides</a>
</div>
</div>
<div class="mega-item">
<a class="mega-link" href="/elements/">Elements</a>
<div class="mega-panel">
<a href="/elements/">The element gallery</a>
<a href="/design-system/">Design system</a>
</div>
</div>
</nav>CSS mega menu questions
Do I need a mega menu plugin for Elementor?
No. This menu is Elementor 4 atomic elements, a CSS file and a short script, with no menu widget or plugin.
Why does my mega menu close before I reach the panel?
There is a gap between the item and the panel where the pointer is over nothing. Bridge it with the item's ::after (step 4), make items full height and delay the close (step 2).
Can keyboard users open the panels?
Yes. :focus-within opens a panel when its link has focus, and Escape closes it again.
Should a site menu use role="menu"?
No. Site navigation is a set of links, not an application menu, so it uses links and aria-expanded only.
Does it work on mobile?
The panels are hidden below 768px and the top-level links work as normal links. Phones need their own menu pattern.
Is the animation bad for performance?
It animates only opacity and transform, which browsers handle smoothly. This site scores 100 for performance on mobile with this menu.
Why is the script needed if the menu is CSS?
CSS opens and closes the panels. The script adds the two things CSS cannot: Escape to close, and aria-expanded for screen readers.
Can I add a glow to the panels?
Yes. A soft coloured box-shadow lifts them off the page. Box Shadow Generator, made by Mark McNeece of 365i, builds one you can copy.