Tutorials CSS Grid Introduction
Ch 29 / 60
Chapter 29 Beginner 8 min read CSS

CSS Grid Introduction

CSS Grid is the first true two-dimensional layout system in CSS. While Flexbox handles a single row or a single column at a time, Grid handles both simultaneously — you define rows and columns and then place items precisely anywhere in that matrix. It transformed how we build page layouts.

What you'll learn
What CSS Grid is and what makes it different
Grid terminology: container, items, lines, tracks, cells, areas
Enabling Grid with display: grid
Defining columns and rows: grid-template-columns/rows
The fr unit — fractions of available space
Grid vs Flexbox — when to use which

What Is CSS Grid?

CSS Grid is a two-dimensional layout system. You activate it by setting display: grid on a container. Then you define a grid of rows and columns, and items are automatically placed into — or explicitly assigned to — cells in that grid.

Two-dimensional
Flexbox works along one axis at a time. Grid controls both rows and columns simultaneously — making complex page layouts natural to express.
Layout-first thinking
With Grid you define the layout first (the tracks), then place content into it. With Flexbox the content drives the layout. Grid is declarative; Flexbox is responsive.
One property enables it
display: grid on the parent. All direct children become grid items automatically. Grandchildren are unaffected.
Without display:grid
Item 1
Item 2
Item 3

Block children stack vertically

display: grid; grid-template-columns: 1fr 1fr 1fr
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6

3-column grid, items auto-placed

CSS — Enabling Grid
/* One property activates Grid */
.container {
  display: grid;
}
/* All direct children are now grid items */
/* Without any column/row definitions, items auto-flow into a single column */

/* inline-grid: grid container that flows inline */
.inline-container {
  display: inline-grid;
}

/* Define columns immediately */
.three-col {
  display:                grid;
  grid-template-columns: 1fr 1fr 1fr; /* 3 equal columns */
  gap:                    16px;
}

Grid Terminology

CSS Grid introduces specific vocabulary. Learning these terms makes the documentation and further chapters much easier to follow:

3×3 grid — numbered parts labelled below
Cell (1,1)
Area (spans 2 cols)
Cell (1,4)
Spans 2 rows
cell
cell
Item
cell
Track (row)
Grid Container
The element with display: grid. All grid properties are set here. Direct children become grid items.
Grid Item
A direct child of the grid container. Automatically becomes a grid item. Can span multiple rows/columns using placement properties.
Grid Line
The dividing lines that form the grid structure. Numbered starting at 1 from the left/top edge. A 3-column grid has 4 column lines (1, 2, 3, 4).
Grid Track
The space between two adjacent grid lines — essentially a column or a row. Sized by grid-template-columns and grid-template-rows.
Grid Cell
The single intersection of a row track and a column track. The smallest unit of the grid. Like a table cell.
Grid Area
A rectangular space that spans one or more cells. An item can be assigned to an area using line numbers or named areas.
Grid lines — 3 columns = 4 column lines, 2 rows = 3 row lines
1 2 3 4
1 2 3
col 1
col 2
col 3
col 1
col 2
col 3

Column lines (blue) run vertically. Row lines (orange) run horizontally. Item spanning cols 1-3 = grid-column: 1 / 4.

grid-template-columns & grid-template-rows

These two properties define the size of each track in the grid. Values are space-separated — one per column (or row). You can mix units: px, %, fr, auto, min-content, max-content.

1fr 1fr 1fr — 3 equal columns
1
2
3
4
5
6
200px 1fr 1fr — fixed + fluid
fixed
2
3
fixed
5
6
auto auto auto — content-sized
Short
Longer text
Md
+ rows: 60px 100px 60px
60px row
60px row
100px row
100px row
60px row
60px row
CSS — grid-template-columns & rows
.container {
  display: grid;

  /* 3 equal columns */
  grid-template-columns: 1fr 1fr 1fr;

  /* shorthand using repeat() */
  grid-template-columns: repeat(3, 1fr);

  /* fixed sidebar + fluid main */
  grid-template-columns: 260px 1fr;

  /* 3 columns: fixed, fluid, fixed */
  grid-template-columns: 200px 1fr 200px;

  /* mixed units */
  grid-template-columns: auto 1fr 2fr;

  /* Explicit row heights */
  grid-template-rows: 64px 1fr 48px;  /* header, content, footer */
  grid-template-rows: repeat(3, 100px);   /* 3 equal rows */
}

The fr Unit — Fractions of Free Space

The fr unit is unique to CSS Grid. It represents a fraction of the available free space in the grid container — after any fixed-size tracks and gaps are subtracted. It works exactly like flex-grow in Flexbox.

