CSS Grid
CSS Grid lays out content in rows and columns at the same time. It is ideal for page layouts and card galleries.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 1, 2026
Related tutorials
Learning objectives#
- Define columns and rows with grid-template-columns
- Use the fr unit, repeat() and minmax()
- Build a responsive card grid without media queries
- Name areas with grid-template-areas
Explanation#
display: grid creates a grid container. grid-template-columns defines the columns; rows are created automatically as needed.
The fr unit means “one fraction of the free space”. 1fr 2fr gives the second column twice the width of the first.
repeat(auto-fill, minmax(220px, 1fr)) creates as many columns as fit, each at least 220px wide — a fully responsive grid in one line.
Syntax#
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
gap: 24px;
}
Code example: Holy-grail layout with named areas#
<style>
body { margin:0; font-family:sans-serif; }
.page {
display:grid; min-height:100vh; gap:8px;
grid-template-columns: 180px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas: "header header" "sidebar main" "footer footer";
}
.page > * { padding:16px; background:#eef2ff; }
header { grid-area: header; } aside { grid-area: sidebar; }
main { grid-area: main; } footer { grid-area: footer; }
@media (max-width: 600px) {
.page { grid-template-columns: 1fr; grid-template-areas: "header" "main" "sidebar" "footer"; }
}
</style>
<div class="page">
<header>Header</header><aside>Sidebar</aside><main>Main content</main><footer>Footer</footer>
</div>
A header across the top, a sidebar and main area side by side, and a footer. Below 600px the sidebar moves under the main content.
Step-by-step explanation#
- grid-template-columns: 180px 1fr creates a fixed sidebar column and a flexible main column.
- grid-template-rows: auto 1fr auto lets the middle row stretch to fill the height.
- grid-template-areas draws the layout with names; each child is placed with grid-area.
- The media query redefines the areas as a single column for narrow screens.
Real-world use case#
Dashboards, documentation layouts (like this page), photo galleries and product listings are all natural Grid layouts.
Common mistakes#
Only direct children of the grid container are placed on the grid.
Each string in grid-template-areas must have the same number of cells, and named areas must be rectangles.
Best practices#
- Use repeat(auto-fill, minmax()) for responsive card grids
- Use named areas for page-level layouts
- Combine Grid for the page with Flexbox inside components
- Keep the source order logical — visual reordering does not change keyboard order
Remember this#
Grid = rows and columns together.
grid-template-columns: repeat(3, 1fr).
Flexbox for components, Grid for page layout.
Practice exercise#
Your task: Create a gallery where each card is at least 160px wide, with 12px gaps, that adapts to any screen width.
Show solution
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
Mini quiz#
What does 1fr represent?
Want more? Practise all CSS MCQs · CSS interview questions
My notes#
Saved in this browser.