How to Use CSS Grid
What You’ll Build or Solve
You’ll create structured layouts using display: grid, define rows and columns, and control placement and spacing.
When This Approach Works Best
This approach works best when you:
Learn CSS on Mimo
- Build full-page layouts with header, sidebar, and content areas
- Create gallery or card grids with consistent spacing
- Align elements across both rows and columns
- Design dashboards with structured panels
Avoid Grid when you only need simple one-dimensional alignment. Flexbox handles rows or columns better when the layout complexity is low.
Prerequisites
- A basic HTML file
- A linked CSS file or
<style>block - Familiarity with
divelements and CSS selectors
Example structure used in this guide:
<divclass="container">
<divclass="item">A</div>
<divclass="item">B</div>
<divclass="item">C</div>
<divclass="item">D</div>
</div>
Step-by-Step Instructions
Step 1: Turn a Container into a Grid
Start by applying display: grid to the parent element.
CSS
.container {
display:grid;
}
By default, items stack vertically until you define columns.
Step 2: Define Columns and Rows
Use grid-template-columns to create column structure.
CSS
.container {
display:grid;
grid-template-columns:200px200px200px;
}
This creates three fixed-width columns.
For flexible columns:
CSS
.container {
display:grid;
grid-template-columns:1fr1fr1fr;
}
fr means fractional unit. Each column shares available space evenly.
You can also mix values:
CSS
grid-template-columns: 200px 1fr 1fr;
Rows are defined similarly:
CSS
grid-template-rows: 100px 200px;
If you do not define rows, Grid creates them automatically based on content.
Step 3: Add Spacing Between Items
Use gap to create consistent spacing.
CSS
.container {
display:grid;
grid-template-columns:repeat(3,1fr);
gap:1rem;
}
gap replaces manual margins and keeps spacing consistent.
Step 4: Control Item Placement
You can manually place items using column and row lines.
CSS
.item:first-child {
grid-column:1/3;
}
This makes the first item span across two columns.
You can also span rows:
CSS
.item:first-child {
grid-row:1/3;
}
What to look for:
- Grid lines start at 1
grid-column: 1 / 3spans two columns- Spanning works only within defined grid tracks
Step 5: Use repeat() and auto-fit for Responsive Layouts
Instead of fixed columns, use repeat().
CSS
.container {
display:grid;
grid-template-columns:repeat(3,1fr);
}
For responsive layouts:
CSS
.container {
display:grid;
grid-template-columns:repeat(auto-fit,minmax(200px,1fr));
gap:1rem;
}
This creates as many columns as fit, each at least 200px wide.
Use this pattern for card layouts that adapt to screen width.
Examples You Can Copy
Example 1: Simple Three-Column Layout
<divclass="grid">
<divclass="box">One</div>
<divclass="box">Two</div>
<divclass="box">Three</div>
</div>
CSS
.grid {
display:grid;
grid-template-columns:repeat(3,1fr);
gap:1rem;
}
Example 2: Page Layout with Header and Sidebar
CSS
<divclass="layout">
<header>Header</header>
<aside>Sidebar</aside>
<main>Content</main>
</div>
CSS
.layout {
display:grid;
grid-template-columns:200px1fr;
grid-template-rows:100px1fr;
gap:1rem;
}
header {
grid-column:1/3;
}
The header spans both columns.
Example 3: Responsive Card Grid
<divclass="cards">
<divclass="card">A</div>
<divclass="card">B</div>
<divclass="card">C</div>
<divclass="card">D</div>
</div>
CSS
.cards {
display:grid;
grid-template-columns:repeat(auto-fit,minmax(250px,1fr));
gap:1rem;
}
Cards wrap automatically as the screen size changes.
Example 4: Dashboard Panel Layout
<divclass="dashboard">
<divclass="panel large">Stats</div>
<divclass="panel">Chart</div>
<divclass="panel">Activity</div>
</div>
CSS
.dashboard {
display:grid;
grid-template-columns:repeat(3,1fr);
gap:1rem;
}
.large {
grid-column:1/4;
}
The large panel spans the full width.
Common Mistakes and How to Fix Them
Mistake 1: Forgetting display: grid
What you might do:
CSS
.container {
grid-template-columns:1fr1fr;
}
Why it breaks:
Grid properties only work when the container uses display: grid.
Correct approach:
CSS
.container {
display:grid;
grid-template-columns:1fr1fr;
}
Mistake 2: Confusing fr with Percentages
What you might do:
CSS
grid-template-columns: 50% 50%;
Expecting flexible resizing like fr.
Why it breaks:
Percentages depend on container width and can cause overflow when combined with gap.
Correct approach:
CSS
grid-template-columns: 1fr 1fr;
fr distributes remaining space automatically.
Troubleshooting
If items stack vertically, confirm you defined grid-template-columns.
If spacing looks inconsistent, check gap and child margins.
If spanning does not work, confirm grid lines exist within defined columns.
If layout breaks on mobile, review fixed column widths and consider auto-fit with minmax.
If styles do not apply, confirm your CSS file is linked correctly.
Quick Recap
- Apply
display: gridto the parent - Define columns with
grid-template-columns - Use
gapfor spacing - Span items using
grid-columnorgrid-row - Use
auto-fitandminmaxfor responsive layouts
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot