Sub topic 17
Common patterns: navbar, card row, centering, sticky footer
This chapter assumes you know the CSS box model, selectors, and the core Flexbox properties: display: flex, flex-direction, justify-content, align-items, flex-wrap, gap, and the flex shorthand. Here you combine them into four layouts that appear on almost every site. You will also see the mistakes that break each one.
How to approach a pattern
Every Flexbox pattern comes down to three decisions:
- Which element is the flex container, and which elements are its direct children (the flex items)?
- Which axis is the main axis? That is
rowby default andcolumnwhen you setflex-direction: column. - What should happen to leftover space, and what should happen when there is too little?
Pattern 1: Navigation bar
A navigation bar is a row where the logo sits at one edge, the links sit at the other, and everything is centered vertically.
<nav class="nav">
<a class="logo" href="/">Acme</a>
<ul class="links">
<li><a href="/docs">Docs</a></li>
<li><a href="/pricing">Pricing</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>.nav {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
}The .nav element has two direct children, .logo and .links. With space-between, the first item goes to the start edge and the last goes to the end edge. align-items: center centers both on the cross axis, so a tall logo and a short list line up. The gap keeps a minimum distance between them when the bar gets narrow.
The links inside .links are not flex items of .nav, because they are grandchildren. To lay them out in a row, make .links a flex container too:
.links {
display: flex;
gap: 1.5rem;
list-style: none;
margin: 0;
padding: 0;
}Nesting flex containers is normal. Each container controls only its own children.
Variation: one item pushed away
space-between works well with two or three items. With more items it spreads all of them evenly, which is often not what you want. If the logo and first links should stay together and only the last item should move to the far end, use an auto margin:
.toolbar {
display: flex;
align-items: center;
}
.toolbar .push-right {
margin-left: auto;
}An auto margin absorbs all free space on the main axis. The .push-right item and every item after it move to the end.
Pattern 2: Card row that wraps
You want cards that sit side by side on a wide screen and stack on a narrow one, without media queries.
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 200px;
}flex: 1 1 200px means flex-grow: 1, flex-shrink: 1, and flex-basis: 200px. Each card wants to be 200px wide. The browser fits as many 200px cards as it can on a line, counting the gaps. Because of flex-wrap: wrap, the rest go to the next line. On each line, flex-grow: 1 divides the leftover space equally among the cards on that line.
The last-row problem
Each flex line is sized on its own. If you have 5 cards and 4 fit per line, the fifth card grows to fill the entire second line. It becomes much wider than the others.
To keep the last card the same width as the rest, you have two choices:
- Cap growth with
max-width, for examplemax-width: 300px. The card stops growing, and the leftover space stays empty. - Use
flex: 0 0 calc(25% - 0.75rem)for a fixed number of columns. You then have to account for the gaps yourself.
If you need a strict grid of equal columns, Grid is the better tool. The final chapter compares the two.
Card contents that line up
Cards often have a title, text, and a button. To pin the button to the bottom of every card, even when the text lengths differ, make each card a column flex container:
.card {
flex: 1 1 200px;
display: flex;
flex-direction: column;
}
.card .button {
margin-top: auto;
}Items in a flex row stretch to equal height by default (align-items: stretch), so all cards in a line are as tall as the tallest one. The auto top margin then pushes the button to the bottom of each card.
Pattern 3: Centering
Centering one child both ways takes three declarations:
.center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}justify-content centers on the main axis (horizontal in a row). align-items centers on the cross axis (vertical in a row). The container needs height for vertical centering to be visible. A block element is only as tall as its content, so there would be no free vertical space. That is why min-height: 100vh is there. Using min-height instead of height lets the container grow if the content is taller than the viewport.
Two shorter alternatives exist. margin: auto on the single child centers it on both axes inside a flex container. place-content: center sets both alignment properties at once on a flex container in current browsers.
Centering gotchas
- If you change to
flex-direction: column, the axes swap.justify-contentnow controls vertical position andalign-itemscontrols horizontal position. - Centering can clip content that overflows the start edge. With
justify-content: center, an item wider than the container overflows on both sides, and the part past the left edge cannot be scrolled to. Usingsafe centeravoids this, or usemargin: autoon the item instead. - On mobile browsers,
100vhcan be taller than the visible area because of the browser toolbar. The100dvhunit tracks the visible height. You can writemin-height: 100vh;followed bymin-height: 100dvh;so that older browsers use the first value.
Pattern 4: Sticky footer
On a short page, a footer normally ends up in the middle of the screen. A sticky footer stays at the bottom of the viewport on short pages and below the content on long pages.
<body>
<header>Site header</header>
<main>Short content</main>
<footer>Site footer</footer>
</body>body {
display: flex;
flex-direction: column;
min-height: 100vh;
margin: 0;
}
main {
flex: 1;
}The body is a column container at least as tall as the viewport. The header and footer take their natural height. main has flex: 1, which is flex: 1 1 0%, so it takes all the remaining height. The footer is pushed to the bottom.
When the content is taller than the viewport, min-height lets body grow, so the footer follows the content. If you had used height: 100vh, the content would overflow the body instead.
The same effect works with margin-top: auto on the footer and no flex: 1 on main. Use flex: 1 when you also want main itself to be tall, for example to center content inside it.
Common errors in these patterns
| Symptom | Likely cause | Fix |
|---|---|---|
| Child is not centered vertically | Container has no extra height | Add `min-height` to the container |
| Nav links stack instead of sitting in a row | `display: flex` was put on `nav`, but the links are in a nested `ul` | Make the `ul` a flex container |
| Cards overflow the row | `flex-wrap` is missing, so items shrink and then overflow | Add `flex-wrap: wrap` |
| Last card is too wide | Each line shares its own free space | Set `max-width` on cards |
| Long title pushes other items out of the row | A flex item will not shrink below its content width | Set `min-width: 0` on the item |
| Footer floats mid-page | `main` does not grow | Use `flex: 1` on `main` in a column container |
The long-title case deserves an example. Without min-width: 0, a flex item's automatic minimum size is its content width, so it refuses to shrink and the text overflows. Setting the minimum to zero allows truncation:
.row {
display: flex;
gap: 0.5rem;
}
.row .title {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}Exercise
You build a page with body { display: flex; flex-direction: column; }, a header, a main, and a footer. The page has little content, and the footer sits directly under main, in the upper part of the window. Which two changes put the footer at the bottom of the window, and why does each work?
Show answer
First, give body a min-height: 100vh. Without it, body is only as tall as its content, so there is no free vertical space to distribute. Second, give main flex: 1 (or give the footer margin-top: auto). In a column container, this makes main grow into the free space, or lets the auto margin absorb it, which pushes the footer to the bottom edge.
Exercise
A row of 7 cards uses .cards { display: flex; flex-wrap: wrap; gap: 1rem; } and .card { flex: 1 1 200px; }. On a wide screen, 3 cards fit per line, and the seventh card spans the whole last line. How can you keep it from becoming wider than the others without changing the number of cards per line?
Show answer
Set a max-width on .card equal to the width you want the cards to have at most, for example max-width: 300px. The card then stops growing at that size, and the leftover space on the last line stays empty. Because the cap would also apply to other lines, choose a value that matches the layout, or switch to CSS Grid with equal columns.