Flexbox in Practice: Navigation Bars, Media Objects, Sticky Footers and Forms
Common flexbox layouts built from this part: navigation bars, media objects, input groups, cards with footers, a page footer that stays at the bottom, a sidebar that wraps without media queries, a row that switches to a column, and app shells.
kvi5hnuCode Sprout team member8 min readOn this page
The previous four articles covered the properties. This one puts them together into the patterns you'll write again and again. Each is short, works in every browser, and needs no media queries unless noted. Most of them rely on three ideas:
gapfor spacing (article 63);flexvalues that let one item take the free space (article 62);min-width: 0so content can shrink (article 62).
Navigation bar
A logo at the start, links at the end, wrapping on small screens:
<header class="nav">
<a class="logo" href="/">Shelf</a>
<nav><ul class="links">
<li><a href="/new">New</a></li><li><a href="/events">Events</a></li><li><a href="/basket">Basket</a></li>
</ul></nav>
</header>
.nav { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem 1.5rem; }
.nav nav { margin-inline-start: auto; } /* push the links to the end */
.links { display: flex; flex-wrap: wrap; gap: 0.25rem 1rem; list-style: none; margin: 0; padding: 0; }
When the screen is too narrow, the links' list wraps under the logo, and its own links wrap if they need to. The list keeps its list semantics, which tells screen reader users how many links there are.
Media object
An image or avatar beside text, the shape of comments, notifications and search results:
.media { display: flex; gap: 0.75rem; align-items: start; }
.media img { flex: none; inline-size: 3rem; block-size: 3rem; border-radius: 50%; }
.media .body { flex: 1; min-width: 0; } /* the text takes the rest and can wrap */
flex: none keeps the image from being squashed, align-items: start keeps it at the top instead of stretched, and min-width: 0 lets long words in the body wrap instead of pushing the layout wide.
Input group
A text field with a button attached, the field taking the remaining width:
.search { display: flex; }
.search input { flex: 1; min-width: 0; border-start-end-radius: 0; border-end-end-radius: 0; }
.search button { flex: none; border-start-start-radius: 0; border-end-start-radius: 0; }
An <input> has a default width from its size attribute, so without min-width: 0 the group can overflow small screens.
body { font: 0.85rem system-ui, sans-serif; margin: 1rem; }
.w { display: block; margin-bottom: 0.5rem; }
.frame { inline-size: var(--w, 18rem); display: grid; gap: 1rem; border: 2px dashed #d8cfc0; border-radius: 8px; padding: 0.6rem; }
.nav { display: flex; flex-wrap: wrap; align-items: center; gap: 0.4rem 1.5rem; }
.nav nav { margin-inline-start: auto; }
.links { display: flex; flex-wrap: wrap; gap: 0.25rem 1rem; list-style: none; margin: 0; padding: 0; }
.logo { font-weight: 700; color: #8a3b12; font-size: 1.1rem; text-decoration: none; }
.links a { color: #2b2118; }
.media { display: flex; gap: 0.75rem; align-items: start; }
.media .avatar {
flex: none; inline-size: 2.5rem; block-size: 2.5rem; border-radius: 50%;
display: grid; place-items: center; background: #8a3b12; color: white; font-weight: 700;
}
.media .body { flex: 1; min-width: 0; }
.search { display: flex; }
.search input {
flex: 1; min-width: 0; font: inherit; padding: 0.45rem 0.6rem;
border: 1px solid #8a3b12; border-radius: 6px 0 0 6px;
}
.search button {
flex: none; font: inherit; padding: 0.45rem 0.8rem; border: 1px solid #8a3b12; border-inline-start: 0;
border-radius: 0 6px 6px 0; background: #8a3b12; color: white;
}
Narrow the frame: the links wrap below the logo, the review text wraps beside the avatar, and the search field shrinks while the button keeps its size.

Each broken measurement on the left maps to one missing declaration: flex-wrap, flex: none, min-width: 0 with overflow-wrap, and min-width: 0 on the input.
Card with a footer at the bottom
Cards in a row stretch to the same height (article 63). Making each card a column and pushing the last part down lines up their buttons:
.cards { display: flex; flex-wrap: wrap; gap: 1rem; }
.card { flex: 1 1 14rem; display: flex; flex-direction: column; gap: 0.5rem; }
.card .footer { margin-block-start: auto; } /* takes the free space above it */
Footer at the bottom of the page
On short pages, the footer should sit at the bottom of the window, not halfway up:
body { min-block-size: 100dvh; display: flex; flex-direction: column; margin: 0; }
body > main { flex: 1; } /* main takes the spare height */
Tested in Chrome: in a 400px-tall column with a header, a short main and a footer, the footer's bottom edge is at 400px. On long pages, the body is taller than the screen and the footer simply follows the content. Use min-block-size, not block-size, so the page can grow; dvh follows mobile browsers' changing toolbars (article 25).
A sidebar that wraps without a media query
A sidebar beside the main content when there's room, above it when there isn't:
.with-sidebar { display: flex; flex-wrap: wrap; gap: 1rem; }
.with-sidebar > aside { flex: 1 1 14rem; } /* ideally 14rem */
.with-sidebar > main { flex: 999 1 0; min-inline-size: 60%; } /* takes almost all the growth */
How it works: main starts from 0 and grows 999 times faster than the sidebar, so the sidebar stays close to 14rem. But main must be at least 60% of the container: when the container is too narrow for both 60% and the sidebar's 14rem, they can't share a line, so they wrap, and each fills the full width. Tested in Chrome: at 720px, the aside is about 200px and main about 504px side by side (with a 200px basis and a 16px gap); at 400px, both are 400px wide, stacked.
The breakpoint depends on the container, not the viewport, so the same component works in a narrow column and a wide page. Container queries (article 78) are the explicit alternative.
A row that switches to a column
Equal items in a row, switching all at once to a column below a threshold, instead of wrapping one at a time:
.switcher { display: flex; flex-wrap: wrap; gap: 1rem; }
.switcher > * { flex: 1 1 calc((30rem - 100%) * 999); }
The basis is huge and negative when the container is wider than 30rem, which counts as 0, so the items share the row equally. When the container is narrower, the basis is huge and positive, so each item needs a full line. Tested in Chrome with a 480px threshold: in 720px, with the 1rem gap, three items are about 229px each in a row; in 400px, they're stacked at 400px each. It's a trick, with an unreadable formula; a container query is clearer when you can use one.

The sidebar here uses a 100px basis instead of 14rem so the switch happens at a small size; the switcher's threshold is 300px.
Full-height app shell
A header and footer of natural height, with a scrolling middle:
.app { block-size: 100dvh; display: flex; flex-direction: column; }
.app main { flex: 1; overflow: auto; }
Because main scrolls, its automatic minimum height is 0, so it can be shorter than its content. The trap is a scroller nested one level deeper, such as a chat window whose main is itself a column holding a header and a scrolling message list:
.app main { flex: 1; display: flex; flex-direction: column; min-block-size: 0; } /* needed */
.app .messages { flex: 1; overflow: auto; }
Here main doesn't scroll, so its automatic minimum height is its content's height. Tested in Chrome: in a 200px app with a one-line header and footer, 18px each, without min-block-size: 0 on main a 500px message list pushes the footer down to 518px (18 + 500); with it, the footer stays at 182px (200 − 18) and the list scrolls (article 62).
Button groups and toolbars
.actions { display: flex; flex-wrap: wrap; gap: 0.5rem; justify-content: flex-end; }
.actions .danger { margin-inline-end: auto; } /* "Delete" alone at the start, others at the end */
.toolbar { display: flex; gap: 0.25rem; overflow-x: auto; } /* one scrolling line, for many small buttons */
When to reach for grid instead
Flexbox patterns fall apart when you start fighting for columns: cards whose widths should line up across rows, form labels in a column, or a page layout with named regions. Those are grid's job, starting with the next article. A good rule: if you're calculating widths to make items line up with other rows, use grid.
What's current
- Every pattern here works in all browsers;
gapin flexbox (2021) was the last piece. dvhand the other dynamic viewport units are Baseline (article 25).- Container queries (article 78) can replace the wrapping tricks with explicit breakpoints based on the component's size.
Common questions
Why does my footer float in the middle of short pages?
Nothing makes the page fill the window. Make body a column with min-block-size: 100dvh and give main flex: 1.
Why does my scrolling panel push the footer off the screen?
The panel is inside a flex item that doesn't scroll, whose minimum height is its content's height. Add min-block-size: 0 to that item.
Why does my search input overflow on phones?
Inputs have an intrinsic width. Add min-width: 0 to the input, with flex: 1.
Should I use flexbox or grid for a card list? Flexbox if each row's cards can have their own widths; grid if cards should line up in columns.
Summary
- Navigation bars:
margin-inline-start: autopushes the links to the end;flex-wraphandles small screens. - Media objects and input groups:
flex: nonefor the fixed part,flex: 1; min-width: 0for the rest. - Cards: column flex with
margin-block-start: autoon the footer. - Page footer:
bodyas a column withmin-block-size: 100dvh,mainwithflex: 1. - Wrapping sidebars and switchers respond to their container without media queries; container queries are the clearer modern alternative.
- App shells:
overflow: autoon the scrolling middle, andmin-block-size: 0on any flex item between it and the shell.
Practice questions
Write CSS for a notification with an icon on the left, the text in the middle taking the remaining space, and a close button on the right.
Answer
.notice { display: flex; align-items: start; gap: 0.75rem; }
.notice .icon,
.notice .close { flex: none; }
.notice .text { flex: 1; min-width: 0; }
In the CSS below, a long message list pushes the footer off the screen instead of scrolling. Fix it.
.app { display: flex; flex-direction: column; height: 100dvh; }
.app main { flex: 1; display: flex; flex-direction: column; }
.app .messages { flex: 1; overflow: auto; }
Answer
.app main { min-block-size: 0; }
main doesn't scroll, so its automatic minimum height is its content's height. min-block-size: 0 lets it shrink, so .messages gets a limited height and scrolls.
In the wrapping sidebar pattern below, what happens if you remove min-inline-size: 60%?
.with-sidebar > aside { flex: 1 1 14rem; }
.with-sidebar > main { flex: 999 1 0; }
Answer
They wrap far too late. main's basis is 0, so they share a line until the sidebar's 14rem plus main's longest word no longer fit, and before that main gets squeezed down to its longest word. The minimum width is what forces the wrap at a sensible point.
Card buttons don't line up because the cards have different amounts of text. The cards are already flex items in a row. What two rules fix it?
Answer
Make each card a column, .card { display: flex; flex-direction: column; }, and push the button down with .card .button { margin-block-start: auto; }. The row already stretches the cards to equal heights.
Review quiz
Test yourself on Part 9 with the CSS Part 9 review quiz.
Previous: Visual Order and Accessibility: order, reading-flow and Reversed Directions
Next: Grid Basics: Grid Containers, Tracks, Lines, Cells and the fr Unit

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.