Choosing between CSS Grid and Flexbox becomes easier when you first identify the shape of the layout. This guide gives you a practical decision checklist, reusable patterns for common components, and a debugging process for overflow, wrapping, sizing, and unexpected gaps.
Overview
CSS Grid and Flexbox are both capable layout systems, but they solve different problems most naturally. Flexbox is primarily a one-dimensional system: it arranges items along a row or a column. Grid is a two-dimensional system: it can coordinate columns and rows at the same time.
That distinction is a useful starting point, not an absolute rule. A navigation bar can use Flexbox even when it wraps, and a Grid layout can contain a single row. The better question is whether the component’s structure is mainly linear or whether items need to align across both dimensions.
| Question | Prefer Flexbox when... | Prefer Grid when... |
|---|---|---|
| How many dimensions matter? | Items follow one main row or column. | Rows and columns must work together. |
| What controls the size? | Content should distribute available space. | You want explicit tracks, minimums, or areas. |
| How does it respond? | Items can grow, shrink, wrap, or reorder along one axis. | The overall structure should change between defined or automatic tracks. |
| What needs alignment? | Items need alignment within a line or component. | Separate items need shared column or row boundaries. |
| What is the component? | Navigation, toolbar, button group, or inline form. | Page shell, dashboard, gallery, or card layout. |
It is also normal to combine them. A page may use Grid for its main regions, Flexbox for the header, and another Flexbox container inside each card. Treat layout methods as composable tools rather than competing choices.
Checklist by scenario
Navigation bars and toolbars
Start with Flexbox when items sit in a line and the main requirements are spacing, alignment, and flexible distribution.
.nav { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.nav__links { display: flex; gap: 0.75rem; flex-wrap: wrap; }
.nav__action { margin-inline-start: auto; }Use gap for spacing between items instead of adding margins that require special handling at the edges. Add flex-wrap: wrap when a narrow viewport should create another line rather than cause horizontal overflow. If the navigation needs distinct columns or a defined mobile and desktop structure, Grid may provide a clearer outer layout.
Cards and repeated content
Choose Grid when cards should form a responsive set of columns and rows. A track definition based on a minimum card width can reduce the need for several breakpoint-specific rules.
.card-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr)); gap: 1.25rem; }The min(100%, 16rem) safeguard helps prevent a card track from being wider than a narrow container. Use auto-fill instead of auto-fit when you want empty tracks to remain part of the grid’s calculation; use auto-fit when available space should be given to the cards that exist.
Flexbox is still a good choice when the cards are a single row of variable-width items, such as horizontally scrolling filters or tags. In that case, define the intended overflow behavior rather than assuming the items will always fit.
Forms and control groups
Use Flexbox for a label-and-control row, a button group, or a compact search control. Use Grid when labels and fields need consistent columns across multiple rows.
.form-row { display: grid; grid-template-columns: minmax(8rem, 12rem) minmax(0, 1fr); gap: 0.75rem 1rem; align-items: start; }
@media (max-width: 40rem) {
.form-row { grid-template-columns: 1fr; }
}The minmax(0, 1fr) value is important when a field contains long text or a wide control. It allows the track to shrink instead of preserving an automatic minimum that can push the form beyond its container.
Page shells and dashboards
Grid is usually the clearer choice when a page has a header, navigation region, main content, and optional sidebar. Name the regions when doing so makes the structure easier to read and maintain.
.shell { display: grid; grid-template-columns: 16rem minmax(0, 1fr); grid-template-areas: 'sidebar main'; min-height: 100vh; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; min-width: 0; }
@media (max-width: 48rem) {
.shell { grid-template-columns: 1fr; grid-template-areas: 'main'; }
.sidebar { display: none; }
}For a page with a full-width header, add a header row to the grid rather than using positioning as a shortcut. Keep the source order logical so the layout remains understandable when styles change or assistive technology reads the document.
What to double-check
- Container width: Inspect the element that establishes the available width. A correct child rule cannot compensate for a parent with unexpected padding, width, or overflow.
- Intrinsic minimum sizes: Grid and Flexbox items can resist shrinking because of long words, unbroken URLs, images, or form controls. Try
min-width: 0on the relevant flex or grid child, and use safe text wrapping where appropriate. - Flex direction and axes: In Flexbox,
justify-contentfollows the main axis andalign-itemsfollows the cross axis. Those axes change whenflex-directionchanges. - Grid gaps:
gapadds space between tracks or flex items. It does not create outer space around the container. Check padding separately before changing the gap value. - Track sizing: An
frunit distributes remaining space, but content can still affect the result. Useminmax(0, 1fr)when a track must be allowed to shrink. - Box sizing: Apply
box-sizing: border-boxconsistently if width calculations seem larger than expected. Borders and padding otherwise affect the final size differently than many developers anticipate. - Responsive assumptions: Test the component at its actual container widths, not only at familiar device widths. A component may fail inside a narrow panel even when it works across the full viewport.
When a layout behaves unexpectedly, temporarily add outlines to the container and its children, inspect computed dimensions, and disable rules one at a time. A small, isolated reproduction is often faster than repeatedly adjusting spacing values in a large stylesheet.
Common mistakes
Using Flexbox to simulate a complete page grid. Nested flex containers can work, but they often make shared columns and row alignment harder to maintain. If multiple regions must line up in two dimensions, start with Grid.
Using Grid for every linear group. A one-row button group does not need explicit tracks merely because Grid can create them. Flexbox usually communicates the intent more directly.
Adding fixed widths to stop wrapping. Fixed widths may hide the original sizing problem and create overflow at another viewport. Prefer flexible tracks, sensible minimums, and deliberate wrapping.
Confusing visual order with document order. Properties such as order can change visual placement without changing source order. Use them carefully, especially for navigation, forms, and content where reading or tab order matters.
Forgetting replaced elements. Images, videos, and embedded controls may have intrinsic dimensions. Set a maximum inline size such as max-inline-size: 100% when an asset must stay within its container, and define an appropriate aspect ratio when the design requires predictable space.
Relying on unexplained magic numbers. A value that fixes one screenshot may fail when text changes, localization expands a label, or a sidebar is removed. Document intentional constraints and test with realistic content.
When to revisit
Revisit the layout decision whenever a component changes shape, not only when a browser or framework changes. A Flexbox toolbar may become a two-dimensional settings panel; a Grid card collection may become a horizontally scrolling list; a fixed sidebar may become collapsible navigation.
Use this short checklist before committing or refactoring:
- Describe the layout in one sentence: is it mainly a line, a set of shared rows and columns, or a combination?
- Test with short, long, and translated-looking labels rather than one ideal content sample.
- Resize the component’s container and check for overflow, wrapping, clipped focus indicators, and unusable controls.
- Inspect the smallest supported viewport and a wide viewport, plus any intermediate width where the design changes.
- Confirm that source order, keyboard focus order, and visual order still make sense.
- Record why Grid, Flexbox, or both were chosen so the next refactor starts with context.
Browser support and CSS features can evolve, so confirm implementation details when adopting newer layout capabilities or changing a project’s support baseline. For everyday work, however, the durable rule is simple: use Flexbox to distribute and align items along one axis, use Grid to organize two-dimensional structure, and combine them when each layer has a different job.