Module 15: Page Layouts

Lesson 4 of 5 · 15–25 minutes

CSS Grid

🏠The everyday picture

A chessboard or a newspaper front page: rows and columns, where a big story can span three columns and a small one takes one box.

📘What it really is

.gallery {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 1rem;
}

fr means "a fraction of the free space", so three 1fr columns are equal.

grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));

🖱️Try it

Change your .cards section to use Grid with repeat(auto-fit, minmax(220px, 1fr)) instead of Flexbox. Resize the window: the columns change from three to two to one automatically. Inspect the section: developer tools show a grid badge that draws the grid lines when clicked.

✅Quick check

A photo gallery in rows and columns: Flexbox or Grid?

Show the answer

Grid, because it's a layout in two directions.

What does 1fr mean?

Show the answer

One share of the free space. Columns of 1fr 2fr split the space one-third and two-thirds.

← PreviousFlexbox for real layouts
ProLearners Assist