Skip to main content

CSS Grid Tracks (Rows and Columns)

A grid container decides how many columns and rows it has, and how large they are.

Three properties control this:

  • grid-template-columns — how many columns the grid has, and how wide.
  • grid-template-rows — how many rows the grid has, and how high.
  • grid-template-areas — lays out columns and rows through named grid items.

The CSS grid-template-columns Property

Column count and width come from grid-template-columns.

You give it a space-separated list with one value per column, and that value is the column's width.

Typical choices include:

  • Fixed lengths, as in 100px 300px 200px
  • Percentages, as in 20% 60% 20%
  • The fr unit, as in 1fr 2fr 1fr
  • auto, as in auto auto auto
  • repeat(), as in repeat(3, 1fr)
  • minmax(), as in minmax(80px, 1fr) 150px 150px

CSS grid-template-columns: auto

Writing the auto keyword three times gives a grid layout of 3 columns. With content of similar size, as here, the columns end up at equal width.

Example

Three columns of equal width, holding six numbered items.

Prompt
Create a CSS grid container with 3 equal-width columns using grid-template-columns: auto auto auto. Place 6 numbered items inside the grid.
Output: complete HTML page

Grid Columns with Mixed Width

Columns do not have to match in size. You might, for instance, fix the width of 2 columns and leave 1 at auto size.

Example

Here the widths are 80px for column 1, 200px for column 2, and auto size for column 3.

Prompt
Create a CSS grid container with 3 columns where the first column is 80px wide, the second is 200px wide, and the third uses auto. Place 6 numbered items inside.
Output: complete HTML page

Grid Columns with the fr Unit

Short for "fraction", the fr unit automatically splits the available space into equal shares called fractions.

So 1fr claims 1 fraction of that space and 2fr claims 2.

Equal columns

The container width is split equally, giving each column 33.3%.

Prompt
Create a CSS grid container with 3 equal columns using the fr unit (1fr 1fr 1fr). Place 6 numbered items inside the grid.
Output: complete HTML page

Unequal columns

Compared with the others, the second column has double the width.

Prompt
Create a CSS grid container with 3 columns using the fr unit where the second column is twice as wide as the others (1fr 2fr 1fr). Place 6 numbered items inside.
Output: complete HTML page

Grid Columns with the repeat() Function

A grid can reuse a group of columns or rows many times over through the repeat() function.

Example

Three equal-width columns written with repeat().

Prompt
Create a CSS grid container with 3 equal-width columns using the repeat() function (repeat(3, 1fr)). Place 6 numbered items inside.
Output: complete HTML page

Grid Columns with the minmax() Function

A size range with a minimum and a maximum value comes from the minmax() function: the size is never below the first or above the second.

Example

Never narrower than 80px, the first column can grow as far as 1fr.

Prompt
Create a CSS grid container with 3 columns using minmax(80px, 1fr) 150px 150px for grid-template-columns. Place 6 numbered items inside.
Output: complete HTML page

The CSS grid-template-rows Property

Row count and height come from grid-template-rows.

Again you supply a space-separated list, and each entry gives one row its height.

Typical choices include fixed lengths, percentages, the fr unit, auto, min-content, max-content, repeat(), minmax(), and fit-content().

Example

The first row gets a height of 80px and the second 200px. Rows beyond those two default to auto.

Prompt
Create a CSS grid container with 3 columns (auto) and use grid-template-rows: 80px 200px to set the first row to 80px and the second to 200px. Place 8 numbered items inside.
Output: complete HTML page