Layout · Overview

Grid layout

The properties used with display: grid, grouped by role. Some go on the box and draw the column and row lines, take gaps, or align; others go on the items inside and decide where each one sits.

Try them together

The grid blocks are already stacked. Move the values, add or remove blocks, and see how they work together. Properties written for an item apply to the outlined second item.

Preview

1234
Sample
Items
Drag the bottom-right corner to resize

Code

.box {
  display: grid;
  flex-wrap: wrap;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px 8px;
}

.box > :nth-child(2) {
  grid-column: span 2;
}

Blocks3

  1. grid-template-columnsrepeat(3, 1fr)
    grid-template-columns
    repeat() count3
    Tracks (column or row sizes)
    Track 1
    minmax()
1234

Properties

Make columns and rowsOn the box

Draw the column and row lines and size the cells. These also size the rows (or columns) added when there are more items than you wrote, and set which way items fill in.

Add gapsOn the box

Space between columns and between rows. Nothing is added at the edges.

AlignOn the box

-items places each item inside its cell; -content places the whole set of tracks inside the box. justify works along the row (horizontal), align across it (vertical), and place sets both at once.

Place itemsOn an item

Name the lines an item starts and ends on to put it in any cell or span several cells. grid-area writes all four row and column lines at once. order changes the sequence items are placed in automatically.

Align one itemOn an item

Override the box's -items alignment for a single item.