Lesson 12 of 14 · 10 min

CSS Grid

Flexbox arranges items in one direction. Grid arranges them in rows and columns at once, which is ideal for galleries, dashboards and whole page layouts.

.board {
  display: grid;
  grid-template-columns: 200px 1fr 1fr;
  gap: 16px;
}
  • grid-template-columns lists the width of each column.
  • fr means "a fraction of the free space": 1fr 1fr is two equal columns, 2fr 1fr gives the first twice the space.
  • repeat(4, 1fr) is short for 1fr 1fr 1fr 1fr.
  • gap sets the space between rows and columns.

Children fill the cells in order, wrapping onto new rows automatically.

Spanning cells

A child can take up several columns or rows:

.banner {
  grid-column: span 3;   /* three columns wide */
}

.sidebar {
  grid-row: span 2;      /* two rows tall */
}

Responsive without media queries

This handy line makes as many columns as fit, each at least 150px wide:

grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));

Your task

Make .gallery a grid with three equal columns and a 10px gap, and make the .featured item span two columns.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Gallery</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="gallery">
    <div class="tile featured">Victoria Falls</div>
    <div class="tile">Great Zimbabwe</div>
    <div class="tile">Le Morne</div>
    <div class="tile">Nyanga</div>
    <div class="tile">Chamarel</div>
  </div>
</body>
</html>

Your page

Discussion

Stuck? Ask here. Be kind, and share hints rather than full answers.

Loading the discussion…