Aligning grid items

Topic 3 of 4 in Advanced grid6 min readBeginnerFree course

Overview

Grid uses the same alignment properties as flexbox, but it has two kinds of alignment:

  1. aligning items inside their grid areas (cells),
  2. aligning the tracks themselves inside the grid container.

And it has a property for each direction of each kind. The names follow a simple pattern: justify- is the inline (row) axis, align- is the block (column) axis.

Aligning in grid. justify-items / align-items position items within their cells: stretch fills the cell, start puts the item at the top-left, center in the middle, end at the bottom-right. justify-content / align-content move the whole grid within a bigger container: start, center, end and space-between.

Aligning items in their cells

Property Set on Axis
justify-items container inline (horizontal in English) — all items
align-items container block (vertical) — all items
justify-self item inline — one item
align-self item block — one item
place-items / place-self shorthand: align then justify

Values: stretch (default), start, end, center, and baseline for align-*.

CSS
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  align-items: start;         /* items sit at the top of their row instead of stretching */
}

.grid .badge {
  justify-self: end;          /* this one item hugs the right edge of its cell */
}

Stretching

By default, grid items stretch in both directions, so every item fills its cell — that’s why cards in a grid row are all the same height without any extra CSS. As soon as you choose another value, the item shrinks to its content size and moves within the cell.

Images and other replaced elements are a special case: they keep their aspect ratio, so they won’t stretch to fill the cell unless you give them width: 100%; height: 100% (and usually object-fit: cover).

Unlike flexbox, justify works per item

Flexbox has no justify-items or justify-self — items on the main axis are packed together and moved with justify-content or auto margins. In grid, each item sits in its own cell, so it can be justified individually.

Auto margins also work in grid: margin-inline-start: auto pushes an item to the end of its cell.

Aligning the tracks

When the tracks don’t fill the grid container — fixed-size columns in a wider container, or a grid with a set height taller than its rows — there’s free space around the tracks. justify-content and align-content distribute it, exactly as in flexbox:

CSS
.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fill, 10rem);
  justify-content: center;          /* centre the block of columns in the container */
  gap: 1rem;
}

Values: start, end, center, stretch, space-between, space-around, space-evenly.

These have no effect when tracks use fr — fr tracks already consume all the free space, so there’s nothing left to distribute.

Centring with place-items

Centring something in both directions is a one-liner:

CSS
.screen {
  display: grid;
  place-items: center;
  min-height: 100dvh;
}

The container gets a single implicit cell filling it, and place-items: center centres the child within that cell. It works for any number of children (they stack in rows, each centred), and nothing needs a fixed size. Centering anything compares every centring technique.

Baseline alignment

align-items: baseline lines up the first line of text in each item of a row — useful in forms and in rows that mix heading sizes:

CSS
.form-row {
  display: grid;
  grid-template-columns: 10rem 1fr;
  align-items: baseline;    /* the label lines up with the text inside the input */
}

Quick reference

Goal Property
Don’t stretch items vertically align-items: start
Centre content within every cell place-items: center
Push one item to the right of its cell justify-self: end
Centre a block of fixed-width columns justify-content: center
Spread rows over a tall container align-content: space-between

Mnemonic: justify goes along the line of text (inline), align goes across it (block). -items sets all items, -self overrides one, -content moves the tracks.

Exercise

A stats panel and a centred login

Build .stats as a 3-column grid of stat boxes where every number is centred horizontally and vertically inside its cell, but the last box’s label is aligned to the bottom-right of its cell using the -self properties. Then make .screen fill the viewport and centre a .login box of natural width in the middle with a single grid property on the container.

HTML
<div class="stats">
  <div class="stat"><strong>49</strong></div>
  <div class="stat"><strong>12h</strong></div>
  <div class="stat"><span class="label">updated today</span></div>
</div>

<div class="screen">
  <form class="login">Log in form</form>
</div>

<style>
  * { box-sizing: border-box; }
  body { margin: 0; }
  .stat { background: #eaf0fc; }
  .login { background: #f1ecfd; padding: 2rem; }
  /* your code here */
</style>

Summary

  • justify-items aligns items along the inline (row) axis inside their cells; align-items along the block (column) axis.
  • justify-self and align-self override alignment for a single item; place-self sets both.
  • Items stretch to fill their cells by default; any other alignment sizes them to their content.
  • justify-content and align-content move the whole set of tracks when the grid is smaller than its container.
  • place-items: center on a grid container is the shortest way to centre something in both directions.