← Back to Course

Module -09

CSS Grid

The two-dimensional layout system for complex page structures. Rows AND columns, defined with precision or flexibility.

CSS Grid shipped in 2017 and transformed web layout. Browser DevTools now include Grid inspectors that visualize grid lines, making debugging intuitive.

Before Grid: Bootstrap 12-column grids, float-based layouts. After Grid: truly two-dimensional layouts without frameworks. Subgrid is now extending its power.

Grid thinks in rows AND columns simultaneously. Define the structure, place items. The grid doesn't care about source order — you control exactly where things go.

Part A

Core Concepts

01

Grid vs Flexbox: When to Use Each

Grid and Flexbox aren't competitors — they're complementary. Understanding when to use each is key to clean layouts.

Flexbox: One Axis
Items flow, wrap if needed. Sizes vary.
Grid: Two Axes
Defined structure. Aligned rows & columns.
One Dimension vs Two Dimensions

Flexbox is one-dimensional: items flow along a single axis, wrapping when needed. Grid is two-dimensional: you define rows AND columns, creating a structured layout where items align in both directions.

Navigation Items
Home
About
Contact Us
Blog
Button Groups
Save
Cancel
Card Content (vertical stack)
Use Flexbox For...

Use Flexbox when content should flow: nav menus, button groups, card interiors, centering content, space distribution. When items have varying sizes and should adapt, Flexbox is your tool.

Page Layout
Card Grid (equal sizing)
Dashboard Widgets
Use Grid For...

Use Grid when you need structure: page layouts with header/sidebar/main/footer, card grids with equal sizing, dashboards, image galleries. When alignment across rows AND columns matters, Grid is your tool.

GRID
Sidebar
GRID: Main Content
FLEX: Nav
GRID: Cards
GRID: Cards
Grid for page structure, Flexbox for components inside
They Work Together

Real layouts use both: Grid for the overall page structure (sidebar + main), Flexbox for component internals (nav items, card content). They're complementary tools, not alternatives.

02

Defining a Grid

Create grids with grid-template-columns and grid-template-rows. The grid container becomes a two-dimensional canvas.

grid-template-columns: 100px 200px 100px;
100px
200px
100px
grid-template-columns: 1fr 2fr 1fr;
1fr
2fr
1fr
grid-template-columns: repeat(4, 1fr);
grid-template-columns

grid-template-columns defines column sizes. Use fixed units (px), flexible fractions (fr), or repeat() for patterns. 1fr means "1 fraction of available space" — like Flexbox's flex: 1.

Fixed row heights
30px
60px
30px
Auto rows (content-sized)
Short
This has more content so it's taller
Medium
grid-template-rows

grid-template-rows defines row heights. Often rows are auto (content-sized) rather than fixed. You can mix: grid-template-rows: 60px auto 1fr 40px for header/content/spacer/footer.

gap: 8px
row-gap: 16px; column-gap: 8px
Spacing with gap

gap works the same as in Flexbox — spacing between grid cells. Use row-gap and column-gap for different spacing on each axis. No more margin hacks!

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
}
As many columns as fit, minimum 200px, grows to fill space
Responsive Grids with auto-fill

The magic formula for responsive grids: repeat(auto-fill, minmax(200px, 1fr)). Creates as many columns as will fit, each at least 200px, growing to fill remaining space. No media queries needed!

03

Placing Grid Items

Control exactly where items go with grid-column and grid-row. Make items span multiple cells.

1
2
3
4
Grid lines are numbered 1, 2, 3... (not 0-indexed). A 3-column grid has 4 column lines. Items are placed between lines.
Understanding Grid Lines

Grid lines are the invisible lines between columns and rows. They're numbered starting at 1. A 3-column grid has 4 column lines. You place items by referencing these line numbers.

grid-column: span 2
grid-row:
span 2
grid-column: span 2; /* span 2 columns */
grid-row: span 2; /* span 2 rows */
Making Items Span Multiple Cells

span makes items stretch across multiple cells. grid-column: span 2 spans 2 columns. grid-row: span 2 spans 2 rows. Great for featured cards or full-width sections.

1/3, 1/2
3/5, 1/3
1/2, 2/4
2/5, 3/4
grid-column: 1 / 3; /* start at line 1, end at line 3 */
grid-row: 1 / 2; /* start at line 1, end at line 2 */
Precise Placement with Line Numbers

