What is CSS Grid?
CSS Grid is a two-dimensional layout system — unlike Flexbox, it lets you control rows and columns at the same time, making it ideal for full page layouts and card grids.
A Basic Grid
<style>
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.grid div {
background: steelblue;
color: white;
padding: 20px;
text-align: center;
}
</style>
<div class="grid">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>Key Grid Properties
| Property | Purpose |
|---|---|
| grid-template-columns | Defines the number and size of columns |
| grid-template-rows | Defines the number and size of rows |
| gap | Space between rows and columns |
| grid-column / grid-row | Places an item across specific tracks |
| grid-template-areas | Names layout regions for readable placement |
The fr Unit
fr represents a fraction of the available space in the grid container. grid-template-columns: 1fr 2fr creates two columns, the second twice as wide as the first.
Spanning Multiple Columns
<style>
.layout {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
.featured {
grid-column: span 2;
background: #764ba2;
color: white;
padding: 20px;
}
.layout div:not(.featured) {
background: #eef4fb;
padding: 20px;
}
</style>
<div class="layout">
<div class="featured">Spans 2 columns</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
</div>Named Grid Areas
grid-template-areas lets you visually sketch a layout in your CSS using named regions, then assign each child to a named area with grid-area.
Named Areas Layout
<style>
.page {
display: grid;
grid-template-columns: 150px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 8px;
height: 220px;
}
.header { grid-area: header; background: #333; color: white; }
.sidebar { grid-area: sidebar; background: #764ba2; color: white; }
.main { grid-area: main; background: #eef4fb; }
.footer { grid-area: footer; background: #333; color: white; }
.page > div { padding: 8px; }
</style>
<div class="page">
<div class="header">Header</div>
<div class="sidebar">Sidebar</div>
<div class="main">Main</div>
<div class="footer">Footer</div>
</div>Grid vs Flexbox
Use Grid when you need to control both rows and columns together, like a full page layout. Use Flexbox for simpler, one-directional alignment, like a navbar or a row of buttons. The two are often combined in the same project.
Best Practice
Use repeat() and the fr unit for flexible column definitions instead of hardcoding many equal-width tracks by hand — it’s shorter and adapts more gracefully.