Sub topic 17
Common patterns: navbar, card row, centering, sticky footer
This chapter assumes you know the basics of a flex container, the main and cross axes, justify-content, align-items, flex-wrap, gap and the flex shorthand. Here you combine them into four layouts you will build often.
Navigation bar
A navbar puts a logo at one end and links at the other, all centered vertically.
.nav {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
}space-between pushes the first and last children to the edges. align-items: center aligns them on the cross axis. gap keeps neighbors apart when the bar gets tight.
To push only one item to the end, use an auto margin instead:
.toolbar {
display: flex;
align-items: center;
}
.toolbar .push-right {
margin-left: auto;
}The auto margin absorbs all free space on the main axis. The .push-right item and every item after it move to the far end.
Card row
Cards should start at a sensible width, wrap when a row is full, and share leftover space.
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 200px;
}flex: 1 1 200px means grow 1, shrink 1, basis 200px. Each card tries to be 200px wide. Wrapping starts a new line when the next card would not fit, and the cards on each line grow to fill it.
Each line grows independently. A last line with one card will stretch that card across the whole row. If you want it to stay card-sized, use Grid instead (see the later chapter on choosing between them).
Centering
Center one child horizontally and vertically:
.center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}justify-content centers on the main axis and align-items on the cross axis. The container needs a height larger than its content, so min-height: 100vh gives it the full viewport. Without a height, there is no free space to center within.
Sticky footer
On a short page, a footer should still sit at the bottom of the window.
body {
display: flex;
flex-direction: column;
min-height: 100vh;
margin: 0;
}
main {
flex: 1;
}The body is a column, so its main axis is vertical. main has flex: 1, so it grows to take all the remaining height. A footer after it ends up at the bottom. On long pages the body grows past 100vh and the footer follows the content as usual.
A fix for long titles
A flexible item with long text can push the row wider than its container. Flex items have an automatic minimum size equal to their content, so they refuse to shrink below it.
.row {
display: flex;
gap: 0.5rem;
}
.row .title {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}min-width: 0 removes the automatic minimum. The title can now shrink, and the ellipsis appears instead of overflow.
Exercise
A toolbar has a logo, two links and a "Log in" button. You want the logo and links on the left and the button alone on the right. Which single declaration on which element does this, and why does it work?
Show answer
Put margin-left: auto on the "Log in" button, inside a container with display: flex. The auto margin takes all free space on the main axis, which pushes the button to the far end while the other items stay at the start.
Exercise
In the sticky footer layout, what happens if you remove flex: 1 from main, and the page content is short?
Show answer
main keeps its natural height, so the footer sits directly below it with empty space after the footer. Nothing absorbs the leftover height of the 100vh body.