Chapter 26: CSS Grid

CSS Grid, which is (together with Flexbox) one of the two most powerful modern layout systems in CSS — and in 2026 it is used in almost every serious project for anything that needs two-dimensional control (rows and columns at the same time).

Flexbox is great when you want to arrange things in one direction (row or column). Grid is perfect when you want to control both directions simultaneously — think:

  • full-page layouts (header, sidebar, main content, footer)
  • photo galleries
  • dashboards
  • pricing tables
  • complex card grids
  • forms with aligned labels & inputs
  • anything that feels like a table but should not be a <table>

Let’s learn it properly — slow, clear, with lots of explanations and a real, playable example you can copy-paste right now.

1. The Core Idea of CSS Grid

Two main players:

  • Grid Container (the parent) → gets display: grid
  • Grid Items (direct children) → automatically placed into the grid cells

You define:

  • how many rows and columns you want
  • how wide/tall each track (row or column) should be
  • where each child should go (or let auto-placement do the work)

2. Most Important Properties (All on the Parent = Grid Container)

Property Most common values / examples What it actually does (very clearly)
display grid or inline-grid Turns the element into a grid container
grid-template-columns 1fr 1fr 1fr, repeat(3, 1fr), 200px auto 300px Defines column tracks (how many & how wide)
grid-template-rows auto, 100px 200px, repeat(4, minmax(150px, auto)) Defines row tracks (how many & how tall)
grid-template Shorthand: grid-template-rows / grid-template-columns Write rows and columns in one line
gap / row-gap / column-gap 20px, 1.5rem, 1em 2em Space between grid cells (replaces margin hacks)
justify-items start, center, end, stretch (default) Horizontal alignment inside each cell
align-items start, center, end, stretch (default) Vertical alignment inside each cell
place-items Shorthand for align-items justify-items One line for both
justify-content start, center, space-between, space-around, space-evenly Distributes extra horizontal space when total columns < container width
align-content same as above Distributes extra vertical space when total rows < container height

3. Placing Items (Two Main Ways)

Way 1: Auto-placement (easiest — let browser do the work)

Just put items in HTML order — grid fills row-by-row (default) or column-by-column.

CSS

Way 2: Explicit placement with line numbers

You name the grid lines or use numbers:

CSS

Or with named areas (very clean for page layouts):

CSS

4. Real, Practical CSS Grid Example (Full Page Layout + Card Grid)

index.html

HTML

style.css (focused on Grid)

CSS

What You Should See & Play With

  1. Full page layout — header & footer full width, sidebar fixed 250px, main takes remaining space
  2. Responsive cards — repeat(auto-fit, minmax(280px, 1fr)) → automatically creates as many columns as fit, wraps beautifully on mobile
  3. Resize browser → sidebar stays, main content shrinks/grows, cards reflow
  4. Try changing grid-template-columns: 300px 1fr → sidebar gets wider
  5. Remove grid-template-areas and use line numbers instead — see how it still works

Quick Grid Mastery Checklist (2026)

  • Always start with display: grid on parent
  • Use fr unit for flexible tracks (1fr 2fr = second column twice as wide)
  • Use auto-fit + minmax() for responsive card grids
  • Use grid-template-areas for named, readable page layouts
  • gap > manual margins (cleaner & prevents double spacing)
  • Combine with Flexbox — Grid for overall structure, Flexbox for inside components

How does the page behave when you make the browser narrow? Sidebar still readable? Cards stacking nicely?

Next steps — tell me what you want:

  • “Grid deep dive – 10 real patterns (gallery, dashboard, form layout…)”
  • “Combine Flexbox + Grid in one project”
  • “Responsive grid with media queries”
  • “grid-auto-flow, dense packing, named lines”
  • “common Grid mistakes & how to fix them”

You’ve just learned the second pillar of modern layout (Flexbox was the first). Together they can build almost anything. Chai khatam? Fresh cup le aao — let’s keep going! 🚀 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *