Flexbox & Grid
Center a div. The final boss of frontend.
▶ the lesson · narrated sketch video · tap for sound
the intro
Layout is the part of CSS that used to hurt. Flexbox handles rows and columns in one direction, grid handles full two-dimensional layouts. Learn both and you can build almost any screen without a single float hack.
the sample
the final boss, defeated
.parent {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}fun fact ✦
“How to center a div” has millions of search results. Flexbox made them all obsolete.
the docs
the deep dive
the ideas behind the video — concepts, diagrams, a walkthrough and the traps to avoid
Layout is just deciding who gets the leftover space. Flexbox lets the content negotiate: items grow and shrink to fit. Grid lets you dictate: you draw the tracks and place things on them. Most real screens use grid for the skeleton and flex inside each region.
flexbox negotiates, grid dictates
key terms
main axis
Flexbox's primary direction, set by flex-direction. justify-content works along it.
row = left/right, column = up/downcross axis
The perpendicular direction in flex. align-items and align-content live here.
align-items: centergrid track
One row or column of a grid, sized in the template. This is where you dictate the layout.
grid-template-columns: 1fr 2frfr unit
A fraction of the free space in a grid. Two 1fr columns split the leftovers equally.
1fr 1fr = 50/50how it works
- 1Pick the container
display: flex for one direction of content, display: grid for rows and columns together. This one choice does most of the work.
- 2Distribute the space
In flex: justify-content for the main axis, align-items for the cross. In grid: define tracks with template-columns.
- 3Tune the children
Flex items use flex-grow, shrink, and basis. Grid items can span tracks or be placed by line number.
- 4Gap over margins
Set gap on the container and delete the margin hacks and last-child cleanup for good.
the code, explained
a photo gallery with zero media queries
side by side
flexbox
- one direction at a time
- content decides the sizing
- navbars, toolbars, button rows
- great for unknown item counts
grid
- rows and columns together
- you define the tracks up front
- page shells, galleries, forms
- overlapping and precise placement
Flex items refuse to shrink below their content by default — that is min-width: auto, and it is why your flex row overflows with one long word or a wide table. The fix is min-width: 0 on the item. Almost every mysterious flexbox overflow is this one line.
quick answers
Which should I learn first?
Flexbox — smaller mental model, immediate payoff. Grid second, and then notice you use both inside the same component.
Can flexbox and grid mix?
Not only can they, they should: grid for the page shell, flex inside each cell. They compose instead of compete.
Is centering actually hard?
Three lines of flex. The meme is inherited trauma from the float era. You may still laugh at it in interviews.
quick hits about Flexbox & Grid
short answers · interviews, tips, trivia & hidden gems