1fr 1fr 1fr — equal thirds (100% ÷ 3)
1fr (33%)
1fr (33%)
1fr (33%)
1fr 2fr 1fr — middle gets twice as much space
1fr (25%)
2fr (50%)
1fr (25%)
200px 1fr — fixed sidebar, fr gets remainder
200px fixed
1fr (all remaining space)
1fr 1fr 1fr 1fr — four equal columns
1fr
1fr
1fr
1fr
CSS — fr unit
/* fr = fraction of available free space */

/* 3 equal columns */
.grid { grid-template-columns: 1fr 1fr 1fr; }
/* same as above */
.grid { grid-template-columns: repeat(3, 1fr); }

/* middle column is 2× wider than sides */
.grid { grid-template-columns: 1fr 2fr 1fr; }
/* 25% : 50% : 25% — total 4 fr units, each 1fr = 25% */

/* Fixed sidebar + fluid main content */
.layout { grid-template-columns: 260px 1fr; }
/* 1fr gets everything left after 260px + gap */

/* fr vs % — why fr is better:
   - fr is calculated AFTER fixed widths and gaps
   - % includes gap space → items may overflow
   Use fr whenever possible for fluid columns */
fr vs percentage — why fr wins
With % in a 3-column grid: 33.33% 33.33% 33.33% — if you add a gap: 16px the total exceeds 100% and items overflow. With fr: 1fr 1fr 1fr — the gap is subtracted first and the remaining space is divided into three equal fractions. Always prefer fr for fluid columns.

The repeat() Function

repeat() avoids writing repetitive track sizes. It takes a count and a track size (or a list of track sizes) and repeats them:

repeat(3, 1fr) — 3 equal columns
1
2
3
repeat(4, 1fr) — 4 equal columns
1
2
3
4
repeat(2, 1fr 2fr) — 4 cols: 1fr 2fr 1fr 2fr
1fr
2fr
1fr
2fr
repeat(3, 80px) — three fixed columns
80px
80px
80px
CSS — repeat()
/* repeat(count, track-size) */
.grid { grid-template-columns: repeat(3, 1fr); }
/* = 1fr 1fr 1fr */

.grid { grid-template-columns: repeat(4, 200px); }
/* = 200px 200px 200px 200px */

.grid { grid-template-columns: repeat(2, 1fr 2fr); }
/* = 1fr 2fr 1fr 2fr */

/* repeat with mixed prefix/suffix */
.layout { grid-template-columns: 260px repeat(3, 1fr) 260px; }
/* sidebars + 3 fluid middle cols */

/* auto-fill and auto-fit (covered in Chapter 30) */
.responsive { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }

Grid vs Flexbox — When to Use Which

Both are powerful layout tools and they are complementary, not competing. The key difference: Flexbox is one-dimensional; Grid is two-dimensional.

🔵 CSS Grid
Header (full width)
Sidebar
Main
Footer (full width)
Use Grid when:
— Building the overall page layout
— You need items in both rows and columns
— Items should align to a shared grid
— You want to span items across multiple rows/cols
— Card grids, magazine layouts, dashboards
🟠 Flexbox
Logo
Home
About
CTA
Use Flexbox when:
— Arranging items in a single row or column
— Navigation bars, button groups, toolbars
— Icon + label combinations
— When layout should adapt to content size
— Centring a single item in a container
Feature Flexbox CSS Grid
Dimensions One (row or column) Two (rows and columns)
Layout direction Content drives layout Layout drives content
Column alignment Not guaranteed across rows Perfect alignment across rows
Items spanning multiple rows/cols Not possible Yes — grid-column/row span
Best for Nav bars, components, alignment Page layouts, card grids, dashboards
Gap support Yes Yes
Nesting Yes — nest flex in grid items Yes — nest grid in grid items
Use both together
The most common pattern in production apps: Grid for the page/section layout, Flexbox for components inside grid cells. For example: a dashboard uses Grid to define the page structure (header, sidebar, main, footer), while each card inside uses Flexbox to arrange its icon, title and body text.
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
Complete reference covering all CSS Grid and Flexbox properties with visual examples.
Download PDF

Try It Yourself

Experiment with grid-template-columns, different fr values, repeat(), and a real page layout. Change the number of columns and watch the auto-placement:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 29

Questions & Answers
The fundamental difference is dimensionality:

Flexbox is one-dimensional — it lays items out along a single axis (either a row or a column) at a time. While you can use flex-wrap to create multiple rows, items in different rows don't align to a shared column structure.

CSS Grid is two-dimensional — it simultaneously controls both rows and columns. Items in different rows do align to the same column tracks. This makes it possible to create complex layouts where items span multiple rows or columns, and where columns stay aligned across all rows.

