Lesson 6 of 7 · 5 min

CSS Grid

Two-dimensional layout with rows and columns. Fixed and flexible tracks, the fr unit, auto-fill card grids, named areas, and when to choose Grid over Flexbox.

On this page
  1. Define the tracks
  2. The fr unit
  3. A responsive card grid with no media queries
  4. Placing items
  5. Named areas
  6. Alignment
  7. Grid or Flexbox?
  8. Try it

Grid lays out items in rows and columns at the same time. Use it for page layouts, card grids, and anything that should line up in two directions.

Define the tracks

.layout {
  display: grid;
  grid-template-columns: 16rem 1fr;
  gap: 2rem;
}

This makes two columns: a fixed 16rem sidebar and a main column that takes the rest. Children fill the cells in order.

The fr unit

fr means "a fraction of the free space":

grid-template-columns: 1fr 1fr 1fr;   /* three equal columns */
grid-template-columns: 2fr 1fr;       /* two thirds and one third */
grid-template-columns: repeat(4, 1fr);/* four equal columns */

A responsive card grid with no media queries

This one line is worth memorising:

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 17rem), 1fr));
  gap: 2rem;
}

It reads: "make as many columns as fit, each at least 17rem wide, and share the leftover space". On a phone you get one column, on a laptop three, on a wide screen four. The min(100%, 17rem) stops a card from overflowing on very small screens.

Placing items

Items can span several tracks or sit on specific lines:

.feature {
  grid-column: 1 / -1;  /* from the first line to the last: full width */
}
.wide {
  grid-column: span 2;  /* two columns wide */
}

Named areas

For page layouts, naming areas makes the CSS read like a picture of the page:

.page {
  display: grid;
  grid-template-columns: 16rem 1fr;
  grid-template-areas:
    "header header"
    "nav    main"
    "footer footer";
  gap: 1.5rem;
}
.page > header { grid-area: header; }
.page > nav    { grid-area: nav; }
.page > main   { grid-area: main; }
.page > footer { grid-area: footer; }

To change the layout on a phone, redefine the areas and nothing else:

@media (max-width: 700px) {
  .page {
    grid-template-columns: 1fr;
    grid-template-areas: "header" "main" "nav" "footer";
  }
}

Alignment

Grid uses the same alignment words as Flexbox:

.grid {
  align-items: start;      /* each item to the top of its row */
  justify-items: center;   /* each item to the middle of its column */
}
.centered {
  display: grid;
  place-items: center;     /* both at once: the shortest way to center */
}

Grid or Flexbox?

  • Flexbox: content decides the size. One row or one column. Navigation, button groups, tags.
  • Grid: the layout decides the size. Rows and columns that line up. Page structure, card grids, forms with labels in one column.

They work well together: a Grid page layout whose header is a Flexbox row.

Try it

Make a .cards container with eight cards using the auto-fill line above. Resize the window and watch the column count change. Then make the first card span the full width with grid-column: 1 / -1.

More from CSS Crash Course