Sub topic 11
Structuring and laying out mega menu content
This chapter assumes you can write semantic HTML and CSS, know how the box model and CSS Grid work, and have built a basic nav with a disclosure button. It covers how to organize the content of a mega menu panel and lay it out. Click, hover, and Escape behavior comes in the next chapter.
Start from groups, not a long list
A mega menu panel is useful only when its links fall into clear groups. Take the labels you wrote earlier and sort them into three to five groups. Each group needs a short heading, and the group names should say what the links have in common, such as "Platform" or "Solutions". If you can't name a group in one or two words, the content probably needs a simpler menu.
Keep each group short. Five to seven links per group is a practical ceiling. Longer lists make the panel hard to scan and push content off small screens.
Markup: a button, a panel, and headed groups
The trigger is a button, not a link, because it opens a panel and doesn't navigate anywhere. Its aria-expanded attribute reports the state, and aria-controls points to the panel. Inside the panel, each group is a section with a heading and a list of links.
<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>
<h3>Platform</h3>
<ul>
<li><a href='/platform/overview'>Overview</a></li>
<li><a href='/platform/security'>Security</a></li>
</ul>
</section>
<section>
<h3>Solutions</h3>
<ul>
<li><a href='/solutions/startups'>Startups</a></li>
<li><a href='/solutions/enterprise'>Enterprise</a></li>
</ul>
</section>
</div>
</li>
<li><a href='/pricing'>Pricing</a></li>
</ul>
</nav>Use real headings so screen reader users can jump between groups. Choose the heading level to fit the page's outline. Here h3 assumes the page already uses h1 and h2 above it. Don't pick a level for its default size, because you can restyle it.
Lay out the panel with grid
CSS Grid fits this job because the number of columns can follow the available width instead of a fixed breakpoint.
.mega-panel {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
gap: 1.5rem;
padding: 1.5rem;
}
.mega-panel[hidden] {
display: none;
}repeat(auto-fit, minmax(12rem, 1fr)) creates as many columns of at least 12rem as fit, then shares the leftover space. On a narrow screen the groups stack into one column with no media query.
The [hidden] rule matters. The browser hides elements with the hidden attribute through a weak default display: none. Your display: grid rule overrides it, so without the second rule the panel would always be visible.
Layout decisions
- Order groups by importance, left to right in reading order, so the visual order matches the DOM order. Don't use
orderor grid placement to move groups away from their source order, because keyboard focus follows the DOM. - Keep every link a text link. Descriptions or icons can support a link but should not replace its label.
- Limit the panel's width and height. Content that scrolls inside a panel is easy to miss and hard to use with touch.
Exercise
A developer sets .mega-panel { display: grid; } and finds the panel visible on page load, even though the HTML has the hidden attribute. Why does this happen, and how do you fix it?
Show answer
The author rule display: grid overrides the browser's default style that hides [hidden] elements. Fix it by adding .mega-panel[hidden] { display: none; }.