57 entries · 7 sections
CSS Grid Cheat Sheet
CSS Grid from the container down: defining tracks, the sizing functions that make grids responsive without media queries, placing and spanning items, named template areas, alignment, and the differences that trip people up.
Creating a grid
| Syntax | What it does |
|---|---|
display: grid | Make the element a block-level grid container. |
display: inline-grid | A grid container that flows inline with text. |
grid-template-columns: 200px 1fr | A fixed sidebar and a flexible main column. |
grid-template-columns: repeat(3, 1fr) | Three equal columns. |
grid-template-rows: auto 1fr auto | Header, growing body, footer. |
gap: 16px | Space between tracks. Older code calls it grid-gap. |
row-gap / column-gap | Control the two gaps separately. |
grid-auto-flow: column | Place new items into new columns rather than new rows. |
grid-auto-flow: dense | Backfill the holes that spanning items leave behind. It can reorder items visually. |
Sizing tracks
| Syntax | What it does |
|---|---|
1fr | One share of the free space left after fixed tracks and gaps. |
2fr 1fr | Two columns where the first gets twice the free space of the second. |
minmax(200px, 1fr) | At least 200px, then flexible up to a share of the free space. |
min-content | As narrow as the content can be without overflowing. |
max-content | As wide as the content wants, with no wrapping. |
fit-content(300px) | Size to the content, capped at 300px. |
auto | Size to the content, but stretch to absorb leftover space. |
repeat(auto-fill, minmax(200px, 1fr)) | As many 200px-or-wider columns as fit. Empty tracks are kept. |
repeat(auto-fit, minmax(200px, 1fr)) | The same, but empty tracks collapse so the real items stretch to fill the row. |
grid-auto-rows: minmax(100px, auto) | The size of rows the browser creates beyond your template. |
grid-auto-columns: 1fr | The size of implicitly created columns. |
Placing items
| Syntax | What it does |
|---|---|
grid-column: 1 / 3 | Start at line 1 and end at line 3 — a two-column span. Lines, not cells. |
grid-column: span 2 | Span two columns from wherever the item lands. |
grid-column: 1 / -1 | First line to last line: full width, whatever the column count turns out to be. |
grid-row: 2 / 4 | Occupy rows two and three. |
grid-area: 1 / 1 / 3 / 3 | row-start / column-start / row-end / column-end, in that order. |
grid-column-start / grid-column-end | The longhand pair, when a shorthand is harder to read. |
grid-row: span 2 / auto | Span two rows and let the browser choose where to start. |
z-index on a grid item | Grid items are allowed to overlap; z-index decides which is on top. |
Named areas and lines
| Syntax | What it does |
|---|---|
grid-template-areas: "head head" "side main" | Draw the layout as a picture in the stylesheet. |
grid-area: head | Assign an item to a named area. |
"head head" "side main" "foot foot" | The classic three-row application shell. |
a . in an area string | A dot leaves that cell deliberately empty. |
grid-template: "head head" 60px "side main" 1fr / 200px 1fr | Areas, row sizes and column sizes in a single declaration. |
[main-start] 1fr [main-end] | Name the grid lines themselves inside a track list. |
grid-column: main | Place an item between the lines named main-start and main-end. |
Alignment
| Syntax | What it does |
|---|---|
justify-items: center | Align every item inside its own cell along the inline axis. |
align-items: center | Align every item inside its own cell along the block axis. |
place-items: center | Both at once — the shortest genuine centring in CSS. |
justify-content: center | Position the whole grid inside the container when the tracks are narrower than it. |
align-content: space-between | Distribute the rows inside a container that is taller than the grid. |
place-content: center | Both content alignments in one declaration. |
justify-self / align-self | Override the alignment for a single item. |
place-self: end start | Both self alignments at once, block axis first. |
Recipes
| Syntax | What it does |
|---|---|
display: grid; place-items: center; min-height: 100dvh | A perfectly centred, full-height hero. |
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)) | A responsive card grid with no media queries at all. |
grid-template-columns: minmax(0, 1fr) | Stops a child with long unbreakable content from blowing the track out. |
grid-template-rows: auto 1fr auto; min-height: 100dvh | Header, content and footer filling the viewport, with the footer pinned down. |
grid-template-areas: "stack"; children all set grid-area: stack | Stack children on top of each other without absolute positioning. |
grid-column: 1 / -1 on one card | A full-width row inside an otherwise regular card grid. |
aspect-ratio: 1 on the items | Square cells regardless of what is inside them. |
Gotchas
| Syntax | What it does |
|---|---|
1fr is not minmax(0, 1fr) | 1fr has a min-content floor, which is why a long word can push a track wider than its share. |
auto-fill versus auto-fit | auto-fill keeps empty tracks so items stay their base width; auto-fit collapses them so items stretch. |
grid-gap | The old name for gap. gap is the current one and is supported everywhere. |
Ragged grid-template-areas | Every row string must contain the same number of cells or the whole declaration is ignored. |
Implicit tracks | Items placed past your template create rows sized by grid-auto-rows, not by grid-template-rows. |
Margins do not collapse | Adjacent margins inside a grid container behave independently, unlike in block layout. |
Percentage gaps | Resolve against the container's own size on that axis, which can surprise you inside a flexible parent. |
Worth knowing
- repeat(auto-fit, minmax(240px, 1fr)) is the single most useful line in CSS Grid: it produces a card gallery that adds and removes columns as the container resizes, with no breakpoints to maintain. Swap auto-fit for auto-fill when you want the leftover columns to stay empty rather than letting the items stretch.
- Grid lines are numbered from 1, and there is always one more line than there are tracks. grid-column: 1 / 3 therefore spans two columns, not three, and grid-column: 1 / -1 spans the whole row no matter how many columns exist.
- Firefox DevTools has the best grid inspector of any browser: it overlays line numbers, track sizes and area names directly on the page. When a grid is not doing what you expect, opening that overlay is faster than reasoning about the CSS.
Try a snippet instead of reading about it
Reference is faster when you can run the thing. Paste a snippet into AltF Code, the free in-browser editor — real syntax highlighting for HTML, CSS, JavaScript, Python, SQL, JSON and more, with HTML, CSS and JavaScript running live in the preview. No install and no sign-up.
Open the editorCSS Grid — common questions
- What is the difference between auto-fit and auto-fill?
- Both repeat as many tracks as will fit. auto-fill keeps the empty tracks it created, so a single item stays its minimum width with blank space beside it. auto-fit collapses the empty tracks to zero, so the real items stretch to fill the row. For card grids people almost always want auto-fit.
- What does the fr unit actually mean?
- A fraction of the free space remaining after fixed-size tracks and gaps have been subtracted. In 200px 1fr 2fr, the two flexible tracks split whatever is left one-to-two. Note that 1fr also has an automatic minimum of min-content, so use minmax(0, 1fr) when you need a track that can genuinely shrink.
- How do I make a responsive grid without media queries?
- Use grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)) with a gap. The browser fits as many 240px-minimum columns as the container allows and stretches them to share the width, so the layout responds to the container rather than to the viewport — which also works inside a sidebar.
- Should I use CSS Grid or Flexbox?
- Grid when you are describing a two-dimensional structure up front — rows and columns that should line up. Flexbox when you are distributing items along one axis and want the content to drive the sizes. Most real pages use both: grid for the page skeleton, flex inside the components.