Sub topic 10
Combining patterns across breakpoints
Earlier chapters built three patterns on their own: the hamburger menu, the bottom tab bar, and the priority menu. Real sites rarely use just one. This chapter shows how to switch patterns as the screen grows, and how to do it without duplicating markup or breaking accessibility.
This chapter assumes you have read the earlier chapters and are comfortable with flexbox, min-width media queries, and basic DOM scripting.
Why combine patterns
A single pattern fits a single range of screen widths.
- A tab bar suits phones, but it looks odd stretched across a 1400px monitor.
- A full horizontal nav suits desktops, but it overflows on a phone.
- A hamburger menu works at any width, but hiding every link behind a button costs discoverability on screens that have room.
So you choose a pattern for each width range and let CSS switch between them. The goal is one set of links in the DOM, presented differently at each breakpoint.
A three-tier plan
A common split looks like this:
| Width | Primary links (3 to 5 key destinations) | Secondary links (Help, About, Contact) |
|---|---|---|
| Phone, under 48em | Fixed bottom tab bar | Hamburger menu in the header |
| 48em and wider | Inline in the header | Inline in the header |
You could add a middle tier where a priority menu handles the secondary links. The mechanism is the same: each tier changes presentation only, while the markup stays put.
One markup, two layouts
Use two nav elements, one per group of links. Give each its own aria-label, so screen reader users can tell the landmarks apart.
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<header class="site-header">
<a href="/" class="brand">Site name</a>
<button id="menu-toggle" type="button" aria-expanded="false" aria-controls="secondary-menu">Menu</button>
<nav class="secondary" aria-label="Secondary">
<ul id="secondary-menu" hidden>
<li><a href="/help">Help</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
<nav class="primary" aria-label="Primary">
<a href="/home" aria-current="page">Home</a>
<a href="/search">Search</a>
<a href="/orders">Orders</a>
<a href="/account">Account</a>
</nav>
</header>The CSS is mobile-first. The base rules build the phone layout, and one min-width query rebuilds it for wide screens.
body {
margin: 0;
padding-bottom: calc(48px + env(safe-area-inset-bottom));
}
.primary {
position: fixed;
left: 0;
right: 0;
bottom: 0;
display: flex;
background: #fff;
border-top: 1px solid #ccc;
padding-bottom: env(safe-area-inset-bottom);
}
.primary a {
flex: 1;
min-height: 48px;
display: flex;
align-items: center;
justify-content: center;
}
.primary a[aria-current="page"] {
font-weight: bold;
}
#secondary-menu {
list-style: none;
margin: 0;
padding: 0;
}
#secondary-menu a {
display: block;
padding: 0.75rem 1rem;
}
@media (min-width: 48em) {
body {
padding-bottom: 0;
}
.site-header {
display: flex;
align-items: center;
gap: 1rem;
}
.primary {
position: static;
order: 1;
margin-right: auto;
border-top: 0;
padding-bottom: 0;
}
.primary a {
flex: none;
min-height: 0;
padding: 0.75rem 1rem;
}
.secondary {
order: 2;
}
#menu-toggle {
display: none;
}
#secondary-menu,
#secondary-menu[hidden] {
display: flex;
gap: 1rem;
}
}Two details matter here. The body padding stops the fixed tab bar from covering the last lines of content on phones. The [hidden] override keeps the secondary list visible on wide screens, even if the script left it hidden.
Keeping state in sync
CSS handles layout, but the toggle's state lives in JavaScript and in aria-expanded. Suppose a user opens the menu on a phone, then rotates to landscape or resizes a window. The button disappears, but aria-expanded="true" stays set. When the screen narrows again, the menu opens by itself.
Reset the state when the breakpoint changes. Use matchMedia with the same value as your CSS query.
const toggle = document.getElementById('menu-toggle');
const menu = document.getElementById('secondary-menu');
const wide = window.matchMedia('(min-width: 48em)');
function closeMenu() {
toggle.setAttribute('aria-expanded', 'false');
menu.hidden = true;
}
toggle.addEventListener('click', () => {
const isOpen = toggle.getAttribute('aria-expanded') === 'true';
toggle.setAttribute('aria-expanded', String(!isOpen));
menu.hidden = isOpen;
});
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && !menu.hidden) {
closeMenu();
toggle.focus();
}
});
wide.addEventListener('change', closeMenu);Common errors
- Duplicated link lists. Copying the links into a desktop nav and a mobile nav, then hiding one with
display: none, doubles the markup. Every link change then has to be made twice, and the copies drift apart. - Unlabeled landmarks. Two
navelements with noaria-labelboth announce as "navigation", and screen reader users cannot tell them apart. - Fixed bar over content. Forgetting the bottom padding on
bodyhides the last content behind the tab bar. - Missing safe-area support. Without
viewport-fit=coverin the viewport tag,env(safe-area-inset-bottom)has no effect, and the tab bar can sit under the home indicator on some phones.
Exercise
A user opens the hamburger menu on a phone, then rotates the device so the viewport becomes wider than 48em, then rotates back. The script has no matchMedia listener. What does the user see after rotating back, and how do you fix it?
Show answer
The menu is still open, and aria-expanded is still "true", because nothing reset the state while the toggle button was hidden. The user sees an open menu they never reopened. Fix it by adding a matchMedia('(min-width: 48em)') change listener that sets aria-expanded to "false" and menu.hidden to true.