Sub topic 13
Mega menu accessibility pitfalls
This chapter assumes you can build the disclosure-style mega menu from the earlier chapters. That means a button with aria-expanded and aria-controls, a hidden panel of grouped links, and a small click handler. It also assumes you know basic keyboard focus behavior. A menu that works with a mouse can still fail for keyboard, screen reader, touch, and zoom users. This chapter covers the failures that show up most often and how to fix each one.
Pitfall 1: Using menu roles for site navigation
The most common mistake is adding role="menubar", role="menu", and role="menuitem" because the widget is called a "menu". Those roles describe application menus, like the File menu in a desktop editor. They change how assistive technology behaves:
- Screen readers often switch into a mode that passes keystrokes straight to the page.
- Users expect arrow-key navigation and typeahead, which you would then have to build.
- Links inside the panel stop being announced as ordinary links.
A website's navigation is a list of links. Keep the nav landmark, the ul lists, and the disclosure button. Also leave out aria-haspopup="true", which tells users that a menu will appear.
Pitfall 2: Opening only on hover
A panel that opens on :hover cannot be reached by keyboard or touch users. If you add hover for mouse users, treat it as an enhancement on top of the button. WCAG 1.4.13 (Content on Hover or Focus) sets three rules for content that appears on hover or focus:
- Dismissible. Users can close it without moving the pointer, usually with Escape.
- Hoverable. The pointer can move from the trigger onto the panel without the panel vanishing.
- Persistent. It stays visible until the user dismisses it or moves away.
The usual hover failure is a gap of a few pixels between the button and the panel. The pointer crosses the gap, hover ends, and the panel closes. Remove the gap with padding on the trigger, or add a short close delay. The simplest approach is to open on click only. It works the same for mouse, keyboard, and touch, and it avoids accidental opening when the pointer crosses the bar.
Pitfall 3: State that drifts out of sync
A panel can be visible while aria-expanded="false", or hidden while it says "true". A screen reader then announces the wrong state. Update the attribute and the panel's visibility in one function, as the spine's setOpen does, and never in two places.
A related trap is CSS that overrides the hidden attribute. The rule .mega-panel { display: grid; } has higher precedence than the browser's built-in [hidden] rule, so the panel stays visible. Always add the explicit rule:
.mega-panel {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
gap: 1.5rem;
padding: 1.5rem;
max-height: calc(100vh - 6rem);
overflow-y: auto;
}
.mega-panel[hidden] {
display: none;
}The max-height and overflow-y lines address a separate problem. At 400% zoom, or on a short landscape phone, a tall panel can run off the screen and its lower links become unreachable. Letting the panel scroll keeps every link reachable.
Also avoid hiding a closed panel with opacity: 0 or by moving it off-screen. Those links stay in the tab order, so keyboard users tab through invisible links. hidden and display: none remove them from the tab order and from the accessibility tree.
Pitfall 4: Focus handling
Three focus rules prevent most keyboard problems:
- Do not move focus when a panel opens. Opening a disclosure should leave focus on the button. The next Tab press then enters the panel, because the panel follows the button in the DOM.
- Do not trap focus. A navigation panel is not a modal dialog. Tab must be able to leave it.
- Return focus after Escape. If focus was inside the panel when the user pressed Escape, move it back to the button. Otherwise it lands on a hidden element and the user is sent back to the top of the page.
The spine's Escape handler always calls button.focus(), even when focus was elsewhere on the page. That pulls focus away from, say, a search box, which is its own bug. The version below checks where focus is first. It also handles several panels, closes on outside click, and closes when focus leaves the panel.
const triggers = document.querySelectorAll('nav button[aria-controls]');
function panelFor(button) {
return document.getElementById(button.getAttribute('aria-controls'));
}
function setOpen(button, open) {
button.setAttribute('aria-expanded', String(open));
panelFor(button).hidden = !open;
}
function closeAll(except) {
triggers.forEach((button) => {
if (button !== except) setOpen(button, false);
});
}
triggers.forEach((button) => {
const panel = panelFor(button);
button.addEventListener('click', () => {
const willOpen = button.getAttribute('aria-expanded') !== 'true';
closeAll(button);
setOpen(button, willOpen);
});
panel.addEventListener('focusout', (event) => {
const next = event.relatedTarget;
if (next && !panel.contains(next) && next !== button) {
setOpen(button, false);
}
});
});
document.addEventListener('keydown', (event) => {
if (event.key !== 'Escape') return;
triggers.forEach((button) => {
if (button.getAttribute('aria-expanded') !== 'true') return;
const focusWasInside = panelFor(button).contains(document.activeElement);
setOpen(button, false);
if (focusWasInside) button.focus();
});
});
document.addEventListener('click', (event) => {
if (!event.target.closest('nav')) closeAll();
});The focusout handler checks relatedTarget, the element receiving focus. It closes the panel only when focus moves to a real element outside the panel and the button. Without that check, the panel would close in the middle of Shift+Tab back to the button.
Pitfall 5: Triggers that are also links
Many sites make "Products" both a link to /products and a hover trigger for the panel. On a touch screen, a tap cannot do both. The result is either a page load that skips the panel or a panel with no way to reach the landing page.
Pick one behavior for each top-level item. If the item opens a panel, make it a button. Put the landing page inside the panel as its first link, such as "Products overview". If the item is only a destination, make it a plain link and give it no panel.
Pitfall 6: Heading and landmark noise
The spine's panel uses h3 for group titles. That works only if the page's heading outline makes sense. A navigation panel that exposes headings competes with the page's real headings in a screen reader's heading list. Keep the group titles short and keep their level consistent across every panel. Do not use headings just for visual size. If the groups are short, a bold span or a list label is a reasonable alternative.
Use only one nav landmark per distinct set of links, and give each its own aria-label, such as "Primary" and "Footer". Do not wrap each panel in its own labeled nav. Users would then hear a long list of landmarks for one menu.
Pitfall summary
| Pitfall | What the user experiences | Fix |
|---|---|---|
| `role="menu"` on site navigation | Screen reader mode changes, arrow-key expectations are unmet | Use `nav`, lists, and disclosure buttons |
| Hover-only opening | Keyboard and touch users cannot open the panel | Open on click, add hover as an extra |
| Gap between trigger and panel | Panel closes while the pointer crosses | Remove the gap or add a short delay |
| `aria-expanded` out of sync | Wrong state announced | Update attribute and `hidden` in one function |
| CSS overrides `hidden` | Panel is always visible | Add `[hidden] { display: none; }` |
| Closed panel hidden by opacity | Invisible tab stops | Use `hidden` or `display: none` |
| Focus lost after Escape | Focus resets to page top | Return focus to the button |
| Panel taller than viewport | Links clipped at zoom or on short screens | Set `max-height` and `overflow-y: auto` |
| Trigger is also a link | Touch taps do the wrong thing | Use a button and link the landing page inside |
Quick checks before you ship
Run these in about five minutes with only the keyboard:
- Press Tab until the trigger gets focus. Is the outline clearly visible?
- Press Enter and Space. Does the panel open on both, with focus still on the button?
- Press Tab. Does focus move to the first link in the panel?
- Press Escape from inside the panel. Does focus return to the button?
- Open the panel, then Tab past its last link. Does the panel close?
- Zoom to 400% and repeat. Can you reach every link?
Exercise
A mega menu's "Products" button opens a panel on hover and on click. Its Escape handler closes the panel and always calls button.focus(). A user presses Escape while typing in the site search box, with the panel open, and their cursor jumps out of the search field. What is wrong, and how do you fix it?
Show answer
The handler moves focus to the button no matter where focus was. Check whether the panel contains document.activeElement before closing, and call button.focus() only when it does. In the fixed code, focusWasInside = panel.contains(document.activeElement) gates the focus call, so Escape in the search box closes the panel without taking focus.
Exercise
A developer sets .mega-panel { display: grid; } and hides the panel with the hidden attribute. The panel is visible on page load. Why, and what single rule fixes it?
Show answer
Author CSS for display overrides the browser's default [hidden] styling, so the attribute no longer hides the element. Add .mega-panel[hidden] { display: none; }.