CSS Flexbox vs Grid: When to Use Which (With Visual Examples)
The golden rule: Flexbox is one-dimensional (handles rows OR columns with content-first sizing), while CSS Grid is two-dimensional (handles rows AND columns simultaneously with track-first layout). Use Flexbox for linear components like navbars, badge clusters, and media objects; use CSS Grid for page layouts, card galleries, and dashboards.
Step 1: The One-Dimensional vs Two-Dimensional Model
Flexbox places items along a single axis (either main-axis horizontal or vertical). When items wrap onto a second line in flexbox, the second row does not know about the widths in the first row. CSS Grid, in contrast, defines rigid columns and rows simultaneously, forcing items into coordinated 2D tracks.
/* 1D: Flex items size based on their individual content */
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
gap: 16px;
}
/* 2D: Grid items strictly adhere to declared track templates */
.dashboard-grid {
display: grid;
grid-template-columns: 260px 1fr;
grid-template-rows: auto 1fr;
gap: 20px;
}
Step 2: Responsive Cards Without Media Queries (Grid Auto-Fit)
One of CSS Grid's greatest superpowers is creating completely responsive card grids that automatically calculate the optimal number of columns based on available container width, eliminating the need for hardcoded @media breakpoints.
.card-catalog {
display: grid;
/* Auto-fill columns, each minimum 280px, maximum 1fr */
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 24px;
}
Step 3: When to Combine Flexbox and Grid Together
Professional front-end architectures never treat Flexbox and Grid as rivals. Use CSS Grid for the macro layout (sidebar, header, content sections, and card grids), and use Flexbox inside each card or widget for micro layout (avatar + text alignment, tag pills, and button action bars).
/* Macro Grid Structure */
.product-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
}
/* Micro Flex Alignment Inside Each Card */
.product-card {
display: flex;
flex-direction: column;
justify-content: space-between;
}
Next-Level Addons: Modern Layout Architecture & Design Tokens
Mastering Flexbox and Grid is only step one. Production websites combine layout engines with typography clamping and systematic spacing:
A trick I use in almost every project: don't fight specificity battles with !important. Leverage modern CSS cascade layers (@layer) and CSS variables as design tokens—it makes dark/light theme switching and responsive tuning effortless.
Frequently Asked Questions
Practical answers to common questions about 1D vs 2D alignment, auto-fit responsive tracks, and CSS Subgrid.
Can I use CSS Grid and Flexbox together in the same component?
Yes! In modern CSS architectures, combining both is standard practice. CSS Grid excels at outer page layouts and 2D card matrices, while Flexbox excels at aligning text, icons, and buttons inside individual cards.
What is the difference between justify-content and align-items?
In Flexbox, 'justify-content' aligns children along the main axis (horizontal by default), while 'align-items' aligns them along the cross axis (vertical by default). If you switch to 'flex-direction: column', these axes swap roles. In CSS Grid, 'justify-items' aligns horizontally across columns, while 'align-items' aligns vertically across rows.
What is CSS Subgrid and when should you use it?
CSS Subgrid allows nested child elements to inherit and align directly with the parent grid's column or row tracks (using grid-template-rows: subgrid). It solves the age-old problem of card footers or titles having mismatched heights in responsive card grids.
When should you choose auto-fit vs auto-fill in CSS Grid?
With repeat(auto-fit, minmax(250px, 1fr)), existing grid items stretch across the full container width when there are fewer items than columns. With repeat(auto-fill, ...), the browser creates empty placeholder tracks to preserve the column width without expanding existing items.
Why does flex-basis: 0 behave differently from flex-basis: auto?
With flex-basis: auto, the browser factors in the intrinsic content width of each child before distributing remaining space, which can cause unequal column widths. With flex-basis: 0 (or flex: 1), intrinsic width is ignored and available space is divided completely equally.