CSS Grid is a two-dimensional layout system: it controls rows and columns at the same time. You define a grid on the parent, and items drop into its cells or span several of them. Grid is the most direct way to build whole page layouts, dashboards and galleries, and with auto-fit it can be responsive without a single media query.
In this lesson you will learn
- How to define columns and rows
- The
frunit andrepeat() - Making items span several cells
- Responsive grids with
auto-fitandminmax() - Named layouts with
grid-template-areas
Columns, fr Units and Spanning
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Grid</title>
<style>
.grid { display: grid; grid-template-columns: 1fr 2fr 1fr; gap: 8px; margin-bottom: 10px; }
.grid div, .auto div { background: #00695c; color: white; padding: 10px; border-radius: 6px; }
.wide { grid-column: span 2; }
.auto { display: grid; grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); gap: 8px; }
</style>
</head>
<body>
<div class="grid"><div>1fr</div><div>2fr</div><div>1fr</div><div class="wide">spans 2 columns</div><div>1fr</div></div>
<div class="auto"><div>A</div><div>B</div><div>C</div><div>D</div><div>E</div></div>
</body>
</html>
Result
display: gridmakes a grid container.grid-template-columns: 1fr 2fr 1frcreates three columns. Thefrunit means a fraction of the free space, so the middle column is twice as wide as the others.- Items fill cells left to right, then move to the next row automatically.
grid-column: span 2makes an item two columns wide.repeat(auto-fit, minmax(100px, 1fr))fits as many columns of at least 100px as the width allows, then stretches them to fill the row. Narrow your window to watch the second grid reflow.
Page Layout with Named Areas
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Grid areas</title>
<style>
body { margin: 0; font-family: sans-serif; }
.page {
display: grid;
grid-template-columns: 150px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 6px; height: 220px; padding: 6px;
}
.page > * { padding: 8px; border-radius: 6px; }
header { grid-area: header; background: #b2dfdb; }
nav { grid-area: sidebar; background: #e0f2f1; }
main { grid-area: main; background: #fff8e1; }
footer { grid-area: footer; background: #eceff1; }
</style>
</head>
<body>
<div class="page">
<header>Header</header>
<nav>Sidebar</nav>
<main>Main content</main>
<footer>Footer</footer>
</div>
</body>
</html>
Result
grid-template-areasdraws the layout as text. Each quoted string is a row, and repeating a name makes an area span several cells.- Each element joins its area with
grid-area: header;and so on. - To change the layout on phones, redefine the areas in a media query, for example stacking everything in one column.
@media (max-width: 600px) {
.page {
grid-template-columns: 1fr;
grid-template-areas: "header" "main" "sidebar" "footer";
}
}
Grid Cheat Sheet
| Property | Example |
|---|---|
grid-template-columns | 200px 1fr, repeat(3, 1fr) |
grid-template-rows | auto 1fr auto |
gap | 16px, or row-gap / column-gap |
grid-column / grid-row | span 2, 1 / 3 (from line 1 to line 3) |
place-items | center centres every item in its cell |
Common Mistakes
- Area names that do not form rectangles, which makes the whole template invalid.
- Confusing grid lines with cells: three columns have four lines, so the full width is
1 / 4or1 / -1. - Using
auto-fillwhen you wantedauto-fit:auto-fillkeeps empty columns,auto-fitcollapses them so items stretch. - Setting display: grid on the wrong element; it belongs on the direct parent of the items.
Practice Exercises
Exercise 1. Create a gallery with 4 equal columns and 10px gaps.
Show answer
.gallery {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
Exercise 2. Make the first gallery item fill the whole first row.
Show answer
.gallery > :first-child { grid-column: 1 / -1; }
Frequently Asked Questions
Can I use Grid and Flexbox together?
Yes, and it is common: Grid for the overall page, Flexbox inside individual components such as a navigation bar or a card’s footer.
Is CSS Grid supported in all browsers?
Yes. All modern browsers have supported CSS Grid since 2017.
Summary
display: gridcontrols rows and columns together.frshares free space;repeat()avoids repetition.spanand line numbers make items cover several cells.auto-fitwithminmax()and named areas make responsive layouts simple.
Next: CSS Positioning.
