Skip to content
Web Design & Development

A Practical Guide to CSS Grid, Flexbox and Container Queries

The modern CSS layout tools: Flexbox for arranging items along one axis, Grid for two-dimensional layouts, and container queries for components that adapt to the space they sit in.

Marcus Adeyemi Technical Director 2 min read 21 views
A Practical Guide to CSS Grid, Flexbox and Container Queries

What is at stake

They replace floats, fixed widths and layout hacks, making responsive layouts simpler to build and cheaper to change later.

The playbook

  1. Use Grid for page structure and Flexbox inside components
  2. Let tracks size to content with minmax and auto-fit
  3. Use container queries for components reused in different column widths
  4. Keep source order logical so keyboard order follows reading order
  5. Test at widths between breakpoints, not only at them

Where it goes wrong

Avoid:

  • Reordering content visually so it no longer matches the page order
  • Fixed pixel widths that break on narrow screens
  • Stacks of nested flex containers imitating a grid
  • Breakpoints tied to particular device models
What to do and what to avoid with CSS layout: grid, flexbox and container queries, side by side
Good practice against the usual mistakes, from the sources listed below.

The numbers behind it

Published figures for CSS Layout: Grid, Flexbox and Container Queries
MeasureFigure
Flexboxone-dimensional layout along a row or a column
Gridtwo-dimensional layout of rows and columns at once
Container queriesstyle a component based on its container's size rather than the viewport
Browser support for container size queriesall major browsers since 2023
Subgridlets a nested grid align to its parent grid's tracks

Getting outside help

When to hand it over: Bring in help when a legacy layout built on floats or frameworks needs modernizing, or when components break whenever they move to a different column.

Where this comes from

The figures and practices above come from the sources listed.

Working on something like this?

We take on Web Design & Development work for teams who want it done once, properly. Tell us what you are building and we will tell you honestly whether we are the right studio for it. Start a project.

Where to go next

Spotted something wrong? Report an error on this page. We correct on the page and say what changed.

All services

The work behind this article, and what it costs.

Marcus Adeyemi

Builds and maintains the web work. Writes about front-end architecture, performance, accessibility and the unglamorous parts of keeping a site alive.

Keep reading

More in Web Design & Development