CSS Grid Layout

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 4 Examples
Layout

What You’ll Learn

CSS Grid lets you build two-dimensional layouts with rows and columns. Define a grid container, place items in cells, and create responsive page layouts without floats or positioning hacks.

01

Container

display: grid.

02

Columns

grid-template.

03

Areas

Named regions.

04

Span

grid-column.

05

fr unit

Flexible size.

06

2D layout

Rows + cols.

Introduction

The CSS Grid Layout Module is a powerful system for creating web layouts with rows and columns. Unlike Flexbox, which handles one dimension at a time, Grid controls both dimensions simultaneously.

What Is CSS Grid?

CSS Grid divides a container into tracks (rows and columns) and places child elements into cells. You get precise control over placement, spanning, gaps, and responsive reflow.

Grid is ideal for page layouts (header, sidebar, main, footer), photo galleries, dashboards, and card grids that need to adapt to screen size.

💡
Beginner Tip

Start with display: grid, grid-template-columns: repeat(3, 1fr), and gap: 1rem. That alone creates a clean three-column layout. Add grid-template-areas when you need named page regions.

Basic Concepts

  • Grid container — Parent with display: grid.
  • Grid item — Direct child placed on the grid.
  • Grid tracks — Rows and columns that form the grid.
  • Grid lines — Dividing lines between tracks (numbered from 1).
  • Grid area — Named rectangular region spanning multiple cells.

📝 Syntax

Define a basic three-column grid with equal-width tracks:

grid-basic.css
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

Name layout regions with grid-template-areas:

grid-areas.css
.layout {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
}

.header { grid-area: header; }

Key Container Properties

PropertyPurpose
display: gridCreates a grid container
grid-template-columnsDefine column sizes: px, %, fr, repeat()
grid-template-rowsDefine row sizes
grid-template-areasNamed layout regions
gapSpace between rows and columns
justify-items / align-itemsAlign items inside their cells

Key Item Properties

PropertyPurpose
grid-columnWhich columns an item spans (e.g. 1 / 3 or span 2)
grid-rowWhich rows an item spans
grid-areaAssign to a named area from grid-template-areas
justify-self / align-selfAlign one item inside its cell
display: grid repeat(3, 1fr) auto-fit minmax()

⚡ Quick Reference

TaskCSS
Enable griddisplay: grid;
3 equal columnsgrid-template-columns: repeat(3, 1fr);
Gap between cellsgap: 1rem;
Span 2 columnsgrid-column: span 2;
Full width itemgrid-column: 1 / -1;
Responsive columnsrepeat(auto-fit, minmax(200px, 1fr))
Named page layoutgrid-template-areas + grid-area

When to Use CSS Grid

  • Page layouts — Header, sidebar, main content, and footer in one grid.
  • Card galleries — Responsive rows of cards with auto-fit.
  • Dashboards — Widgets placed in specific rows and columns.
  • Overlapping elements — Items can share the same cell area when needed.
  • Equal-height columns — Grid tracks naturally align row heights.

Use Flexbox for one-dimensional flows like navbars. Use Grid when you need both rows and columns. They work great together on the same page.

👀 Live Preview

See Grid layouts in action:

repeat(3, 1fr) — 3 equal columns
1 2 3
grid-template-areas — page layout
header sidebar main footer

Examples Gallery

Practice CSS Grid with basic columns, page layouts, column spanning, and responsive auto-fit grids.

🔠 Core Patterns

Start with equal columns, then build named page regions.

Example 1 — Basic 3-Column Grid

Create three equal columns with repeat(3, 1fr) and gap.

grid-3col.css
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
}
Try It Yourself

How It Works

1fr divides available space equally among three columns. Items flow left to right, then wrap to the next row automatically.

Example 2 — Page Layout with Areas

Define named regions for header, sidebar, main, and footer.

grid-areas.css
.layout {
  display: grid;
  grid-template-columns: 120px 1fr;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  gap: 0.5rem;
}

.header { grid-area: header; }
Try It Yourself

How It Works

Each string in grid-template-areas is a row. Matching names in adjacent cells form a named region. Assign items with grid-area: header.

🎨 Spanning & Responsive

Span multiple columns and build grids that reflow on smaller screens.

Example 3 — Spanning Columns

Make items span multiple columns with grid-column: span 2 or 1 / -1.

grid-span.css
.wide {
  grid-column: span 2;
}

.full {
  grid-column: 1 / -1;
}
Try It Yourself

How It Works

span 2 stretches an item across two columns. 1 / -1 spans from the first line to the last line (full width).

Example 4 — Responsive auto-fit Grid

