CSS grid layout vs. Flexbox

The short answerCSS grid layout arranges items in two dimensions, rows and columns at once, from a structure you define on the container. Flexbox arranges items in one dimension, a row or a column, and lets their content decide how space is shared. Most layouts use both.

CSS grid layout and Flexbox are the two modern CSS layout systems built for arranging boxes on a page. Both replaced older techniques such as floats and table layouts, both use the same alignment properties and both support the gap property. People compare them because many layouts can be built with either, but each one makes a different kind of layout easier.

CSS grid layout vs. Flexbox at a glance
CSS grid layoutFlexbox
DimensionsTwo: rows and columns at the same timeOne: a single row or column, with optional wrapping
ApproachLayout first: define the grid, then place items into itContent first: items size themselves and share the remaining space
How to turn it ondisplay: grid on the containerdisplay: flex on the container
Sizing toolsTrack sizes, the fr unit, minmax() and repeat()flex-grow, flex-shrink and flex-basis on each item
PlacementItems can be placed by line number or named area, and can overlapItems follow source order along the line, unless the order property changes it
WrappingItems in every row line up in the same columnsEach wrapped line is laid out on its own, so items don't align across lines
Typical usesPage layouts, card grids, galleries and formsNavigation bars, toolbars, button groups and icon-and-text rows

How CSS grid layout works

With grid, you define the structure on the container. grid-template-columns and grid-template-rows set the tracks, using lengths, percentages or the fr unit, which shares out the free space. Functions such as repeat() and minmax() make responsive grids possible without media queries, for example with repeat(auto-fit, minmax(200px, 1fr)).

Items then fill the grid automatically or are placed explicitly by line numbers or by named areas from grid-template-areas. Items can span several tracks or overlap one another, and subgrid lets a nested grid line up with its parent's tracks.

How Flexbox works

With Flexbox, the container sets a direction, row or column, and items line up along it. Each item's size starts from its content or its flex-basis, and flex-grow and flex-shrink decide how leftover space is added or taken away. justify-content spreads items along the main axis, and align-items positions them across it.

When items wrap with flex-wrap, each new line is laid out independently. That works well for content of varying sizes, such as tags or buttons, but it means items on different lines don't form neat columns.

Using them together

Grid and Flexbox aren't rivals so much as tools for different jobs, and most sites use both. A common pattern is grid for the overall page and for anything that needs to align in rows and columns, with Flexbox inside components such as headers, toolbars and cards. Both share the box alignment properties and gap, so switching a component from one to the other is often a small change.

When to choose CSS grid layout

Choose grid when you care about both rows and columns, when items should line up across rows, or when you want the layout to control sizing rather than the content. It's the better fit for page templates, card grids, image galleries, forms with aligned labels and any design that starts as a sketch of boxes on a grid.

When to choose Flexbox

Choose Flexbox when items flow in a single line and their content should decide their size, such as a navigation bar, a row of buttons, a list of tags or an icon next to text. It's also handy for pushing one item to the far end of a row with an auto margin.

The verdict

Use CSS grid layout when the layout is two-dimensional or should be defined from the container, and Flexbox when items sit in one row or column and their content should set their size. For most projects the answer is both: grid for the structure, Flexbox for the components inside it.

Published