CSS Flexbox and CSS Grid solve different layout problems, and choosing the right one becomes much easier when you think in terms of dimensions. This guide compares their strengths, provides copyable responsive CSS layout examples, and gives you practical patterns for navigation bars, card grids, dashboards, centered content, and complete page shells. Use the troubleshooting and review checklists when a layout starts overflowing, stretching, or behaving differently at a breakpoint.
Overview
Flexbox is primarily a one-dimensional layout system. It arranges items along a row or a column and is especially useful when the size of the content should influence how available space is distributed. Grid is a two-dimensional layout system. It lets you define rows and columns together, making it a stronger choice when the relationship between both axes matters.
Neither method is a replacement for the other. A page may use Grid for its overall structure, Flexbox inside a card, and normal document flow for text. A useful rule is to choose the layout mechanism based on the component’s most important relationship:
- Choose Flexbox when items primarily need to flow in one direction, such as a navigation bar, button group, toolbar, or row of metadata.
- Choose Grid when rows and columns need coordinated sizing, such as a dashboard, gallery, pricing comparison, or page shell.
- Use normal flow when content should simply stack naturally without special alignment requirements.
| Question | Flexbox | Grid |
|---|---|---|
| How many dimensions matter? | One: row or column | Two: rows and columns |
| What controls sizing? | Content and free-space distribution | Explicit or flexible tracks |
| Typical uses | Navigation, toolbars, inline controls | Cards, dashboards, page regions |
| Best alignment tools | justify-content and align-items | Track definitions plus place-items and gaps |
| Can items wrap? | Yes, with flex-wrap | Yes, through responsive tracks and auto-placement |
Both systems are established CSS layout choices in modern browsers. For projects with a defined browser-support policy, verify the relevant properties against your project’s Browserslist or compatibility workflow rather than assuming that every CSS feature has identical support everywhere.
What to track
Flexbox patterns for one-dimensional components
A navigation bar usually needs horizontal distribution, vertical centering, and a sensible mobile fallback. Flexbox expresses that intent directly:
.site-nav {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
flex-wrap: wrap;
}
.site-nav__links {
display: flex;
align-items: center;
gap: 0.75rem;
flex-wrap: wrap;
}
@media (max-width: fortyrem) {
.site-nav {
align-items: flex-start;
flex-direction: column;
}
}
Replace the illustrative breakpoint value with the unit and threshold appropriate for your design system. The important behavior is that links may wrap, and the layout can switch to a column when the available width becomes insufficient.
For a centered content block, Grid and Flexbox both work. Flexbox is concise when there is only one item or one axis to manage:
.centered {
min-height: 100dvh;
display: flex;
align-items: center;
justify-content: center;
padding: 1rem;
}
.centered__content {
width: min(100%, 42rem);
}
The min() function keeps the content from exceeding its container while preserving a readable maximum width. If the centered region becomes part of a larger two-dimensional layout, Grid may make the surrounding structure clearer.
Grid patterns for coordinated rows and columns
A responsive card grid is a common case for CSS Grid. The browser creates as many columns as fit while keeping each card above a minimum width:
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
gap: 1rem;
}
.card {
min-width: 0;
}
auto-fit collapses unused tracks, while minmax() establishes a lower and upper size. The min-width: 0 declaration is a useful safeguard when long text or wide children would otherwise force a grid item beyond its track.
For a dashboard with a sidebar and a main region, define the relationship explicitly:
.dashboard {
display: grid;
grid-template-columns: minmax(12rem, 16rem) minmax(0, 1fr);
gap: 1.5rem;
}
@media (max-width: 50rem) {
.dashboard {
grid-template-columns: 1fr;
}
}
The minmax(0, 1fr) track is intentional: it allows the main region to shrink instead of letting an unbreakable child create horizontal overflow.
Page shells and the holy-grail layout
Grid is well suited to a page with a header, footer, navigation area, main content, and optional aside. Named areas make the structure visible in the CSS:
.page {
min-height: 100dvh;
display: grid;
grid-template:
"header header header" auto
"nav main aside" 1fr
"footer footer footer" auto
/ minmax(10rem, 15rem) minmax(0, 1fr) minmax(10rem, 18rem);
gap: 1rem;
}
.page__header { grid-area: header; }
.page__nav { grid-area: nav; }
.page__main { grid-area: main; }
.page__aside { grid-area: aside; }
.page__footer { grid-area: footer; }
@media (max-width: 60rem) {
.page {
grid-template:
"header" auto
"main" 1fr
"nav" auto
"aside" auto
"footer" auto
/ 1fr;
}
}
On smaller screens, the areas become a single column without requiring changes to the markup. Decide the order deliberately: source order, visual order, and keyboard navigation should remain understandable to users of assistive technology.
Cadence and checkpoints
Layout decisions do not need daily monitoring, but they benefit from a lightweight review cadence. Recheck responsive components when they are created, when their content changes, and during routine browser or design-system maintenance.
- During component work: identify whether the component has a one-dimensional or two-dimensional relationship before writing layout rules.
- At pull request review: test the narrow, expected, and wide states. Look for hard-coded widths, unnecessary negative margins, and layout rules that depend on a particular text length.
- After content changes: test longer labels, translated strings, missing images, empty states, and user-generated text. Content is often what reveals fragile sizing.
- During monthly or quarterly maintenance: review browser-support assumptions and run the project’s normal visual or end-to-end checks at key viewport sizes.
Keep a small checkpoint list for important layouts: navigation, forms, card collections, dashboards, and page shells. Record the intended behavior at each breakpoint rather than recording only pixel dimensions. For example: “navigation wraps before controls overlap” is more useful than “works at 768 pixels.”
How to interpret changes
Unexpected layout changes usually come from a mismatch between the chosen layout model and the content constraints. Use this checklist before adding another override:
- Unexpected horizontal overflow: inspect long words, fixed-width children, images, and nested flex or grid items. Try
min-width: 0on the shrinking child and make replaced elements responsive withmax-width: 100%. - Items stretch vertically: remember that the default cross-axis behavior often stretches children. Set
align-items: startoralign-items: centerwhen equal-height stretching is not intended. - Gaps look inconsistent: prefer
gapfor spacing between flex or grid children. Check whether margins on the children are being combined with the container gap. - Alignment appears wrong: in Flexbox,
justify-contentfollows the main axis andalign-itemsfollows the cross axis. The result changes whenflex-directionchanges. - Grid columns become too wide: inspect fixed track sizes and use flexible tracks such as
minmax(0, 1fr)where content should be allowed to shrink. - A breakpoint feels arbitrary: choose it where the content stops fitting comfortably, not solely where a device category begins. Container queries may be appropriate when a component’s available width matters more than the viewport.
- Visual order differs from source order: avoid using
orderas a substitute for semantic markup. Confirm keyboard and reading order after any visual rearrangement.
When debugging, temporarily add outlines to the container and its children. Inspect the computed width, min-width, padding, gap, and overflow values. A small isolated reproduction is often faster than stacking more selectors onto a complex component.
When to revisit
Revisit a Flexbox or Grid decision when the component’s responsibilities change. A row of buttons may become a toolbar with grouped controls; a card list may gain featured items that span multiple columns; a page shell may add a persistent sidebar. Those changes can turn a one-dimensional problem into a two-dimensional one, or vice versa.
Schedule a broader layout review monthly or quarterly for active interfaces, especially after changes to typography, spacing tokens, content models, or supported viewport ranges. Also review immediately when a regression appears in a browser, when a new component is reused in a narrower container, or when a design-system update changes default sizing or alignment.
For your next layout, take this practical sequence:
- Write down the relationship that must remain stable: a single row, a set of rows and columns, or a natural content stack.
- Choose Flexbox, Grid, or normal flow based on that relationship.
- Start with flexible tracks, intrinsic content, and
gaprather than fixed offsets. - Test real content at narrow and wide container sizes.
- Record the intended responsive behavior so the next refactor has a clear baseline.
That process keeps CSS layout decisions understandable, reduces breakpoint patches, and gives you a repeatable way to diagnose regressions when an interface evolves.