Columns reflow automatically with repeat(auto-fit, minmax(120px, 1fr)).

grid-responsive.css
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 0.75rem;
}
Try It Yourself

How It Works

auto-fit creates as many columns as fit. minmax(120px, 1fr) ensures each column is at least 120px but grows to fill space. Columns wrap when the container shrinks.

💬 Usage Tips

  • Start simple — Three equal columns teach the basics before complex areas.
  • Use fr for flexibility — Prefer 1fr over percentages for equal columns.
  • gap over marginsgap handles spacing between all cells cleanly.
  • DevTools overlay — Firefox and Chrome show grid lines for debugging.
  • Combine with Flexbox — Grid for page structure; Flexbox inside components.

⚠️ Common Pitfalls

  • Only direct children — Grid applies to immediate children only.
  • Overlapping items — Poorly placed spans can cause items to overlap.
  • Over-complexity — Start simple; add areas and spans as needed.
  • Using Grid for navbars — Flexbox is often simpler for one-row navigation.
  • Forgetting minmax minimum — Too-small min values can squash content on mobile.

♿ Accessibility

  • Source order — Visual grid placement should not break logical DOM order for screen readers.
  • Semantic HTML — Use <header>, <nav>, <main>, <footer> in grid layouts.
  • Reading order — Avoid rearranging content in ways that confuse assistive technology.
  • Responsive reflowauto-fit helps content remain readable without horizontal scroll.
  • Focus visibility — Grid items still need visible focus styles for keyboard users.

🧠 How CSS Grid Works

1

display: grid on parent

The container becomes a grid formatting context with rows and columns.

Container
2

Tracks are defined

grid-template-columns and grid-template-rows set track sizes.

Tracks
3

Items placed in cells

Children auto-place or use grid-area / grid-column for precise placement.

Place
=

Two-dimensional layout

Rows and columns align content in a structured grid.

🖥 Browser Compatibility

CSS Grid has universal support in all modern browsers including Chrome, Firefox, Safari, Edge, and mobile browsers.

Baseline · Universal support

CSS Grid Layout Module

Grid is production-ready in every current browser. No vendor prefixes needed.

100% Modern browsers
Google Chrome Full support
Full support
Mozilla Firefox Full support
Full support
Apple Safari Full support
Full support
Microsoft Edge Full support
Full support
Opera Full support
Full support
CSS Grid 100% supported

Bottom line: CSS Grid is safe for production. Legacy IE issues are irrelevant for modern development.

🎉 Conclusion

CSS Grid is essential for modern two-dimensional layouts. With display: grid, grid-template-columns, and gap, you can build page structures and card grids that adapt to any screen size.

Practice the four examples above, then explore grid-template-areas for full page layouts and auto-fit for responsive galleries. Pair Grid with Flexbox for the best of both worlds.

💡 Best Practices

✅ Do

  • Use fr units for flexible column sizing
  • Use gap for consistent cell spacing
  • Use grid-template-areas for page layouts
  • Use auto-fit minmax() for responsive card grids
  • Combine Grid (page) with Flexbox (components)

❌ Don’t

  • Use Grid for simple one-row navbars
  • Rearrange DOM order visually without a11y consideration
  • Over-engineer layouts before trying simple columns
  • Forget that only direct children are grid items
  • Rely on floats when Grid solves the problem cleanly

Key Takeaways

Knowledge Unlocked

Five things to remember about CSS Grid

Use these points when building two-dimensional layouts.

5
Core concepts
fr 02

fr unit

Flexible size.

Sizing
area 03

Areas

Named regions.

Layout
span 04

Span

grid-column.

Place
2D 05

Two axes

Rows + cols.

Scope

❓ Frequently Asked Questions

CSS Grid is a two-dimensional layout system. You define rows and columns on a grid container, then place child elements into cells. It is ideal for page layouts, card grids, and any design that needs both horizontal and vertical structure.
Set display: grid or display: inline-grid on the parent element. That parent becomes a grid container, and its direct children become grid items placed into the grid.
Grid handles two dimensions — rows and columns together. Flexbox handles one dimension — a row or a column. Use Grid for page layouts and card grids; use Flexbox for navbars and simple alignment flows.
fr means fraction of available space. In grid-template-columns: 1fr 2fr 1fr, the middle column gets twice as much free space as the side columns.
Yes. CSS Grid is supported in all modern browsers including Chrome, Firefox, Safari, Edge, and mobile browsers. It is safe for production use.

Practice in the Live Editor

Open the HTML editor, add display: grid to a container, and experiment with columns, areas, and gap.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful