Flexbox patterns
Overview
Flexbox is at its best for components: small, one-dimensional groups of things whose sizes come from their content. This lesson collects the patterns you’ll use again and again. Each is only a few lines once you know the pieces from the previous lessons.
Navigation bar
<header class="site-header">
<a class="logo" href="/">Academy</a>
<nav class="links">
<a href="/courses">Courses</a>
<a href="/pricing">Pricing</a>
</nav>
<a class="button" href="/signup">Sign up</a>
</header>.site-header {
display: flex;
align-items: center;
gap: 1.5rem;
padding: 1rem;
}
.links { display: flex; gap: 1rem; }
.site-header .button { margin-inline-start: auto; }align-items: centerlines up items of different heights (a big logo, small links).- The auto margin pushes the button to the far end while keeping the logo and links together.
flex-wrap: wrapon.site-headerstops it overflowing on small screens (a full mobile menu is covered in Navigation layouts).
Media object
An image (avatar, thumbnail, icon) next to a block of text — comments, notifications, search results:
.media { display: flex; gap: 1rem; align-items: flex-start; }
.media > img { flex: none; width: 4rem; }
.media > .body { flex: 1; min-width: 0; }flex: nonekeeps the image exactly its size — it neither grows nor gets squashed by long text.align-items: flex-startstops the image stretching to the height of the text.min-width: 0lets the text shrink and wrap properly, even with long words or nested flex rows.
Swap the image to the other side with flex-direction: row-reverse only if reading order doesn’t matter; otherwise move it in the HTML.
Input group
A text field with attached buttons, like a search box or a “copy link” field:
.input-group { display: flex; }
.input-group input { flex: 1; min-width: 0; }
.input-group button { flex: none; }
/* join the borders */
.input-group > :not(:first-child) { margin-inline-start: -1px; }
.input-group > :first-child { border-start-end-radius: 0; border-end-end-radius: 0; }
.input-group > :last-child { border-start-start-radius: 0; border-end-start-radius: 0; }Button groups and toolbars
.actions {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
justify-content: flex-end; /* buttons at the end, like a dialog's footer */
}Wrapping lets the group survive narrow screens and longer translations.
Page with a sticky footer
On short pages, a footer should sit at the bottom of the window, not halfway up. Make the page a column and let the main content grow:
body {
min-height: 100dvh;
display: flex;
flex-direction: column;
}
main { flex: 1; }When content is short, main grows to fill the space and pushes the footer down. When content is long, the page simply scrolls. (Grid offers an equally short version — see Page layouts.)
Equal-height cards with bottom-aligned actions
Flex items in a row stretch to equal height by default. Make each card a column and push its footer down:
.cards { display: flex; flex-wrap: wrap; gap: 1rem; }
.card {
flex: 1 1 16rem;
display: flex;
flex-direction: column;
}
.card .actions { margin-block-start: auto; }All the buttons line up along the bottom, whatever the length of each card’s text. (For card titles that line up too, you’ll want grid with subgrid — see Subgrid.)
Icon and text
Buttons, links and labels with icons:
.button {
display: inline-flex;
align-items: center;
gap: 0.5em;
}
.button svg { flex: none; width: 1.25em; height: 1.25em; }Using em for the gap and icon size keeps them proportional when the button’s font size changes.
Split header
A heading with an action at the other end, where the action wraps under the heading on narrow screens:
.section-header {
display: flex;
flex-wrap: wrap;
align-items: baseline;
justify-content: space-between;
gap: 0.5rem 1rem;
}align-items: baseline lines up a large heading and a small “View all” link by their text, which looks far better than centring them.
Pattern spotting: when you see a row (or a column) of things that should share space, align, or wrap, think flexbox. When you see a grid of things that should line up in both directions, think grid — next chapter.
A comment component
Build a .comment media object: a 3rem round avatar on the left that never shrinks, and a body that fills the remaining space. The body has a header row with the author name and a timestamp pushed to the right (both aligned on their text baseline), the comment text, and an .actions row of buttons with 0.5rem gaps. Long unbroken text (like a URL) must wrap instead of overflowing the comment.
<article class="comment">
<img class="avatar" src="https://i.pravatar.cc/96?img=12" alt="" width="48" height="48" />
<div class="body">
<header class="meta">
<strong class="author">Asha Rao</strong>
<time class="time">2 hours ago</time>
</header>
<p>Grid finally clicked for me — this demo helped: https://example.com/a-really-long-link-that-keeps-going-and-going</p>
<div class="actions"><button>Reply</button><button>Like</button><button>Share</button></div>
</div>
</article>
<style>
* { box-sizing: border-box; }
.comment { max-width: 32rem; }
.time { font-size: 0.8rem; color: #5b606b; }
/* your code here */
</style><article class="comment">
<img class="avatar" src="https://i.pravatar.cc/96?img=12" alt="" width="48" height="48" />
<div class="body">
<header class="meta">
<strong class="author">Asha Rao</strong>
<time class="time">2 hours ago</time>
</header>
<p>Grid finally clicked for me — this demo helped: https://example.com/a-really-long-link-that-keeps-going-and-going</p>
<div class="actions"><button>Reply</button><button>Like</button><button>Share</button></div>
</div>
</article>
<style>
* { box-sizing: border-box; }
.comment { max-width: 32rem; }
.time { font-size: 0.8rem; color: #5b606b; }
.comment {
display: flex;
gap: 0.75rem;
align-items: flex-start; /* the avatar keeps its size instead of stretching */
}
.avatar {
flex: none;
width: 3rem;
height: 3rem;
border-radius: 50%;
}
.body {
flex: 1;
min-width: 0; /* allow shrinking below the long URL's width */
overflow-wrap: anywhere; /* break the URL instead of overflowing */
}
.meta {
display: flex;
align-items: baseline;
gap: 0.5rem;
}
.time { margin-inline-start: auto; }
.actions {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
</style>Summary
- Navigation bars: flex row,
align-items: center,gap, and an auto margin to split groups. - Media object: a
flex: noneimage beside aflex: 1; min-width: 0body. - Input groups: a flexible input with fixed-size buttons attached.
- Sticky footer and card footers: a column flex container with
flex: 1ormargin-top: auto. - Wrap button rows and tag lists with
flex-wrap: wrapandgap.