Sub topic 11
Structuring and laying out mega menu content
This chapter assumes you can write semantic HTML lists and links and use CSS Grid basics. It also assumes you know the hidden attribute and have read the chapters on navigation labels and when a mega menu is appropriate. Click, hover, and Escape behavior is covered in a later chapter. Here the focus is what goes inside the panel and how it is arranged.
What a mega menu panel contains
A mega menu panel is a large container of links that opens from one top-level item. It is only worth building when that item has more destinations than a simple dropdown can show clearly. The panel should help people scan, so it needs structure.
A good panel has three layers:
- A panel purpose, which is the top-level label that opened it (for example, "Products").
- Two to five groups, each with a short heading.
- Three to eight links per group, in a priority order.
If a group needs more than about eight links, the information architecture is probably wrong. Split the group, move the long tail to a landing page, or add a "View all" link at the end of the group.
Grouping content so people can scan it
Group by the user's task or by a clear category, not by your org chart. "Solutions for startups" makes sense to a visitor. "Division B offerings" does not.
Apply these rules to each group:
- Give every group a heading of two or three words, in the same grammatical style as the others (all nouns, for instance).
- Order groups by importance. In left-to-right languages, the first column gets the most attention.
- Keep link labels short and put the distinguishing word first. "Security overview" and "Security pricing" scan better than "Overview of security" and "Pricing for security".
- Never repeat the same link label in different groups unless the destination is the same page.
- Avoid a link that only repeats its group heading. Under "Solutions", use "Startups", not "Solutions for startups".
A group heading is usually plain text, not a link. If the heading also needs to lead somewhere, add a separate "View all" link in the list. Two controls with the same label and different targets confuse screen reader users who navigate by links list.
Marking up the panel
The panel is a container of sections. Each section has a heading and a list. This version adds a "View all" link and an optional featured area.
<nav aria-label="Primary">
<ul>
<li>
<button type="button" aria-expanded="false" aria-controls="products-panel">Products</button>
<div id="products-panel" class="mega-panel" hidden>
<section aria-labelledby="platform-heading">
<h3 id="platform-heading">Platform</h3>
<ul>
<li><a href="/platform/overview">Overview</a></li>
<li><a href="/platform/security">Security</a></li>
<li><a href="/platform">View all platform pages</a></li>
</ul>
</section>
<section aria-labelledby="solutions-heading">
<h3 id="solutions-heading">Solutions</h3>
<ul>
<li><a href="/solutions/startups">Startups</a></li>
<li><a href="/solutions/enterprise">Enterprise</a></li>
</ul>
</section>
<div class="mega-feature">
<p><strong>New: Audit logs</strong></p>
<p><a href="/platform/audit-logs">See what changed and who changed it</a></p>
</div>
</div>
</li>
<li><a href="/pricing">Pricing</a></li>
</ul>
</nav>Some decisions here are worth noting:
- Each group is a plain list. Screen readers announce "list, 3 items", which tells users how much is in the group before they enter it.
- The
sectionelements getaria-labelledbyso each group is a named region. This is optional, and the headings alone already give structure. Do not add it if it makes the page noisy with landmarks. Asectionwith an accessible name becomes a region landmark, so use it sparingly. - The featured block is not a heading and not a list. Keep promotional content to one item. It should never be the only way to reach a page.
- The heading level should fit the page outline. Here
h3is used because the page usesh1for the title and nothing else sits above the panel. Pick the level that doesn't skip levels from the surrounding context, and keep it consistent across all panels.
Laying out the panel with CSS Grid
CSS Grid fits mega menus well because the number of groups can differ between panels. auto-fit with minmax() lets the browser decide how many columns fit.
header {
position: relative;
}
.mega-panel {
position: absolute;
left: 0;
right: 0;
top: 100%;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
gap: 1.5rem;
padding: 1.5rem;
max-width: 72rem;
margin-inline: auto;
background: #fff;
border: 1px solid #ccc;
max-height: calc(100vh - 6rem);
overflow-y: auto;
}
.mega-panel[hidden] {
display: none;
}
.mega-panel h3 {
margin: 0 0 0.5rem;
font-size: 1rem;
}
.mega-panel ul {
margin: 0;
padding: 0;
list-style: none;
}
.mega-panel a {
display: block;
padding: 0.5rem 0;
}
.mega-feature {
grid-column: span 1;
background: #f3f6fb;
padding: 1rem;
}Here is what each part does:
position: relativeon the header gives the panel a reference box, soleft: 0; right: 0makes it span the header width. Do not putposition: relativeon theli, or the panel will shrink to the width of that item.minmax(12rem, 1fr)keeps columns from becoming too narrow to read. With a 72rem maximum width, you get up to six columns, fewer if the container is narrower.- The
[hidden]rule is required. Thehiddenattribute is only a defaultdisplay: nonefrom the browser stylesheet, and yourdisplay: gridrule overrides it. Without the second rule, the panel will always be visible. max-heightwithoverflow-y: autostops a tall panel from running past the viewport, which would make its lower links unreachable on short screens.display: blockon links with vertical padding gives each link a larger hit area. This helps touch users and anyone with limited motor control.
Handling uneven groups
Real content is rarely balanced. One group may have three links and another eight. Three options work:
- Accept uneven column heights. Aligning the tops with
align-items: startis usually enough. - Move the extra links into a "View all" landing page and keep each group to about five links.
- Let a large group span two columns with
grid-column: span 2, and only do this when the content inside also flows in two columns.
Do not use CSS columns to balance a single long list across columns. It changes the visual order from the DOM order for some layouts, and sighted keyboard users see focus jump unpredictably.
Common errors
| Problem | Cause | Fix |
|---|---|---|
| Panel always visible | `display: grid` overrides `hidden` | Add `.mega-panel[hidden] { display: none; }` |
| Panel shrinks to the trigger width | Parent `li` has `position: relative` | Position against the header instead |
| Links cut off on laptops | No height limit | Add `max-height` and `overflow-y: auto` |
| Screen reader hears "Products" twice | Group heading repeats the trigger label | Rename the group, such as "Platform" |
| Reading order differs from visual order | Reordering with `order` or `columns` | Reorder the HTML instead |
| Promo block hides real links | Feature takes the first column | Place it last, or in a final column |
Exercise
A mega menu panel uses display: grid and a hidden attribute, but it stays visible when the page loads. What is the cause, and what single CSS rule fixes it?
Show answer
The author rule display: grid has higher priority than the browser's built-in [hidden] { display: none; } rule, so the attribute no longer hides the panel. Add .mega-panel[hidden] { display: none; } so the hidden state wins again.
Exercise
A "Solutions" group contains 14 links under one heading. Give two ways to restructure it, and say why you would avoid splitting the list into two columns with CSS columns.
Show answer
Split it into two groups with their own headings (for example by audience and by industry), or keep the five or six most important links and add a "View all solutions" link to a landing page. CSS columns can make the visual order differ from the DOM order, so keyboard focus appears to jump between columns, and it hides the fact that the group is too large.