CSS Grid Layout divides a box into rows and columns. Set display: grid on the parent. That parent is the grid container. Each direct child is a grid item.
grid-template-columns sizes the column tracks. 1fr is one share of the free space, so repeat(3, 1fr) makes three equal columns. gap is the gutter between tracks. grid-column: 1 / 3 places an item from column line 1 to column line 3, which covers two columns.
Lines are numbered from 1.
Three columns have four lines: 1, 2, 3, and 4. grid-column: 1 / 3 stops at line 3, so the item fills columns 1 and 2.
Syntax
CSS Syntax
A three-column grid, with one item placed by line numbers:
CSS
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
grid-auto-rows: minmax(100px, auto);
}
.one {
grid-column: 1 / 3; /* line 1 to line 3 */
grid-row: 1;
}
Reference
Core Ideas
Ideas every CSS Grid tutorial builds on:
Idea
Example
Remember
Container
display: grid
Direct children become items
Columns
repeat(3, 1fr)
Three equal column tracks
Fraction
1fr 2fr
The second track gets twice the free space
Gutter
gap: 10px
Space between tracks
Lines
grid-column: 1 / 3
Two columns, lines 1 through 3
New rows
minmax(100px, auto)
At least 100px, taller if the content needs it
Code
CSS Examples
Three grids. View Output shows the tracks the browser paints.
Example 1 — Three equal columns
repeat(3, 1fr) gives each column the same share of the free space. gap separates them.
1fr + 2fr is three shares of the space left after the gap.
The first column gets one share.
The second column gets two shares, so it is twice as wide.
Frequently Asked Questions
CSS Grid lays out a parent’s direct children in rows and columns. Set display: grid on the parent. That parent is the grid container. Each direct child is a grid item. You then size the tracks and, when you need to, place an item between grid lines.
grid-template-columns: repeat(3, 1fr) or 1fr 1fr 1fr. The fr unit is a share of the free space. Three 1fr tracks are equal. gap: 10px adds the space between tracks.
Column lines are numbered from 1. A three-column grid has lines 1, 2, 3, and 4. grid-column: 1 / 3 starts at line 1 and stops at line 3, so the item covers the first two columns.
grid-auto-rows: minmax(100px, auto) sizes rows that you did not name. Each of those rows is at least 100px tall and grows with the content.
No. Only the direct children of the grid container are grid items. A grandchild is laid out by its own parent.