Sub topic 8
The priority menu
A priority menu (often called "Priority+") shows as many top-level links as fit in the available width. It moves the rest into a "More" disclosure. It adapts to the actual width of the container, not to a fixed breakpoint. This chapter shows how the pattern works internally, builds on the example from the topic plan, and covers the failure modes that usually appear in production.
This chapter assumes you know semantic nav markup, flexbox, and how ResizeObserver and DOM events work. It also assumes you read the earlier chapters on touch targets and accessible toggles. The aria-expanded and focus rules from the hamburger chapter apply here too.
When this pattern fits
A hamburger menu hides every link behind one button. A tab bar shows a few fixed destinations. A priority menu sits between them. It suits sites that have five to ten top-level links and a content-first layout, such as documentation sites, news sites, and marketing sites. The most important links are always visible, and wider screens show more of them.
It works best when the links are ordered by importance, because the last links are the first to move into "More". It works badly when every link matters equally, or when link labels are long and variable.
The working model
The pattern has three parts.
- A list of links in source order, which is also priority order.
- A measurement: does the list overflow its container?
- A move step: while the list overflows, take the last link out of the list and put it in the overflow list.
The overflow list lives inside a details element. details and summary give you a keyboard-operable disclosure with no JavaScript. The browser handles Enter and Space on the summary and exposes the open or closed state to assistive technology.
The CSS makes overflow measurable. The main list is a flex container with overflow: hidden and white-space: nowrap, so links never wrap and extra links are clipped instead of pushed onto a second line. The browser then reports scrollWidth (the full content width) and clientWidth (the visible width). If scrollWidth is larger, something is clipped.
The routine always resets first and then recomputes. It never tries to work out incrementally which links to move or restore. This costs a few extra DOM moves. In exchange, the result depends only on the current width, not on the history of resizes, so it cannot drift out of sync.
The full example
<nav class="priority" aria-label="Main">
<ul class="priority-list">
<li><a href="/products">Products</a></li>
<li><a href="/pricing">Pricing</a></li>
<li><a href="/docs">Docs</a></li>
<li><a href="/blog">Blog</a></li>
<li><a href="/about">About</a></li>
</ul>
<details class="priority-more" hidden>
<summary>More</summary>
<ul class="priority-overflow"></ul>
</details>
</nav>
<style>
.priority { display: flex; align-items: center; }
.priority-list {
display: flex;
flex: 1;
min-width: 0;
overflow: hidden;
white-space: nowrap;
list-style: none;
margin: 0;
padding: 0;
}
.priority-list a, .priority-overflow a { display: block; padding: 0.75rem 1rem; }
.priority-overflow { list-style: none; margin: 0; padding: 0; }
</style>
<script>
const nav = document.querySelector('.priority');
const list = nav.querySelector('.priority-list');
const more = nav.querySelector('.priority-more');
const overflow = nav.querySelector('.priority-overflow');
function update() {
while (overflow.firstChild) list.appendChild(overflow.firstChild);
more.hidden = true;
while (list.scrollWidth > list.clientWidth && list.children.length > 1) {
more.hidden = false;
overflow.prepend(list.lastElementChild);
}
}
new ResizeObserver(update).observe(nav);
update();
</script>Why each line is there
flex: 1on the list makes it take all the space the More control does not need. When More appears, the list shrinks by exactly the width of More, which is why the loop re-measures after showing it.min-width: 0lets the flex item shrink below its content width. Without it, a flex item's automatic minimum size equals its content, so the list never overflows. The loop would then see nothing to move, and the whole nav would overflow the page instead.overflow: hiddenclips what does not fit. It also means More must be a sibling of the list, not a child. Otherwise More would be clipped too.prependon the overflow list keeps the original order. The loop removes links from the end, so the last link is moved first. Prepending puts each newly moved link before the ones already there.list.children.length > 1is a guard. At very narrow widths, one link would be left in the main list, even if clipped, and the loop would not run forever. Chapter-level advice: below a narrow breakpoint, switch to a hamburger menu instead.new ResizeObserver(update).observe(nav)runsupdatewhen the nav's size changes, including the first observation afterobserve. The explicitupdate()call runs it immediately so that the first paint is correct.
A resize observer is better than a window resize listener. The nav's width can change without the window changing, for example when a sidebar opens, a font loads, or the nav sits in a resizable container.
Positioning the More dropdown
In the basic example, the open overflow list appears in normal flow and pushes the layout. In practice you want it to float below the More control. Add this CSS, then the extra script after it.
.priority-more {
position: relative;
flex: none;
}
.priority-more summary {
padding: 0.75rem 1rem;
cursor: pointer;
}
.priority-overflow {
position: absolute;
right: 0;
top: 100%;
min-width: 12rem;
background: #fff;
border: 1px solid #ccc;
z-index: 10;
}flex: none stops the More control from shrinking. position: relative on details gives the absolutely positioned list a reference box. The list does not affect scrollWidth of the main list, because it is not inside it.
more.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && more.open) {
more.open = false;
more.querySelector('summary').focus();
}
});
document.addEventListener('click', (event) => {
if (more.open && !more.contains(event.target)) {
more.open = false;
}
});A native details element does not close on Escape or on an outside click, so you add both. Escape returns focus to the summary, which matches the focus rule you used for the hamburger menu.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| No links ever move, and the page scrolls sideways | `min-width: 0` is missing, so the flex list never shrinks below its content width | Add `min-width: 0` to the list |
| More is clipped or invisible | More is inside the `overflow: hidden` list | Keep it as a sibling of the list |
| Links move too early or too late after the page loads | Web fonts loaded after the first measurement and changed link widths | Run `update` again when `document.fonts.ready` resolves |
| Links appear in the wrong order inside More | Using `append` instead of `prepend` when moving from the end | Use `prepend` |
| Flicker on first paint | Links render in full, then JavaScript moves some | Hide the nav until the first `update`, or accept the flash on slow scripts |
| The current page link is hidden and users lose their place | The link with `aria-current="page"` moved into More | Style the More summary when it contains the current page |
| Nav jumps or loops on resize | `update` changes the nav's own size, which triggers the observer again | Keep the nav height stable and avoid padding that depends on the number of links |
The font case is common enough to show.
if (document.fonts && document.fonts.ready) {
document.fonts.ready.then(update);
}For the current page problem, add this line at the end of update. It marks the More control when the current page is hidden inside it, so CSS can highlight it.
more.classList.toggle(
'has-current',
overflow.querySelector('[aria-current="page"]') !== null
);Then style .priority-more.has-current summary the same way as [aria-current="page"] links.
Accessibility notes
The structure stays simple, which helps. Every link is a real link inside a real list, whether it is in the main list or the overflow list. A screen reader announces one navigation landmark named "Main". The details element reports expanded or collapsed state without extra ARIA. Do not add aria-expanded to the summary yourself, because the browser already exposes it, and a manual copy can disagree with the real state.
Keep these points in mind.
- Source order is tab order. Tab moves through the visible links, then to More. When More is open, it continues into the overflow links. Moving links with
prependkeeps that order logical. - Links clipped by
overflow: hiddenbut still in the main list remain focusable. That is why the loop moves them out. A link that is visible to the DOM but clipped on screen creates a focus trap that the user cannot see. If you ever allow the last remaining link to clip, as thelength > 1guard does, switch to another pattern at that width. - The More summary is a touch target. Give it at least 48 pixels of height, as in the touch target chapter.
Priority menu and the other patterns
| Question | Priority menu | Hamburger menu | Tab bar |
|---|---|---|---|
| Visible links on a small screen | As many as fit | None | Three to five |
| Reacts to container width, not just viewport | Yes | No | No |
| Needs JavaScript to work | Yes, to move links | Yes, to toggle | No |
| Best for | Content sites with ordered links | Many links, low frequency | Few core app destinations |
Because the priority menu depends on JavaScript, plan for it failing. If the script does not run, the nav shows all links clipped inside the overflow: hidden list. A common fallback is to add the clipping CSS only after the script sets a class on the nav, so that without JavaScript the list wraps normally. You can do this by adding flex-wrap: wrap by default and removing it in a rule that applies to .priority.is-enhanced.
Exercise
A developer's priority menu never moves any link into More. At narrow widths the whole page scrolls sideways instead. The script is correct. Name the most likely CSS cause and the fix.
Show answer
The .priority-list flex item is missing min-width: 0. A flex item's automatic minimum width is its content width, so the list never shrinks below it, and scrollWidth never exceeds clientWidth. Adding min-width: 0 lets the list shrink, so the overflow check works and links move into More.
Exercise
Why does the update function move every link back to the main list before measuring, instead of moving one link in or out depending on whether the width grew or shrank?
Show answer
Resetting first makes the result depend only on the current width. Incremental logic must track the history of resizes and can drift out of sync, for example after a font loads or when several size changes arrive in one frame. Resetting costs a few extra DOM moves but removes that class of bug.
Exercise
A user is on the Docs page. At a narrow width the Docs link moves into More, and the nav shows no sign of where the user is. Describe a fix using the markup and script from this chapter.
Show answer
Add a line at the end of update that toggles a class on the More element when the overflow list contains a link with aria-current="page", such as more.classList.toggle('has-current', overflow.querySelector('[aria-current="page"]') !== null). Then style .priority-more.has-current summary like the active link, for example with bold text.