For precise control, specify start and end lines: grid-column: 1 / 3 starts at line 1, ends at line 3. You can also use -1 for the last line: grid-column: 1 / -1 spans full width.

.layout {
  grid-template-columns: [sidebar-start] 200px [sidebar-end main-start] 1fr [main-end];
}

.sidebar { grid-column: sidebar-start / sidebar-end; }
.main { grid-column: main-start / main-end; }
Named lines make complex layouts more readable. Optional but useful for page-level grids.
Named Grid Lines (Advanced)

You can name grid lines for readability: [sidebar-start] 200px [sidebar-end]. Then reference them: grid-column: sidebar-start / sidebar-end. Helpful for complex page layouts.

04

Grid Areas: Visual Layout Definition

Define layouts with ASCII art using grid-template-areas. The most intuitive way to create page structures.

grid-template-areas:
  "header header header"
  "sidebar main main"
  "footer footer footer";
header
sidebar
main
footer
ASCII Art Layouts

grid-template-areas lets you draw your layout with strings. Each string is a row, each word is a cell. Repeat a name to span multiple cells. It's like ASCII art for layouts!

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

.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
Placing Items in Named Areas

Define the template on the container, then assign each item to its area with grid-area: name. The CSS reads like a blueprint — you can visualize the layout just by reading the code.

grid-template-areas:
  "logo . nav"
  "main main main";
logo
. (empty)
nav
main
Creating Empty Spaces

Use a period . for empty cells. The space will be left blank in the layout. You can also use multiple periods ... for readability when empty spans multiple columns.

Mobile (stacked)
"header"
"main"
"footer"
Desktop (sidebar)
"header header"
"sidebar main"
"footer footer"
Changing Layout with Media Queries

With grid-template-areas, changing layout per breakpoint is trivial — just redefine the template in a media query. The HTML stays the same, only the CSS grid definition changes.

05

Grid Alignment

Align items and content within their grid cells and the grid container.

justify-items: start
justify-items: center
justify-items: end
justify-items = horizontal (row axis)
align-items = vertical (column axis)
Aligning Items in Cells

justify-items aligns items horizontally within their cells. align-items aligns vertically. These work on all grid items at once. Default is stretch (fill the cell).

start
center
end
.item { justify-self: end; align-self: center; }
/* Or shorthand: place-self: center end; */
Aligning Individual Items

justify-self and align-self override alignment for individual items. place-self is shorthand for both. Useful when one item needs different alignment than the rest.

justify-content: center
justify-content: space-between
When the grid is smaller than its container, justify-content and align-content position the entire grid.
Aligning the Entire Grid

justify-content and align-content position the entire grid when it doesn't fill its container. Works like Flexbox — space-between, center, etc. Rarely needed but useful for centering a fixed-size grid.

Part H

Self-Assessment

15

Self-Assessment

Test your understanding of CSS Grid. Click each question to reveal the answer.

Q1

When should you use CSS Grid instead of Flexbox?

Click to reveal
Q2

What does the 'fr' unit represent in CSS Grid?

Click to reveal
Q3

What does repeat(auto-fill, minmax(200px, 1fr)) create?

Click to reveal
Q4

How do you make a grid item span 2 columns?

Click to reveal
Q5

What character creates an empty cell in grid-template-areas?

Click to reveal
Q6

What's the difference between justify-items and justify-content in Grid?

Click to reveal

Key Takeaways

Grid for structure, Flex for flow
Grid
2D layouts
Flexbox
1D flow

Use Grid when you need alignment in BOTH directions (page layouts, card grids). Use Flexbox when content should FLOW (nav items, buttons, centering).

Define structure with grid-template-columns
grid-template-columns: 200px 1fr 1fr;

grid-template-columns defines your column structure. Mix fixed (px), flexible (fr), and auto values. fr = fraction of remaining space.

Responsive grids without media queries
repeat(auto-fill, minmax(200px, 1fr))

The magic formula: as many columns as fit, minimum 200px each, growing to fill space. Fully responsive, no media queries.

span for larger items

grid-column: span 2 makes an item span 2 columns. grid-row: span 2 for rows. Simple way to create featured or full-width items.

ASCII art layouts with grid-template-areas
"header header"
"sidebar main"

Define layouts visually with strings. Each string is a row, each word is a cell. The most intuitive way to create page layouts.

Use Grid inspector in DevTools
Elements → grid badge → Enable overlay

Chrome/Firefox DevTools visualize grid lines with numbers. Essential for debugging. Click the 'grid' badge next to any grid container.