Sidebar layouts
Overview
Sidebars are everywhere: filters next to search results, a table of contents next to docs, account navigation next to settings. The challenge is always the same — side by side when there’s room, stacked when there isn’t. There are two good approaches, and a third technique for sticky sidebars.
Approach 1: grid and a media query
The straightforward version:
.with-sidebar {
display: grid;
gap: 2rem;
}
@media (width >= 50rem) {
.with-sidebar { grid-template-columns: 16rem minmax(0, 1fr); }
}It’s clear and predictable. The limitation: it responds to the viewport. If the same component sits in a narrow column on a wide screen, it still tries to show the sidebar. You could switch to a container query (@container (width >= 50rem)) to fix that — or use the next approach.
Approach 2: the flex-wrap sidebar trick
This technique needs no media or container queries. The layout decides for itself when to wrap.
.with-sidebar {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.sidebar {
flex-basis: 15rem; /* its ideal width */
flex-grow: 1;
}
.content {
flex-basis: 0;
flex-grow: 999; /* takes virtually all the extra space */
min-inline-size: 60%; /* …but must be at least 60% wide */
}How it works:
- When there’s room for both, the content’s enormous
flex-growtakes almost all the free space. The sidebar stays at about 15rem (it grows by 1/1000 of the free space — a pixel or two). - The content must be at least 60% of the container. When the container is too narrow for 15rem + gap + 60%, the two can’t fit on one line, so flexbox wraps the content onto its own line.
- Once wrapped, each item is alone on its line, and
flex-growmakes both of them full width.
You tune it with two numbers: the sidebar’s flex-basis (its width side by side) and the content’s min-inline-size (how narrow the content may get before stacking). You saw a version of this in Wrapping and gap; it’s worth having as a reusable class.
Because it measures the container, it works wherever you put it: in a full page, in a card, in a modal.
Sidebar on the right
Put the content first in the HTML and the sidebar second — no other change. When it wraps, the sidebar drops below the content, which is often the right order on mobile (filters after results, related links after the article).
Sticky sidebars
A table of contents or a filter panel is more useful if it stays visible while you scroll. That’s position: sticky (from Fixed and sticky), with one important extra:
.toc {
position: sticky;
top: 1rem;
align-self: start; /* or align-self: flex-start */
}In a flex row or grid row, items stretch to the height of the tallest item by default. A sidebar stretched to the full height of a long article has no room to stick — it’s already as tall as its container. align-self: start makes it only as tall as its own content, so it can slide down as you scroll.
If the sidebar might be taller than the viewport, cap its height and let it scroll on its own:
.toc {
max-block-size: calc(100dvh - 2rem);
overflow-y: auto;
}When the layout wraps and the sidebar sits above the article, it would stick to the top and cover the article as it scrolls past. Only make it sticky in the side-by-side layout. With the media-query approach, put the sticky rules inside the query. With the flex-wrap trick, make the wrapper a container and work out the width at which it wraps — the sidebar basis plus the gap must fit next to the 60% minimum, so with a 15rem basis and a 2rem gap that’s 17rem ÷ 0.4 = 42.5rem:
.with-sidebar { container-type: inline-size; }
@container (width >= 42.5rem) {
.toc { position: sticky; top: 1rem; }
}Collapsible sidebars
For app-style sidebars that collapse to icons, change the grid track and let the content follow:
.app { display: grid; grid-template-columns: 16rem minmax(0, 1fr); transition: grid-template-columns 0.2s; }
.app.is-collapsed { grid-template-columns: 4rem minmax(0, 1fr); }Grid track sizes can animate in modern browsers, so the sidebar slides closed smoothly. Respect prefers-reduced-motion by turning the transition off for people who’ve asked for less movement.
Which approach?
| Situation | Approach |
|---|---|
| Page-level sidebar, exact breakpoint wanted | grid + media query |
| Reusable component, used at different widths | flex-wrap trick |
| Needs extra behaviour when side by side (sticky) | grid + media or container query |
| App navigation that collapses | grid with an animated track |
Remember
align-self: startwhenever a sidebar is sticky — it’s the most common reason “sticky doesn’t work” in sidebars.
A docs page with a sticky table of contents
Build .docs with the flex-wrap sidebar technique (no media queries): a .toc sidebar with a 15rem basis and a .article that takes the rest, wrapping to stack when the article would be narrower than 60% of the container. Then make the .toc stick 1rem from the top of the viewport while you scroll through the long article — but only when the two sit side by side. Work out the container width at which the layout wraps (15rem + the 2rem gap + 60%) and use a container query; remember align-self too.
<div class="docs">
<nav class="toc"><a href="#a">Intro</a><a href="#b">Setup</a><a href="#c">Usage</a></nav>
<article class="article">
<h2 id="a">Intro</h2><p style="block-size: 60rem">Long section…</p>
<h2 id="b">Setup</h2><p style="block-size: 60rem">Long section…</p>
</article>
</div>
<style>
* { box-sizing: border-box; }
.toc { background: #eaf0fc; padding: 1rem; display: grid; gap: 0.5rem; }
/* your code here */
</style><div class="docs">
<nav class="toc"><a href="#a">Intro</a><a href="#b">Setup</a><a href="#c">Usage</a></nav>
<article class="article">
<h2 id="a">Intro</h2><p style="block-size: 60rem">Long section…</p>
<h2 id="b">Setup</h2><p style="block-size: 60rem">Long section…</p>
</article>
</div>
<style>
* { box-sizing: border-box; }
.toc { background: #eaf0fc; padding: 1rem; display: grid; gap: 0.5rem; }
.docs {
display: flex;
flex-wrap: wrap;
gap: 2rem;
container-type: inline-size;
}
.toc {
flex-basis: 15rem;
flex-grow: 1;
align-self: start; /* don't stretch to the article's height */
}
/* Side by side when 15rem + 2rem + 60% of W fits in W: W >= 17rem / 0.4 = 42.5rem */
@container (width >= 42.5rem) {
.toc {
position: sticky;
top: 1rem;
}
}
.article {
flex-basis: 0;
flex-grow: 999;
min-inline-size: 60%;
}
</style>Summary
- The simplest responsive sidebar is a grid column added in a media query.
- The flex-wrap sidebar trick uses a sidebar basis, a huge
flex-growon the content, and amin-inline-sizepercentage to decide when to wrap. - Because it responds to its container, the trick works in any width — no breakpoints.
- A sticky sidebar needs
align-self: startso it isn’t stretched to the full height of the row. - Put the sidebar first in the HTML only if it should come first for keyboard and screen-reader users.