CSS Grid is a two-dimensional layout system for rows and columns. Set display: grid on a container, define columns with grid-template-columns (for example repeat(3, 1fr)), add spacing with gap, and place items by line numbers or named grid-template-areas. The pattern repeat(auto-fit, minmax(250px, 1fr)) creates responsive card grids without media queries.
CSS Grid is one of those tools that changes how you think about layout. Instead of nudging boxes around with floats or nesting flex containers inside flex containers, you describe the layout you want — columns, rows and areas — and the browser places everything for you.
If flexbox is perfect for lining things up in a row or column, grid is perfect for whole page layouts, galleries and card grids. Together they cover almost every layout you will build.
Your first CSS grid
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 24px;
}
The fr unit shares available space. 1fr 2fr makes the second column twice as wide as the first.

Responsive cards without media queries
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
}
Each card is at least 250px wide; the browser fits as many columns as possible and wraps the rest.

Placing and spanning items
.feature {
grid-column: 1 / 3; /* from line 1 to line 3 */
grid-row: span 2; /* span two rows */
}
Named grid areas
.page {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 16px;
}
header { grid-area: header; }
aside { grid-area: sidebar; }
main { grid-area: main; }
footer { grid-area: footer; }
@media (max-width: 768px) {
.page {
grid-template-columns: 1fr;
grid-template-areas: "header" "main" "sidebar" "footer";
}
}
Key grid properties
| Property | What it does |
|---|---|
grid-template-columns / rows |
Define track sizes |
gap |
Space between rows and columns |
grid-column / grid-row |
Place or span items |
grid-template-areas |
Name layout regions |
justify-items / align-items |
Align items inside cells |
place-items |
Shorthand for both alignments |
subgrid |
Lets nested grids align to the parent’s tracks |

Grid vs flexbox
Use grid for two-dimensional page and card layouts; use flexbox for one-dimensional rows or columns such as navigation bars and button groups. They work well together. See CSS flexbox.
Tip: Open your browser’s developer tools and enable the grid overlay to see line numbers and areas while you build.
Real-world layouts to practise
- Blog layoutHeader, main content, sidebar and footer using named areas.
- Product gridResponsive cards with repeat(auto-fit, minmax()).
- Magazine homepageA featured article spanning two columns and two rows.
- Photo galleryMixed image sizes with grid-auto-flow: dense.
- DashboardStats cards above a chart and a table.
Common grid mistakes
| Problem | Fix |
|---|---|
| Content overflows narrow columns | Use minmax(0, 1fr) instead of 1fr for text-heavy columns |
| Layout breaks on mobile | Use auto-fit/minmax or redefine template areas in a media query |
| Reordered items confuse keyboard users | Keep visual order close to HTML order |
| Fixed heights clip content | Prefer auto rows or minmax(min-content, auto) |
Tip: Use the grid inspector in Firefox or Chrome DevTools to see line numbers and named areas overlaid on your page.
More CSS: CSS guides and CSS animation.
Frequently asked questions
What is CSS Grid used for?
CSS Grid creates two-dimensional layouts with rows and columns, such as page layouts, galleries and card grids.
Should I use grid or flexbox?
Use grid for two-dimensional layouts and flexbox for single rows or columns. Many layouts use both.
What does 1fr mean in CSS Grid?
fr is a fraction of the available space. Three columns of 1fr share the space equally.
Is CSS Grid supported in all browsers?
Yes, all modern browsers support CSS Grid, and subgrid is supported in current versions of major browsers.
Can I use CSS Grid in WordPress themes?
Yes. Modern WordPress themes and the block editor use grid for layouts, and you can add grid CSS to custom themes or blocks.
Does CSS Grid replace Bootstrap’s grid?
For many projects, yes. Native CSS Grid handles responsive layouts without a framework, though some teams still use framework grids for consistency.
What is subgrid?
Subgrid lets a nested grid align its rows or columns with its parent grid, useful for lining up content across cards.
Further reading: MDN’s web development learning area.







