Test365i
365i.co.uk
Back to all guides
Layout

An animated CSS mega menu from Elementor 4 atomic elements

A CSS mega menu opens a large panel of links under each top-level item, on hover or keyboard focus, with no menu plugin. This guide builds the one at the top of this site: five panels, 378 Elementor 4 atomic elements, a few dozen lines of CSS and a short script for Escape and screen readers.

45 minutesCustom codeElementorElementor Pro

Works on WordPress 7.2, Elementor 4.4.2 and Elementor Pro 4.4.2 (Theme Builder header); every current browser

Editorial integrity: read our editorial standards.

Live demo, running the code from this guide on a bare page with no theme styles.
  1. Lay out the menu bar

    The bar is a row of items. Each item is position: relative so its panel can hang from it, and align-self: stretch makes 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.

    Without align-self stretch there is a strip under each item where the panel closes; with it there is no gap
    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;
    }
  2. Hide each panel, with a delayed close

    Each panel sits under its item, hidden with opacity: 0 and visibility: hidden and nudged up a few pixels. The transitions carry the timing: when a panel closes, the fade waits 0.15s and visibility only 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 transition values, 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.

  3. Open it on hover and keyboard focus

    :hover opens a panel for a pointer and :focus-within opens it for a keyboard, because tabbing to the item's link focuses something inside the item. Setting transition-delay: 0s here 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.
  4. Bridge the gap between item and panel

    While an item is hovered, its ::after pseudo-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's overflow clipped 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 item's ::after bridges the gap to the panel; a timeline shows instant opening and a delayed close
    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.
  5. 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 opacity and transform animate, 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.
  6. 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-expanded follows the panel, and Escape closes the panel and puts focus back on its top-level link. It runs with defer, so it never delays the page.

    Tab into Lab sets aria-expanded true, Escape closes the panel and focus returns to Lab with aria-expanded false
    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-expanded and nothing else: no role="menu", no arrow-key trapping. Writing this guide made me check the live site against it, and I removed an aria-haspopup attribute from this site's own menu script, which would have told screen readers to expect an application menu.

  7. Close on Escape, hide on phones, calm the motion

    The is-closed class from the script overrides :focus-within until 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.
  8. 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 class mega-item, holding a Paragraph with a link (class mega-link) and a Div Block for the panel (class mega-panel) with the links inside. Elementor 4's style panel cannot target ::after or 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:

    A flexbox menu bar holding one div block per item, each with a link and a panel of links
    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-menu.cssCSS
.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; }
}
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');
	}
});
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>

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.

Sources

  1. W3C WAI: Fly-out Menus tutorial
  2. Adrian Roselli: Don't Use ARIA Menu Roles for Site Nav
  3. MDN Web Docs: :focus-within

More build guides

The latest guides, newest first.

Elementor

A clickable card grid with the Elementor 4 Collection loop

The Elementor 4 Collection loop repeats one design for every post a query finds. On this site it lists the build guides, which are a JetEngine post type, in the element gallery, the Lab and the mega menu, all from one card component. This guide builds that grid, makes each whole card clickable and hides fields that come back empty.

25 minutes Get the guide
Elementor

Style the Elementor 4 Carousel element: arrows, dots and loop

The Elementor 4 Carousel element gives you slides, arrows and dots as separate atomic parts. Out of the box on this site, the arrows sat on top of the slide titles, the active dot was black on a dark panel and Loop did nothing. This guide fixes all three, with the CSS the element gallery now runs.

20 minutes Get the guide
Elementor

The Elementor 4 Accordion element, built on native details

The Elementor 4 Accordion element is built on the browser's own details and summary elements, so it opens and closes with no script. This guide sets it up the way the element gallery on this site does, styles it with classes, and explains the FAQ schema switch and what Google now does with it.

15 minutes Get the guide