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.
display: gridgrid-template-columns/rowsfr unit — fractions of
available spaceWhat 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.
display: grid on the parent. All direct
children become grid items automatically. Grandchildren are unaffected.Block children stack vertically
3-column grid, items auto-placed
/* 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:
display: grid. All grid
properties are set here. Direct children become grid items.grid-template-columns and grid-template-rows.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.
.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.
/* 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 */
% 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(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.
— 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
— 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 |
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:
Quick Check — Chapter 29
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.
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.
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.
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 tallgrid-auto-rows: minmax(100px, auto) — at least 100px but can
growThis 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).
grid-template-columns: 1fr 2fr 1fr
create?repeat(4, 1fr) equivalent to?1fr 1fr 1fr better than 33.33% 33.33% 33.33% in a grid with gaps?- CSS Grid is a two-dimensional layout system.
display: gridon 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.frunit — fraction of available free space after fixed sizes and gaps are subtracted. Always preferfrover%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).
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.