Lesson 7 / 1258%

Lesson 7

CSS Grid

30 min

Build two-dimensional grid layouts.

Definition

CSS Grid (“Grid Layout”) is a layout module that divides a container intorows and columns, forming a grid in which you then place the items. Unlike Flexbox, which works along a single axis, Grid controls both dimensions at once. An element becomes a grid container withdisplay: grid;.

Flexbox vs Grid

Where Flexbox aligns items along a single axis, CSS Grid lets you buildtwo-dimensional layouts: rows AND columns at the same time. It’s the ideal tool for full-page layouts (header/sidebar/content/footer) or card grids.

Creating a grid

grid-base.css
.grid {
  display: grid;
  grid-template-columns: 200px 200px 200px; /* 3 columns of 200px */
  grid-template-rows: 100px 100px;          /* 2 rows of 100px */
  gap: 16px;
}

Grid vocabulary

Grid has its own vocabulary, which is worth mastering before going further: it comes up in every property and in the browser’s inspection tools.

  • Grid container — the element that has display: grid.
  • Grid items — the direct children of the container (not the grandchildren).
  • Track — an entire row or column of the grid.
  • Cell — the intersection of a row and a column, the smallest unit.
  • Grid line — the lines that separate the tracks, numbered starting from 1.
  • Area — a rectangle made up of one or more cells.

The fr unit and the repeat() function

The fr unit (“fraction”) distributes the available space proportionally — far more flexible than fixed pixels.

grid-fr.css
.grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr; /* 3 columns of equal width */
  gap: 16px;
}

.uneven-grid {
  display: grid;
  grid-template-columns: 2fr 1fr; /* the 1st column is twice as wide as the 2nd */
}

.repeated-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr); /* equivalent to "1fr 1fr 1fr 1fr" */
}

The minmax() function

minmax(min, max) defines a track that never goes below a minimum size and never exceeds a maximum size. Combined with fr, it gives you columns that stretch while staying readable. The auto keyword lets the content decide the size of the track.

grid-minmax.css
.grid {
  display: grid;
  grid-template-columns: minmax(150px, 1fr) minmax(300px, 2fr) auto;
  /* column 1: at least 150px; column 2: at least 300px and twice as wide;
     column 3: sized to fit its content */
}

Responsive grids with auto-fit

A very powerful combination lets you create a card grid that automatically adapts to the number of columns that fit, without any media query.

grid-auto-fit.css
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}
/* Each card is at least 250px wide, and the grid adds or removes
   columns automatically depending on the screen width. */

Spacing: gap, row-gap and column-gap

gap sets the space between tracks, never around the grid. With a single value, it applies to both rows and columns; with two, the first applies to rows and the second to columns. You can also set them separately with row-gap andcolumn-gap.

grid-gap.css
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 24px;            /* same space between rows and columns */
  gap: 12px 32px;       /* 12px between rows, 32px between columns */
  row-gap: 12px;        /* between rows only */
  column-gap: 32px;     /* between columns only */
}

auto-fit or auto-fill?

Both keywords create as many columns as possible, but they differ when there is little content. With auto-fill, empty columns stay reserved: the cards keep their size and leave a gap on the right. With auto-fit, empty columns collapse and the existing cards stretch to take up the full width.

auto-fit-vs-auto-fill.css
/* Empty columns are kept: the cards don't stretch */
.grid-a {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}

/* Empty columns disappear: the cards stretch */
.grid-b {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

Placing an item precisely

You can position an item at a precise spot in the grid using grid line numbers (the lines between the cells, not the cells themselves).

grid-placement.css
.grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(3, 100px);
  gap: 12px;
}

.big-block {
  grid-column: 1 / 3;  /* from vertical line 1 to line 3: spans 2 columns */
  grid-row: 1 / 3;     /* spans 2 rows */
}

.shorthand {
  grid-column: span 2; /* equivalent: spans 2 columns from its position */
}

Aligning the content of cells

Once the grid is in place, you can control how items are aligned inside their cell:justify-items works on the horizontal axis, align-items on the vertical axis, andplace-items combines both. To distribute the grid itself within its container, use justify-content and align-content.

grid-alignment.css
.grid {
  display: grid;
  grid-template-columns: repeat(3, 150px);
  height: 400px;
  justify-items: center;   /* centers each item horizontally in its cell */
  align-items: center;     /* centers each item vertically in its cell */
  place-items: center;     /* shorthand for the two previous lines */
  justify-content: center; /* centers the whole grid in the container */
}

.special {
  justify-self: end;       /* one specific item aligns to the right of its cell */
}

Implicit rows and columns

If the grid contains more items than declared cells, Grid automatically creates new rows: this is called the implicit grid. You set their size withgrid-auto-rows (or grid-auto-columns for implicit columns), and the filling direction with grid-auto-flow.

grid-implicit.css
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: minmax(100px, auto); /* each new row is at least 100px tall */
  grid-auto-flow: row;                 /* fills row by row (default) */
  gap: 16px;
}

Naming areas with grid-template-areas

For a full-page layout, you can name areas and literally draw the arrangement in the CSS — very readable.

grid-areas.css
.page {
  display: grid;
  grid-template-columns: 220px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header"
    "sidebar content"
    "footer footer";
  min-height: 100vh;
}

.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.footer  { grid-area: footer; }
grid-areas.html
<div class="page">
  <header class="header">Header</header>
  <nav class="sidebar">Menu</nav>
  <main class="content">Main content</main>
  <footer class="footer">Footer</footer>
</div>
  • grid-template-areas makes the page structure readable at a glance, right in the CSS.
  • You can leave a cell empty with a dot . in the pattern.
  • This technique combines very well with media queries to rearrange the whole page by changing just the grid-template-areas pattern.

Rearranging the page depending on the screen

With grid-template-areas, adapting a page to mobile comes down to redrawing the pattern inside a media query: the HTML elements don’t have to move. Here, the sidebar moves below the content on a narrow screen.

grid-areas-responsive.css
@media (max-width: 700px) {
  .page {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr auto auto; /* one row per area: header, content, sidebar, footer */
    grid-template-areas:
      "header"
      "content"
      "sidebar"
      "footer";
  }
}

Debugging a grid

In the browser inspector, a small “grid” badge appears next to the container. Clicking it displays the numbered lines, the area names and the gaps: the ideal tool for understanding where an item is actually placed.

When to choose which?

A simple rule: if you’re aligning items in a single direction (a navigation bar, a list of buttons), use Flexbox. If you’re building atwo-dimensional layout (a whole page, a card grid), use Grid. The two combine very well together.

Exercise

Create a grid of 6 cards with display: grid;and repeat(auto-fit, minmax(200px, 1fr)), with spacing (gap) between each card.

Exercise: placement

Create a grid with 4 columns and 3 rows. Place a large block that spans 2 columns and 2 rows with grid-column andgrid-row, then fill the rest with small cards.

Exercise: alignment

In a 3-column grid, center the content of each cell withplace-items: center;, then align a single item to the right of its cell withjustify-self: end;.

Going further

Build a complete page layout (header, sidebar, content, footer) withgrid-template-areas, as in the example above.