Flexbox and CSS Grid solve different layout problems, and choosing between them becomes much easier when you identify whether your design is one-dimensional, two-dimensional, content-driven, or explicitly structured. This practical cheatsheet compares their strengths, gives you a repeatable selection workflow, and provides 10 responsive CSS layout templates you can copy, adapt, and test in a real project.
Overview
Use Flexbox when you are arranging items along one primary axis: a row of navigation links, a toolbar, a button group, or content that should distribute itself horizontally or vertically. Use CSS Grid when both rows and columns matter: dashboards, card galleries, page shells, forms, and layouts with deliberate areas.
The choice is not permanent. A common and maintainable pattern is to use Grid for the page-level structure and Flexbox inside individual components. For example, Grid can place a header, sidebar, and main content area, while Flexbox aligns the logo and navigation links inside the header.
| Question | Prefer Flexbox | Prefer Grid |
|---|---|---|
| How many dimensions matter? | One axis at a time | Rows and columns together |
| Who controls sizing? | Content often influences item size | The grid tracks define structure |
| Typical examples | Navigation, toolbars, media objects, button groups | Page shells, galleries, dashboards, forms |
| Main strength | Alignment and distribution | Explicit, responsive structure |
| Common limitation | Complex two-dimensional placement can become indirect | Small one-axis groups may need more declarations than necessary |
A quick selection workflow
- Sketch the layout without CSS and identify whether the main relationship is a row, column, or matrix.
- Choose Flexbox for a single component axis and Grid for a layout with coordinated rows and columns.
- Decide what should happen when space becomes limited: wrap, stack, scroll, or change track sizes.
- Use
gapfor consistent spacing before reaching for child margins. - Test with long labels, narrow screens, zoom, and content in different languages.
- Check the result with keyboard navigation and a suitable accessibility inspection tool.
Checklist by scenario
Each template below is intentionally small. Add the HTML shown, then place the CSS in a stylesheet. The examples use semantic elements and responsive rules that can be adapted to a larger design system.
1. Header with navigation: Flexbox
Use Flexbox when the logo, navigation, and action area need alignment along one horizontal axis.
<header class="site-header">
<a href="/" class="logo">Brand</a>
<nav aria-label="Main navigation"><a href="/docs">Docs</a> <a href="/about">About</a></nav>
<a class="button" href="/start">Get started</a>
</header>
.site-header { display:flex; align-items:center; gap:1rem; padding:1rem; }
.site-header nav { display:flex; gap:1rem; margin-inline:auto; }
@media (max-width:42rem) {
.site-header { flex-wrap:wrap; }
.site-header nav { order:3; flex-basis:100%; margin-inline:0; }
}
2. Page shell with sidebar: Grid
Grid makes the relationship between the sidebar and main content explicit. The minmax() function allows the main column to shrink without creating a fixed-width layout.
<div class="page-shell"><aside>Filters</aside><main>Results</main></div>
.page-shell { display:grid; grid-template-columns:minmax(12rem, 18rem) minmax(0, 1fr); gap:2rem; }
@media (max-width:48rem) { .page-shell { grid-template-columns:1fr; } }
3. Responsive card gallery: Grid
This is a useful default for cards with similar importance. The browser creates as many columns as fit while keeping each card above the minimum width.
.card-grid {
display:grid;
grid-template-columns:repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
gap:1rem;
}
.card { padding:1rem; border:1px solid #ccc; border-radius:.5rem; }
4. Centered content panel: Flexbox
For a single panel centered in the viewport, Flexbox is direct and readable.
.center-stage {
min-height:100dvh;
display:flex;
align-items:center;
justify-content:center;
padding:1rem;
}
.panel { width:min(100%, 38rem); }
5. Media object: Flexbox
Use this pattern for an icon or thumbnail beside text. Allowing wrapping prevents narrow screens from forcing horizontal overflow.
.media { display:flex; align-items:flex-start; gap:1rem; flex-wrap:wrap; }
.media__image { flex:0 0 4rem; aspect-ratio:1; object-fit:cover; }
.media__body { flex:1 1 16rem; min-width:0; }
6. Form with aligned fields: Grid
Grid is helpful when labels and controls should share columns on wide screens, then collapse into one column.
<form class="settings-form">
<label for="name">Name</label><input id="name">
<label for="email">Email</label><input id="email" type="email">
<button>Save</button>
</form>
.settings-form { display:grid; grid-template-columns:max-content minmax(0, 1fr); gap:1rem; align-items:center; }
.settings-form button { grid-column:2; justify-self:start; }
@media (max-width:40rem) {
.settings-form { grid-template-columns:1fr; }
.settings-form button { grid-column:auto; }
}
7. Sticky footer page: Grid
Make the page a three-row grid so short pages still keep the footer at the bottom without positioning it over content.
body { min-height:100dvh; display:grid; grid-template-rows:auto 1fr auto; }
header, main, footer { padding:1rem; }
8. Wrapping toolbar: Flexbox
Flexbox naturally handles controls that need to wrap onto additional lines. Use margin-inline-start:auto to push a secondary action to the end when space permits.
.toolbar { display:flex; align-items:center; gap:.75rem; flex-wrap:wrap; }
.toolbar__secondary { margin-inline-start:auto; }
@media (max-width:30rem) { .toolbar__secondary { margin-inline-start:0; } }
9. Asymmetric feature layout: Grid
Named areas make an intentionally uneven composition easier to understand and change than a collection of positional offsets.
.feature { display:grid; grid-template-columns:1.4fr 1fr; grid-template-areas:"copy image"; gap:2rem; align-items:center; }
.feature__copy { grid-area:copy; }
.feature__image { grid-area:image; }
@media (max-width:48rem) {
.feature { grid-template-columns:1fr; grid-template-areas:"image" "copy"; }
}
10. Grid with a Flexbox card footer: combined layout
Combine the systems when each level has a different responsibility. Grid gives cards equal structure; Flexbox keeps actions aligned at the bottom.
.product-grid { display:grid; grid-template-columns:repeat(auto-fit, minmax(15rem, 1fr)); gap:1rem; }
.product-card { display:flex; flex-direction:column; gap:1rem; padding:1rem; border:1px solid #ccc; }
.product-card__actions { display:flex; gap:.5rem; margin-top:auto; flex-wrap:wrap; }
What to double-check
Alignment and sizing
- Set
min-width:0on flexible children that contain long text; the default minimum size can prevent expected shrinking. - Distinguish
align-itemsfromalign-content. The first aligns items within a line or track; the second distributes multiple lines or tracks. - Use
justify-contentfor leftover space in the main axis, but do not rely on it to create a consistent component gap. Prefergap. - Check whether a fixed height is necessary. Content-driven height is generally safer for translated text and user zoom.
Responsive behavior
- Choose a breakpoint based on where the content becomes uncomfortable, not on a device name.
- Test the smallest expected width and a wide viewport. Also test intermediate widths where wrapping often exposes problems.
- For component reuse, consider container queries when the component’s available space matters more than the viewport. Keep a straightforward fallback or progressive enhancement approach where your support requirements call for it.
- Use
subgridwhen nested components need to align with tracks supplied by a parent Grid, but verify the target browser range before depending on it.
Accessibility and source order
CSS placement should not make the reading or keyboard order confusing. Keep the HTML order logical, use headings and landmarks appropriately, and treat visual reordering as an exception rather than the default. A layout that looks correct but moves a primary action away from its logical position can be difficult to use with a keyboard or assistive technology.
Common mistakes
- Using Grid for every small row: A simple Flexbox rule is often clearer for a label-and-action row or a group of buttons.
- Using Flexbox for a page matrix: Nested flex containers can reproduce Grid, but the resulting alignment rules are harder to maintain when rows and columns must coordinate.
- Adding margins to every child: This creates edge-case rules when items wrap. Start with the parent’s
gap. - Forgetting overflow: Long URLs, code samples, and unbroken strings can widen a flexible item. Use appropriate wrapping and
min-width:0. - Hard-coding card widths: Fixed widths often leave awkward gaps or cause overflow. Try flexible tracks with
minmax(). - Relying on visual order: Do not use
orderor grid-area rearrangement to compensate for poorly structured HTML. - Debugging without an outline: Temporarily add
* { outline:1px solid }or inspect grid and flex overlays in browser developer tools to see the actual boxes.
When to revisit
Revisit this Flexbox vs Grid checklist whenever a layout is redesigned, a component is moved into a different container, or a project changes its supported browser range. CSS layout features and browser implementations continue to evolve, so confirm support for newer tools such as subgrid, container queries, and newer track-sizing functions before making them required foundations.
For an existing page, use this short review before shipping a refactor:
- Write down the intended reading order and compare it with the HTML order.
- Mark each relationship as one-dimensional or two-dimensional.
- Replace manual spacing with parent-level
gapwhere appropriate. - Test long content, narrow widths, zoom, keyboard navigation, and reduced-motion preferences where animation is present.
- Inspect overflow and computed sizes at the breakpoint where the layout changes.
- Record any browser-specific fallback or progressive enhancement decision in the project’s CSS documentation.
As a practical rule, start with the simplest model that expresses the design: Flexbox for a line, Grid for a coordinated arrangement, and both together when page structure and component alignment have different jobs. Save this cheatsheet with your frontend developer resources and return to it when a responsive layout begins fighting its content.