← All guides

Design · 7 min read

Flexbox vs CSS Grid: Which Should You Use?

Flexbox and CSS Grid are both modern layout systems, both supported in every current browser, and both able to centre a div. That overlap makes it hard to know which to reach for. The short answer is that flexbox lays items out along one direction and lets their content decide sizes, while grid defines rows and columns first and places items into them. Most real pages use both.

This guide explains how each one thinks, the cases where each is clearly better, how they combine, and the properties that cause the most confusion.

Try it yourself with the related tool.

Try the layout playground →

Advertisement

Flexbox: one direction, content first

A flex container arranges its children in a row or a column. Items grow and shrink according to flex-grow, flex-shrink and flex-basis, and justify-content and align-items distribute them along the main and cross axes. With flex-wrap: wrap, items flow onto new lines, but each line is laid out on its own — items on one line do not line up with items on the next.

This makes flexbox ideal for components whose size depends on their content: a navigation bar, a toolbar of buttons, a row of tags, a card whose footer should stick to the bottom, or a form row with a label and a stretching input.

Grid: two directions, layout first

A grid container defines tracks with grid-template-columns and grid-template-rows, and items snap into the cells. Because rows and columns are shared, items line up in both directions. The fr unit divides free space, minmax() sets track limits, and grid-template-areas lets you name regions — header, sidebar, main, footer — and rearrange them in a media query without touching the HTML.

Grid suits page layouts, dashboards, image galleries, and any design where alignment across rows matters.

A practical way to choose

Ask whether you are arranging items in a line or placing them on a two-dimensional structure. A list of things that should sit next to each other and share space: flexbox. A layout where things must line up in columns as well as rows: grid. Another useful question is where sizing comes from. If the content should decide, flexbox; if the layout should decide and content should fit, grid.

The responsive card gallery

One line of grid replaces what used to take media queries: grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)). The browser fits as many 220-pixel-minimum columns as it can and stretches them to fill the row. With flexbox, wrapped cards on the last row stretch to fill it unevenly or need careful widths; grid keeps every card the same width. Use auto-fit instead of auto-fill if you want a short final row to stretch across the empty columns.

Combining them

Layouts nest. A typical page uses grid for the overall shell, grid for a gallery of cards, and flexbox inside each card to align an icon with a title and push buttons to the bottom. There is no performance reason to prefer one, so pick whichever expresses the layout most directly at each level. Subgrid, now supported in all major browsers, lets nested elements align to the parent grid's tracks — useful for card titles and footers that should line up across a row.

Properties that trip people up

The gap property works in both flexbox and grid and is cleaner than margins on children. align-items defaults to stretch, which is why flex children are often taller than expected; set align-items: start to stop it. A flex item will not shrink below its content's minimum size, so long words or wide images overflow — add min-width: 0 to the item to fix it. And justify-items does nothing in flexbox; it is a grid property.

Accessibility note

Both systems can visually reorder items with order, flex-direction: row-reverse or grid placement, but screen readers and keyboard focus still follow the HTML source order. Keep the source in a logical reading order and use visual reordering only for presentation, so the page makes sense to everyone.

Quick reference

Related guides