Sub topic 10
Combining patterns across breakpoints
This chapter assumes you have built a hamburger menu, a bottom tab bar, and a priority menu from the earlier chapters. You should also be comfortable with aria-expanded, media queries, and matchMedia. Here you will make those patterns work together in one navigation that changes shape as the screen grows.
Why one pattern is rarely enough
Each pattern fits a range of screen widths and a range of link counts.
- A tab bar suits a phone held in one hand.
- A priority menu suits a tablet, where there is space for most links but not all of them.
- A plain horizontal list suits a desktop.
A site that must work on all three has to switch patterns as the viewport changes. The switch is a design decision, so it goes through the same selection criteria as the single-pattern decision you made earlier: how many items there are, how important each one is, and how often people use it.
The risk is that a combined navigation becomes three navigations glued together. Links get duplicated, the menu state gets out of sync, and screen reader users hear the same list twice. The rest of this chapter shows how to avoid that.
Two rules for combined navigation
Keep one source of links
Do not write a desktop <nav> and a separate mobile <nav> and hide one with CSS. Duplicated markup means every new page must be added in two places, and the two lists drift apart. Write the links once and change their presentation with CSS.
Content hidden with display: none is removed from the accessibility tree, so a hidden duplicate would not be read aloud. The maintenance cost is still enough reason to avoid it.
If you do need two landmarks on one page, give each a distinct aria-label. Two navigations both labelled "Main" are indistinguishable in a screen reader's landmark list.
Keep one source of state
A menu has a state: open or closed. That state lives in two places, aria-expanded on the button and hidden on the list. Update both together in a single function, and call that function from every code path. The paths are click, Escape, and breakpoint change.
If one path updates only one of the two attributes, a screen reader will announce a menu as "expanded" while it is closed, or the reverse.
Choosing breakpoints
Pick breakpoints from the content, not from device names. Resize the page and note the width where the layout stops working, for example where the horizontal list starts to wrap. Use that width, in em, as the breakpoint.
| Viewport | Pattern | Why |
|---|---|---|
| Below 48em | Bottom tab bar with a More panel | Thumb reach, four or five main destinations |
| 48em and above | Horizontal list in the page flow | Enough room for every link, no toggle needed |
You can add a third tier. Between the two, a priority menu keeps as many links visible as fit and moves the rest into More. Add that tier only if you have real content that falls into the gap. Each extra tier is one more layout to test.
The diagram shows the two-tier version built below.
Worked example: tab bar to horizontal nav
The navigation has four primary destinations and three secondary ones. On phones, the primary links become tab bar items. The secondary links stay in a panel that opens from a More button at the end of the bar. On wide screens, all seven links sit in one row and the More button disappears.
Markup
The viewport tag includes viewport-fit=cover so that env(safe-area-inset-bottom) takes effect on devices with a home indicator. There is one <nav> and two lists.
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<nav class="site-nav" aria-label="Main">
<ul class="nav-primary">
<li><a href="/home" aria-current="page">Home</a></li>
<li><a href="/search">Search</a></li>
<li><a href="/orders">Orders</a></li>
<li><a href="/account">Account</a></li>
<li class="nav-more-item">
<button id="more-toggle" type="button" aria-expanded="false" aria-controls="more-menu">More</button>
</li>
</ul>
<ul id="more-menu" class="nav-secondary" hidden>
<li><a href="/help">Help</a></li>
<li><a href="/shipping">Shipping</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>The More button sits in the primary list because on phones it is a tab bar item. It uses the same aria-expanded and aria-controls pairing as the hamburger toggle.
CSS
The base rules are the phone layout, written mobile first. The media query turns the nav into a row.
body {
padding-bottom: calc(48px + env(safe-area-inset-bottom));
}
.site-nav ul {
list-style: none;
margin: 0;
padding: 0;
}
.site-nav a,
.site-nav button {
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
width: 100%;
min-height: 48px;
padding: 0 1rem;
font: inherit;
color: inherit;
background: none;
border: 0;
text-decoration: none;
}
.site-nav a[aria-current="page"] {
font-weight: bold;
}
.site-nav {
position: fixed;
left: 0;
right: 0;
bottom: 0;
background: #fff;
border-top: 1px solid #ccc;
padding-bottom: env(safe-area-inset-bottom);
}
.nav-primary {
display: flex;
}
.nav-primary li {
flex: 1;
}
.nav-secondary {
position: absolute;
bottom: 100%;
left: 0;
right: 0;
background: #fff;
border-top: 1px solid #ccc;
}
.nav-secondary a {
justify-content: flex-start;
}
@media (min-width: 48em) {
body {
padding-bottom: 0;
}
.site-nav {
position: static;
display: flex;
border-top: 0;
padding-bottom: 0;
}
.nav-primary li {
flex: none;
}
.nav-more-item {
display: none;
}
.nav-secondary,
.nav-secondary[hidden] {
position: static;
display: flex;
border-top: 0;
}
.nav-secondary a {
justify-content: center;
}
}Some details to notice:
- The
bodypadding equals the bar height plus the safe area inset. Without it, the fixed bar covers the last 48px of page content. .nav-secondaryhas nodisplayrule below 48em, so the browser's built-in[hidden]rule applies and the panel stays closed until JavaScript opens it.position: absolute; bottom: 100%places the panel directly above the bar. The fixed.site-navis the positioning context, so no extra wrapper is needed.- At 48em the rule
.nav-secondary[hidden]forces the list visible even though thehiddenattribute is still set. Section titles of the earlier hamburger chapter used the same trick.
JavaScript
The script handles three events: a click on the button, the Escape key, and a change of breakpoint. All three go through setOpen.
const toggle = document.getElementById('more-toggle');
const menu = document.getElementById('more-menu');
const wide = window.matchMedia('(min-width: 48em)');
function isOpen() {
return toggle.getAttribute('aria-expanded') === 'true';
}
function setOpen(open) {
toggle.setAttribute('aria-expanded', String(open));
menu.hidden = !open;
}
toggle.addEventListener('click', () => {
setOpen(!isOpen());
});
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && isOpen()) {
setOpen(false);
toggle.focus();
}
});
wide.addEventListener('change', () => {
setOpen(false);
});What happens when the breakpoint changes
The change listener on the matchMedia object is the part people forget. Consider a tablet that rotates while the More panel is open.
Without the listener, the layout switches to the row, but the button still reports aria-expanded="true". The button is now hidden, so nothing can reset it. When the tablet rotates back, the panel is open again on its own.
Resetting to the closed state on every change keeps the CSS and the attributes in agreement. Use the same value in the media query and in matchMedia. If the two disagree, there is a width range where the CSS shows one layout and the script assumes the other.
Focus when the layout changes
Resetting the state can also strand keyboard focus. If focus sits on a link in the open panel and the viewport widens, the link is still there and focus stays on it. That is fine. Focus is only lost when the focused element disappears, for example when focus is on the More button and that button becomes display: none. Browsers then move focus to the document body.
For this navigation that is an acceptable fallback, because the user is resizing the window and is unlikely to be mid-task. If your layout hides an element that commonly holds focus, move focus to a visible element in the change handler.
Gotchas
- Stacking order. A fixed bar and a fixed cookie banner both want the bottom edge. Give each a deliberate
z-indexand leave room for both in the body padding. - The on-screen keyboard. On some mobile browsers a fixed bottom bar rides up above the keyboard and covers a form field. Test every page that has a text input.
- Order consistency. Keep the link order the same in every layout. People build muscle memory for position, and a link that moves between phone and tablet is harder to find.
- Label consistency. Use the same words at every breakpoint. "More" on a phone and "Other" on a tablet are two names for one thing.
- Active state.
aria-current="page"must be on the correct link whichever list it is in. Check secondary links too.
Checking the result
Test each layout, and test the transition between them.
- At phone width, tab through the bar. Focus should move through the four links and the More button in visual order.
- Press Enter on More. The panel should open and a screen reader should announce "expanded".
- Press Escape. The panel should close and focus should return to More.
- Open the panel, then widen the window past 48em. The row should appear and the More button should vanish.
- Narrow the window again. The panel should be closed and the button should say "collapsed".
- On a phone with a home indicator, confirm the bar sits above the indicator and the page's last line is visible above the bar.
Exercise
Why must the code that responds to a breakpoint change call the same setOpen function as the click handler, rather than only changing the CSS?
Show answer
The open state is stored in two attributes, aria-expanded on the button and hidden on the list. CSS cannot change attributes. If only the layout changes, the button can keep reporting "expanded" while it is hidden, and the menu reopens by itself when the viewport narrows again. Calling setOpen(false) updates both attributes together, so the state stays consistent in every path.
Exercise
A tablet layout needs a third tier between 36em and 48em, where all seven links nearly fit in a row. Name the pattern you would use, the first rule you must keep from this chapter, and one thing in the script that must change.
Show answer
Use a priority menu, which keeps as many links visible as fit and moves the rest into More. The rule to keep is a single source of links: reuse the same lists and restyle them instead of adding a second nav. The script must run the overflow calculation only inside the tier's media query, for example by checking a second matchMedia for the range, and must return all links to the list when the viewport leaves that range. The breakpoint-change handler must also reset the open state.