CSS Reference
grid
The two-dimensional layout system. Arrange items in rows and columns simultaneously.
Last updated: October 2026
Definition: CSS Grid is a two-dimensional layout system. Set display: grid on a parent, define columns and rows, and place items anywhere in the grid. It is the most powerful layout system in CSS and the standard for page-level layouts and card grids.
🎬 Live Examples
Four Grid patterns that cover most real-world use cases.
💡 Every layout above is built with the same core Grid properties.
Basic Syntax
.grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* 3 equal columns */
gap: 1rem;
}
The parent becomes a grid container. Children become grid items placed automatically in the defined columns.
🎯 Container Properties (Parent)
| Property | Common Values | Purpose |
|---|---|---|
display | grid / inline-grid | Enable Grid. |
grid-template-columns | repeat(3, 1fr), 200px 1fr, etc. | Define column tracks. |
grid-template-rows | auto 1fr auto, etc. | Define row tracks. |
gap | any length | Space between tracks. |
grid-auto-flow | row / column / dense | Auto-placement direction. |
justify-items | stretch / start / center / end | Align items within their cells (horizontal). |
align-items | stretch / start / center / end | Align items within their cells (vertical). |
🎯 Item Properties (Children)
| Property | Purpose |
|---|---|
grid-column | span 2, 1 / 3. Position/span across columns. |
grid-row | span 2, 1 / 3. Position/span across rows. |
grid-area | Shorthand for row-start / column-start / row-end / column-end. |
justify-self | Override justify-items for this item. |
align-self | Override align-items for this item. |
Equal Columns with repeat()
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
repeat(3, 1fr) is shorthand for 1fr 1fr 1fr. Three equal columns.
Change the number for different counts:
grid-template-columns: repeat(2, 1fr); /* 2 columns */
grid-template-columns: repeat(4, 1fr); /* 4 columns */
grid-template-columns: repeat(6, 1fr); /* 6 columns */
The Responsive Auto-fit Grid
One line of CSS that replaces dozens of media queries.
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1rem;
}
How it works:
auto-fit— create as many columns as will fit.minmax(250px, 1fr)— each column is at least 250px, growing to fill space.- No media queries required. The grid responds to the container width automatically.
Spanning Items Across Tracks
/* Span 2 columns */
.featured {
grid-column: span 2;
}
/* Span 2 rows */
.tall {
grid-row: span 2;
}
/* Explicit positioning: from column line 1 to column line 3 */
.hero {
grid-column: 1 / 3;
}
/* Full-width: from the first to the last column line */
.full-width {
grid-column: 1 / -1;
}
grid-column: 1 / -1 is the "full-width" pattern — the item stretches across every column.
Full Page Layout with Grid
The classic header / sidebar / main / footer layout.
.layout {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100vh;
gap: 1rem;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
Named grid areas make complex layouts readable. Change the ASCII diagram to change the layout.
Responsive Stack → Grid
.layout {
display: grid;
grid-template-columns: 1fr; /* Mobile: stack */
gap: 1rem;
}
@media (min-width: 1024px) {
.layout {
grid-template-columns: 250px 1fr; /* Desktop: sidebar + main */
}
}
🎯 Grid vs Flex — Decision Guide
| Scenario | Use |
|---|---|
| Full page layout (header, sidebar, main) | Grid |
| Card grid with equal rows and columns | Grid |
| Photo gallery with alignment | Grid |
| Overlapping content | Grid |
| Navbar layout | Flex |
| Button with icon + text | Flex |
| Centering content | Flex |
⚠️ Common Mistakes
- Forgetting
display: gridon the parent. Grid properties do nothing without it. - Using
1fr 1fr 1frinstead ofrepeat(3, 1fr). Both work, but repeat is more readable. - Confusing
auto-fitandauto-fill. auto-fit collapses empty tracks. auto-fill keeps them. For card grids, use auto-fit. - Setting a fixed width on grid items. Use grid-column/template to control sizes instead.
- Expecting grid-auto-flow to place things where you want. Auto-placement fills left-to-right, top-to-bottom. Use explicit positioning for control.
- Using Grid for simple 1D layouts. Flexbox is simpler and lighter for navbars and buttons.
- Not testing on mobile. Grid layouts can overflow. Always test with narrow viewports.
- Forgetting
min-height: 0on grid items. Items can overflow their tracks if content is too large. Add min-height: 0 to fix.
♿ Accessibility Notes
- Visual order should match DOM order. Grid lets you place items anywhere. If visual order differs from DOM order, keyboard users get confused.
- Test with keyboard navigation. Tab through the grid. Order should be logical and match the visual layout.
- Semantic HTML still matters. A grid
<div>is still a div. Use<nav>,<main>, etc. for structure. - Don't use Grid for tables. If the data is tabular, use
<table>. Grid is for visual layout, not data. - Test with a screen reader. Grid layouts are announced in DOM order, not visual order.
❓ Frequently Asked Questions
What is CSS Grid?
CSS Grid is a two-dimensional layout system for arranging items in rows and columns simultaneously. Set display: grid on a parent, then define columns and rows with grid-template-columns and grid-template-rows.
What is the difference between grid and flex?
Grid is two-dimensional (rows AND columns) and lets you place items anywhere in the grid. Flex is one-dimensional (row OR column) and arranges items in sequence. Use Grid for page layouts and card grids, Flex for components like navbars and buttons.
What does grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)) do?
It creates a responsive grid that automatically fits as many 250px-minimum columns as possible into the available width. No media queries required. Each column stretches evenly with 1fr.
What is the difference between auto-fit and auto-fill?
auto-fit collapses empty columns and stretches items to fill the row. auto-fill keeps empty columns, leaving whitespace. Use auto-fit for card grids where you want items to fill the space.
How do I span an item across multiple columns?
Use grid-column: span 2 (or the number of columns you want to span). In Tailwind, use col-span-2. The item will extend across that many grid tracks.