Grid vs Flexbox: the one-line rule
Both lay out elements, but they’re built for different jobs:
- Grid is two-dimensional — you control rows and columns at once. Reach for it for page layouts, dashboards, card galleries, and anything with structure in both axes.
- Flexbox is one-dimensional — items flow in a single row or column. Reach for it for toolbars, button groups, and aligning a component’s contents.
A useful heuristic: Grid for the macro layout of the page, Flexbox for the micro alignment inside components. They compose well — a grid cell is often a flex container.
The fr unit and responsive grids without media queries
The fr (fraction) unit splits leftover space proportionally: grid-template-columns: 1fr 2fr 1fr gives the middle column twice the width of the outer two. Combined with repeat(), minmax(), and auto-fit, you get responsiveness for free:
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
This fits as many ≥250px columns as the container allows and wraps to fewer on narrow screens — no breakpoints required. (auto-fit collapses empty tracks; auto-fill keeps them.)
Common pitfalls
| Symptom | Cause & fix |
|---|---|
| Items overflow the container | Fixed px columns exceed width — use minmax(0, 1fr) |
fr rows have no height | Container has no height — give it one (100vh, etc.) |
| Item won’t span as expected | span exceeds track count, or implicit tracks appear |
The minmax(0, 1fr) trick deserves a highlight: a bare 1fr track has a minimum size of its content, so a wide child (a long word, a <pre>) can blow out the layout. The 0 minimum lets the track shrink and prevents the overflow.
Build it visually
Set columns, rows, gaps, and alignment with the live preview here to get clean copy-ready CSS, then layer on the techniques above — name your areas for readable page layouts, use auto-fit + minmax for responsive card grids, and reach for subgrid when nested components need to align. To fine-tune track sizes in rem vs px, the CSS Units Converter pairs well.