Skip to content
Tutorials Center
My learning

Topics

Lesson 77 of 139 ยท Grid

Grid Alignment

Align CSS Grid items inside their cells and the whole grid inside its container with justify-items, align-items, the -content and -self properties and place-*.

Grid gives you two separate alignment problems to solve. First, how does each item sit inside its grid cell: stretched to fill it, or tucked into a corner? Second, if the tracks do not fill the container, where does the whole grid sit: at the start, in the middle, spread out?

CSS answers both with a small, consistent family of properties. Once you see the pattern behind their names, you will never have to look them up again.

The naming pattern

Every alignment property is built from two parts.

Which axis?

  • justify- works along the inline axis: horizontally, in English and other left-to-right or right-to-left languages. Think "rows of text".
  • align- works along the block axis: vertically. Think "the direction paragraphs stack".

What is being aligned?

  • -items (on the container): every item inside its own cell.
  • -self (on an item): just this one item inside its cell. Overrides -items.
  • -content (on the container): the whole set of tracks inside the container.

That gives six properties, plus place- shorthands that set both axes at once:

Inline axis Block axis Both
Items in their cells justify-items align-items place-items
One item in its cell justify-self align-self place-self
Tracks in the container justify-content align-content place-content

If you know Flexbox, most of this is familiar. The difference is that Grid supports justify-items and justify-self, which Flexbox ignores, because a grid item has a real cell to move around in.

Aligning items inside cells

By default, grid items stretch to fill their cell in both directions. That is why a short card in a row of tall cards still matches their height. The common values are:

  • stretch (default): fill the cell.
  • start: hug the start edge (left or top, in English).
  • end: hug the end edge.
  • center: sit in the middle.
  • baseline (block axis): line up the first line of text across the row.

When an item stops stretching, it shrinks to the size of its content. That is often what makes the change visible.

justify-items and align-items

Use the menus to try each value. Notice the gray background of the container showing through: the cells are still there, the items just no longer fill them.

Aligning one item with -self

justify-self and align-self go on a single item and override whatever the container set. place-self sets both, block axis first:

CSS
.badge {
  place-self: start end;  /* align-self: start; justify-self: end; */
}
.icon {
  place-self: center;     /* center in both directions */
}

This is handy for things like a "close" button in the top-right corner of a card or a label pinned to the bottom of a hero area, all without absolute positioning.

Moving single items with place-self

Aligning the whole grid with -content

justify-content and align-content matter only when the tracks are smaller than the container. That happens when you use fixed sizes (100px) or content sizes (auto, max-content) and the container is bigger than their total. If any column uses fr, the free space is already used up and justify-content has nothing to do.

The values move or spread the tracks:

  • start, end, center: move the whole block of tracks.
  • space-between: first and last tracks touch the edges; extra space goes between.
  • space-around: equal space around each track (half-size at the edges).
  • space-evenly: identical space everywhere, edges included.
  • stretch: grow auto-sized tracks to fill the space.
justify-content and align-content

Notice that the space-* values actually make the gaps bigger: they add the leftover space between tracks on top of gap. Change the columns to repeat(3, 1fr) and justify-content stops doing anything, because the fr tracks swallow all the free space.

The place-* shorthands

The shorthands take the block axis first, then the inline axis. If you give one value, it is used for both.

CSS
.container {
  place-items: center;            /* align-items + justify-items */
  place-content: center start;    /* align-content: center; justify-content: start; */
}
.item {
  place-self: end;                /* align-self + justify-self */
}

The famous two-line centering trick comes straight from this:

CSS
.center-me {
  display: grid;
  place-items: center;
}

The container becomes a one-cell grid and its child sits in the middle of that cell, horizontally and vertically. See Centering Anything for when to use this versus other techniques.

Safe alignment and overflow

Centering has a hidden danger. If a centered item is bigger than its container, it overflows on both sides equally, and the part that sticks out the start edge cannot be scrolled to. Text can become unreachable.

The safe keyword fixes this: align-items: safe center centers when there is room, but falls back to start when the item would overflow. Its opposite, unsafe, keeps centering no matter what. The safe keyword works in all major browsers for Grid and Flexbox in recent versions; older browsers ignore the whole declaration, so put a plain fallback before it:

CSS
.dialog-wrapper {
  align-items: center;
  align-items: safe center;
}

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which property centers every item horizontally inside its own grid cell?

    justify- is the inline (horizontal) axis and -items targets every item inside its cell.
  2. 2. You set justify-content to center but nothing moves. The columns are repeat(4, 1fr). Why?

    -content alignment moves the tracks inside the container, which only matters when the tracks are smaller than the container.
  3. 3. What does place-self: start end do?

    place-* shorthands take the block axis first (align-self: start), then the inline axis (justify-self: end).
  4. 4. What is the default value that makes grid items fill their cells?

    Items stretch by default (normal behaves as stretch for most grid items), which is why they match the height of their row.
  5. 5. Why use safe center instead of center?

    safe falls back to start alignment when the item is too large, so no content becomes unreachable.

Summary

  • justify-* aligns along the inline (horizontal) axis; align-* along the block (vertical) axis.
  • -items (container) aligns every item in its cell; -self (item) overrides it for one item.
  • -content (container) moves or spreads the tracks themselves and only matters when tracks are smaller than the container.
  • place-items, place-self and place-content set block then inline in one declaration.
  • Items stretch by default; display: grid; place-items: center; centers a child in both directions.
  • Use safe alignment when content might be larger than its container.

Next: Subgrid, where nested elements finally line up with the grid around them.