Key takeaways
- Flexbox is one-dimensional (a row or a column); Grid is two-dimensional (rows and columns).
- Use Grid for page structure and Flexbox for components inside it.
- repeat(auto-fit, minmax()) gives responsive columns without media queries.
One dimension or two
Flexbox arranges items along a single axis, either a row or a column, and lets them grow, shrink and wrap. CSS Grid controls rows and columns at the same time, so items can line up in both directions.
When Flexbox is the right choice
Use Flexbox for navigation bars, button groups, toolbars, form rows and centering a single element. It shines when the content should decide the size of each item and the layout flows in one direction.
Properties such as justify-content, align-items, gap and flex-wrap cover most everyday needs.
When Grid is the right choice
Use Grid for page layouts, dashboards, card galleries and anything with defined rows and columns. You can place items by line number or named areas, and make an item span multiple tracks.
Grid keeps alignment consistent across rows, which is hard to achieve with Flexbox alone.
Using both together
The two work well together: a Grid defines the page regions, and Flexbox aligns the content inside each card or header. A nested approach keeps each rule simple.
Responsive patterns
For a card grid that adapts to any screen, use grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)). With Flexbox, use flex-wrap and a flex-basis value so items wrap naturally.
Common mistakes
Avoid using percentage widths and margins in Flexbox when gap would be simpler. Remember min-width: 0 on flex children that contain long text, and do not force a two-dimensional layout into Flexbox.
Frequently asked questions
Is Grid replacing Flexbox?+
No. They solve different problems and are designed to be used together.
Do both work in all modern browsers?+
Yes. Flexbox and Grid are supported in all current major browsers.
Try these free tools
- layoutFlexbox PlaygroundFlexbox Playground: experiment with live controls and see the result instantly. Free and private.Open tool
- layoutCSS Grid GeneratorGenerate css grid online with a live preview and copy-ready code. Free, private and instant.Open tool
- responsive designResponsive Grid GeneratorGenerate responsive grid online with a live preview and copy-ready code. Free, private and instant.Open tool