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

flex containerflex itemflex itemflex item
the container decides; the items comply

key terms

main axis

Flexbox's primary direction, set by flex-direction. justify-content works along it.

row = left/right, column = up/down

cross axis

The perpendicular direction in flex. align-items and align-content live here.

align-items: center

grid track

One row or column of a grid, sized in the template. This is where you dictate the layout.

grid-template-columns: 1fr 2fr

fr unit

A fraction of the free space in a grid. Two 1fr columns split the leftovers equally.

1fr 1fr = 50/50

how it works

  1. 1
    Pick the container

    display: flex for one direction of content, display: grid for rows and columns together. This one choice does most of the work.

  2. 2
    Distribute the space

    In flex: justify-content for the main axis, align-items for the cross. In grid: define tracks with template-columns.

  3. 3
    Tune the children

    Flex items use flex-grow, shrink, and basis. Grid items can span tracks or be placed by line number.

  4. 4
    Gap 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

1.gallery {
2 display: grid;
3 grid-template-columns:
4 repeat(auto-fit, minmax(200px, 1fr));
fit as many 200px columns as the row allows
5 gap: 16px;
spacing belongs to the container now
6}

side by side

flexbox

  • one direction at a time
  • content decides the sizing
  • navbars, toolbars, button rows
  • great for unknown item counts
VS

grid

  • rows and columns together
  • you define the tracks up front
  • page shells, galleries, forms
  • overlapping and precise placement
🪤 gotcha

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

🎤 interview

How do you center a div?watch ↗

📚 tutorial

Flexbox or grid?watch ↗

💎 deep

What does flex: 1 actually mean?watch ↗

🔧 useful

Spacing between flex items?watch ↗

🔧 useful

A responsive grid without media queries?watch ↗

💎 deep

Reorder elements without touching HTML?watch ↗