CSS Grid & Flexbox Cheat Sheet
Quick reference for CSS layout: Flexbox alignment, Grid template areas, auto-fit, gap, and when to reach for each layout model.
Flexbox is one-dimensional (a row or a column); Grid is two-dimensional (rows and columns). Rule of thumb: Flexbox for a streak of items or alignment, Grid for a full layout/matrix.
Flexbox Cheat Sheet
.flex-row {
display: flex;
flex-direction: row; /* row | column | row-reverse | column-reverse */
justify-content: flex-start; /* main axis */
align-items: stretch; /* cross axis */
gap: 1rem; /* spacing between items */
flex-wrap: wrap; /* nowrap | wrap | wrap-reverse */
}
| Property | Values | Effect |
|---|---|---|
justify-content | flex-start, center, flex-end, space-between, space-around, space-evenly | Main-axis alignment of the group |
align-items | stretch, flex-start, center, flex-end, baseline | Cross-axis alignment of each item |
align-content | same as justify + space-* | Cross-axis alignment for wrapped lines |
flex-grow | 0, 1, 2… | Share leftover space |
flex-shrink | 0, 1, 2… | How much to shrink when tight |
flex-basis | auto, 200px, 30% | Initial main size before grow/shrink |
align-self | overrides align-items per item | align-self: center |
order | integer | Reorder items visually |
Shortcut: flex: 1 = flex: 1 1 0%. flex: 0 0 auto = no grow, no shrink, natural size.
Grid Cheat Sheet
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr); /* three equal columns */
grid-template-rows: auto 1fr auto; /* header, content, footer */
gap: 1rem;
align-items: stretch;
justify-items: stretch;
}
| Property | Example | Meaning |
|---|---|---|
grid-template-columns | repeat(4, minmax(0, 1fr)) | Define column tracks |
grid-template-rows | auto 1fr auto | Define row tracks |
grid-template-areas | "header header" "nav main" | Name-based layout |
grid-auto-rows | minmax(100px, auto) | Size of implicit rows |
grid-column | 1 / -1 | Span from line 1 to the last |
grid-row | span 2 | Span two rows |
gap / row-gap / column-gap | 1rem | Track spacing |
justify-items / align-items | center | Align items inside their cell |
place-items | center | Shorthand for both |
Responsive Pattern: auto-fit / minmax
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 1rem;
}
This creates as many ≥240px columns as fit, then wraps — no media queries needed for card grids.
Grid Template Areas
.layout {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
.layout > header { grid-area: header; }
.layout > aside { grid-area: sidebar; }
.layout > main { grid-area: main; }
.layout > footer { grid-area: footer; }
Flex vs Grid — Decision Guide
| Need | Use |
|---|---|
| Align a few items in one line | Flexbox |
| Equal-width columns that wrap | Flexbox with flex: 1 or Grid auto-fit |
| Full-page layout (header/sidebar/main/footer) | Grid |
| Card grid that reflows | Grid repeat(auto-fit, minmax(...)) |
| Center a single item | Flexbox justify-content/align-items: center or Grid place-items: center |
| Overlapping elements | Grid (items can share tracks) |
Common Pitfalls
[!WARNING]
justify-contenton a flex container does nothing when the container isflex-wrapand items fill the line — align wrapped lines withalign-content.
[!TIP] Use
minmax(0, 1fr)instead of bare1frwhen grid items contain long content or embedded images, or they may overflow their track.