Common Flexbox Patterns
Six layouts you will build again and again, from centering and nav bars to sticky footers and media objects, and the flexbox property behind each.
Common Flexbox Patterns
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Most flexbox layouts reduce to a few moves: pick the axis, let one item take the leftover (`flex: 1` or `margin: auto`), and space the rest with `gap`.
- Centering anything both ways is `display: flex; justify-content: center; align-items: center`. `place-content: center` on a flex container is the same in one line.
- A sticky footer is a column flex container at least one viewport tall, with the `main` set to `flex: 1` so it absorbs the spare height.
- Equal-height cards with the button pinned to the bottom use `display: flex; flex-direction: column` inside each card and `margin-top: auto` on the button.
- Text next to a fixed-size thing (avatar, icon) needs `flex: none` on the fixed thing and `min-width: 0` on the text, or a long line will blow the row open.
Recipes, not rules
By now you have the whole flexbox model: axes, grow/shrink/basis, alignment, gap. This lesson puts them together. The goal is to recognise the shape of a problem and reach for the property that solves it, rather than trial-and-error with justify-content values.
1. Centering
.center { display: flex; justify-content: center; align-items: center; }
/* same thing */
.center { display: flex; place-content: center; }
justify-content centers on the main axis and align-items on the cross. For a single child you can skip both and write margin: auto on the child. The container needs a height for vertical centering to be visible, such as min-height: 100dvh.
2. A navigation bar
.nav { display: flex; align-items: center; gap: 1.5rem; }
.nav .logo { margin-right: auto; } /* pushes everything else right */
The auto margin takes all the spare space between the logo and the next item, so the links and the button sit together at the far end. This is the pattern from alignment and gap in use. When the bar must collapse on narrow screens, add flex-wrap: wrap and let the links drop to a second line, rather than reaching for a media query first.
3. Equal-height cards with the button at the bottom
A flex row gives every card the same height automatically (align-items: stretch). The card contents are another matter: a short card’s button floats up under its text.
.cards { display: flex; gap: 1rem; }
.card { flex: 1; display: flex; flex-direction: column; }
.card .button { margin-top: auto; }
Each card is now a column flex container, and margin-top: auto on the button takes up all the spare space above it. In a test with one short and one tall card, both buttons had the same y position. The outer flex row sizes the cards, and the inner flex column handles their contents.
4. Sticky footer
body { min-height: 100dvh; display: flex; flex-direction: column; }
main { flex: 1; }
The body is a column, and main grows to absorb every spare pixel, which pushes the footer to the bottom. When the content is taller than the viewport, min-height lets the body grow and nothing changes. A browser test with an 800 by 600 viewport put a 40px footer at y = 560, flush with the bottom. 100dvh is the dynamic viewport unit that accounts for mobile browser toolbars, covered in viewport units and dynamic viewports.
5. The media object with text that truncates
An avatar beside a name and message is the single most common component:
.media { display: flex; gap: 0.75rem; align-items: center; }
.media img { flex: none; width: 40px; height: 40px; border-radius: 50%; }
.media .text { flex: 1; min-width: 0; }
.media .name { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
flex: none stops the avatar from being squeezed. min-width: 0 on the text lets it shrink below the width of its content. Without that line, the text-overflow never kicks in, for the reason covered in flex-grow, shrink and basis. In a test, a 300px media object with a 40px image and a long line gave the text exactly 250px (300 minus 40 minus the 10px gap).
6. Wrapping rows with gap
.tags { display: flex; flex-wrap: wrap; gap: 0.5rem; }
Tags, filter chips and button groups all use this. The last row can be short, and items in it keep their natural width. If you need the items to line up in columns across rows, the two-dimensional alignment of grid is the better tool. Flexbox lays out each line independently.
What the video covers
Kevin Powell’s “Flexbox design patterns you can use in your projects” is from December 2020 and runs about fifteen and a half minutes, so it is over five years old. The flexbox properties it uses haven’t changed. Its timestamps list four sections: even columns, “grid-ish”, content and sidebar, and spacing things out. The description also links to a cheatsheet and a video on gap with a Safari workaround, which is no longer needed since gap works across all major browsers in flexbox. I could not read a transcript, so I do not describe what each section builds. The six recipes above are the prose’s own, tested separately.
Where this fits
Everything here builds on flexbox fundamentals, sizing and alignment. These patterns also feed into intrinsic and fluid layouts, where wrapping flex rows replace media queries. Several of them have a grid equivalent, which choosing between flexbox and grid compares directly.
Check yourself
A page has a short body and its footer floats mid-screen. How do you pin the footer to the bottom without fixed positioning?
Make `body` a column flex container with `min-height: 100vh` (or `100dvh`) and give `main` `flex: 1`. The main grows to fill the spare height and pushes the footer down. If the content is taller than the screen, the page scrolls normally.
Cards in a row have different amounts of text, and their buttons end up at different heights. How do you align the buttons?
Each card is already stretched to the same height by the row. Make each card its own column flex container and put `margin-top: auto` on the button. The auto margin takes all spare space above it, so every button sits at the card's bottom.
Why does `text-overflow: ellipsis` fail on a paragraph inside a flex row?
A flex item won't shrink below its content's minimum width, so the paragraph never gets narrower than its text. Add `min-width: 0` to the item (along with `overflow: hidden` and `white-space: nowrap`) so it can shrink and the ellipsis can appear.
You want a logo on the left, links in the middle and a button at the right of a nav. What is the least code?
A flex container with `justify-content: space-between; align-items: center`, which puts the first and last items at the edges and the middle one centered between them (when the end items are of similar width). If the widths differ, give the middle item `margin-inline: auto` instead.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.