A practical way to think about it: Flexbox is for components (a nav bar, a button group, a card's internal layout). Grid is for page layouts and complex sections (a dashboard, a magazine layout, a card grid where items need to span columns).

They are complementary and work great together — use Grid for the outer layout, Flexbox for items inside grid cells.
The fr (fraction) unit represents a fraction of the available free space in the grid container. It works similarly to flex-grow in Flexbox — the total free space is divided into units, and each fr column gets its proportional share.

Why fr is better than %:
With percentages: 33.33% 33.33% 33.33% — if you add gap: 16px, the columns total more than 100% because the gaps are added on top, causing overflow or unexpected sizing.

With fr: 1fr 1fr 1fr — the browser first subtracts all fixed sizes (px columns) and gap space, then divides the remaining space into fractions. Result: columns always fit perfectly within the container regardless of gap size.

Rule of thumb: always use fr for fluid grid columns. Only use % in grid when you need a column to be a specific percentage of the container and there are no gaps.
These are the core building blocks of the CSS Grid model:

Grid Lines — the dividing lines that form the grid structure. Numbered starting at 1 from the left (columns) or top (rows) edge. A grid with 3 columns has 4 column lines: lines 1, 2, 3, and 4. You reference these numbers when placing items explicitly.

Grid Tracks — the space between two adjacent grid lines. Essentially a column or a row. Sized by grid-template-columns and grid-template-rows.

Grid Cell — the smallest unit of the grid. The intersection of one row track and one column track. Like a table cell.

Grid Area — a rectangular space made up of one or more cells. An item placed from column line 1 to 3 and row line 1 to 2 occupies a 2-column, 1-row area. Areas can also be named with grid-template-areas.
repeat(count, track-size) is a CSS function that generates repeated track definitions. repeat(3, 1fr) is exactly equivalent to writing 1fr 1fr 1fr.

When to use it:
— When you have multiple identical tracks: repeat(4, 200px) = 200px 200px 200px 200px
— When you have a repeating pattern: repeat(3, 1fr 2fr) = 1fr 2fr 1fr 2fr 1fr 2fr
— Combined with auto-fill or auto-fit for responsive grids without media queries (covered in Chapter 30)

You can also combine repeat with explicit tracks: 260px repeat(3, 1fr) 260px creates two fixed sidebars with three fluid middle columns.
When you define columns but no explicit rows, CSS Grid uses implicit row creation. The browser automatically creates new rows as needed to fit all the items.

By default, these implicit rows are sized to auto — they stretch to fit their content. You can control the size of implicit rows with grid-auto-rows:
grid-auto-rows: 200px — all implicit rows are 200px tall
grid-auto-rows: minmax(100px, auto) — at least 100px but can grow

This is the key power of CSS Grid auto-placement: define your columns once and items automatically flow into rows. Add more items and the grid grows by creating new rows. No JavaScript needed.

The auto-placement algorithm fills items left-to-right, top-to-bottom by default (controlled by grid-auto-flow).
Multiple Choice Questions
Question 1 of 5
What property enables CSS Grid on a container?
Question 2 of 5
What does grid-template-columns: 1fr 2fr 1fr create?
Question 3 of 5
A 3-column grid has how many column lines?
Question 4 of 5
What is repeat(4, 1fr) equivalent to?
Question 5 of 5
Why is 1fr 1fr 1fr better than 33.33% 33.33% 33.33% in a grid with gaps?
Chapter Summary
  • CSS Grid is a two-dimensional layout system. display: grid on the parent turns all direct children into grid items.
  • Grid terminology: Container (parent with display:grid), Item (direct child), Line (numbered dividers), Track (space between lines = a column or row), Cell (single row+column intersection), Area (rectangular multi-cell region).
  • Grid lines are numbered from 1. A grid with N columns has N+1 column lines. Items are placed by referencing these line numbers.
  • grid-template-columns — defines column track sizes. grid-template-rows — defines row track sizes. Without explicit rows, the browser creates implicit rows automatically.
  • fr unit — fraction of available free space after fixed sizes and gaps are subtracted. Always prefer fr over % for fluid columns in grids with gaps.
  • repeat(count, size) — shorthand for repeated tracks. repeat(3, 1fr) = 1fr 1fr 1fr. Can repeat patterns: repeat(2, 1fr 2fr).
  • Grid vs Flexbox: Flexbox = 1D (row or column), for components. Grid = 2D (rows and columns), for page layouts. Use both together — Grid for structure, Flexbox inside grid cells.
  • Implicit rows: when columns are defined but rows are not, the browser auto-creates rows sized by grid-auto-rows (default: auto).
~8 min read
Beginner level
Chapter 29 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel