🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·
🎨 CSS Styling

CSS Grid

CSS Grid is perfect for two-dimensional layouts (rows AND columns at once). Define grid columns with grid-template-columns. Use fr units to share available space. gap controls gutters between cells. Grid is ideal for card galleries, page layouts, and dashboards.

3 min 10 XP Lesson 7 of 10
CSS Grid
🌐

Appy Says…

CSS Grid is the most powerful layout system the web has ever had. Photo galleries, magazine layouts, dashboards, responsive grids — things that used to require JavaScript and complex hacks now take 10 lines of CSS.

📖

What is CSS Grid?

CSS Grid arranges elements in two dimensions: rows AND columns. It's the go-to for full-page layouts, image galleries, and any complex 2D arrangement.

  • display: grid — activate grid
  • grid-template-columns: 1fr 1fr 1fr — three equal columns
  • grid-template-columns: repeat(3, 1fr) — same thing
  • grid-template-rows — control row sizes
  • gap: 16px — spacing between cells
  • grid-column: 1 / 3 — span from column line 1 to 3
  • grid-area: header + grid-template-areas — named layout
  • Auto-fit: repeat(auto-fit, minmax(250px, 1fr)) — responsive without media queries
🎮

Think of it like a Minecraft map grid

A Minecraft chunk map is a perfect grid — rows and columns of equal cells. CSS Grid works the same way, but you can make cells span multiple columns (like a biome taking up 3 chunks wide) using grid-column.

⚙️

How It Works

  • 1. Parent: display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px;
  • 2. Children automatically fill the grid cells in order
  • 3. fr unit = fraction of available space
  • 4. Override a child's position: grid-column: 1 / -1; (span full width)
  • 5. auto-fit + minmax: responsive columns with no media queries
  • 6. Grid areas: name zones visually with grid-template-areas: 'header header' 'sidebar main'
🌍

Real-World Examples

  • Responsive photo gallery: grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  • Dashboard: header full-width, sidebar + main two-column below
  • Product cards: repeat(4, 1fr) on desktop, repeat(2, 1fr) on mobile
  • Pinterest-style masonry (with grid-auto-rows + JS span assignment)
💡

Key Facts

  • CSS Grid is supported in 97%+ of browsers
  • Grid and Flexbox complement each other — use Grid for page layout, Flexbox for component internals
  • auto-fill vs auto-fit: auto-fill keeps empty tracks, auto-fit collapses them
  • Firefox has the best DevGrid visualiser in its DevTools
⚠️

Watch Out!

Don't use Grid for everything — Flexbox is simpler for single-row/column components like navbars or button groups. Use Grid when you need both row AND column control simultaneously.

📌

Remember

repeat(auto-fit, minmax(250px, 1fr)) creates a fully responsive grid with zero media queries. This single line works for most card/gallery layouts.

What You Learned

  • CSS Grid: display:grid + grid-template-columns creates 2D layouts
  • fr units, gap, auto-fit/minmax for responsive layouts without media queries
  • Unlocks: page layouts, photo galleries, dashboards, responsive card grids

Key Facts

  • CSS Grid is supported in 97%+ of browsers
  • Grid and Flexbox complement each other — use Grid for page layout, Flexbox for component internals
  • auto-fill vs auto-fit: auto-fill keeps empty tracks, auto-fit collapses them
  • Firefox has the best DevGrid visualiser in its DevTools

Real-World Examples

• Responsive photo gallery: <code>grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));</code> • Dashboard: header full-width, sidebar + main two-column below • Product cards: <code>repeat(4, 1fr)</code> on desktop, <code>repeat(2, 1fr)</code> on mobile • Pinterest-style masonry (with <code>grid-auto-rows</code> + JS span assignment)

Remember

repeat(auto-fit, minmax(250px, 1fr)) creates a fully responsive grid with zero media queries. This single line works for most card/gallery layouts.

Quick Quiz

1 / 2

What does grid-template-columns: 1fr 1fr do?