Sub topic 13
Aligning Items and Tracks
This chapter assumes you can define a grid with grid-template-columns and grid-template-rows, and that you know what a grid cell and a track are. A track is a single row or column.
Two things you can align
Grid has two separate alignment jobs, and mixing them up is the most common error.
- Tracks are positioned inside the container when the tracks do not fill it. This uses
justify-contentandalign-content. - Items are positioned inside their own grid cell. This uses
justify-itemsandalign-items.
The naming rule is the same for both pairs. justify-* works along the inline axis (horizontal in English). align-* works along the block axis (vertical in English).
| Property | Applies to | Axis |
|---|---|---|
| `justify-content` | The set of tracks | Inline |
| `align-content` | The set of tracks | Block |
| `justify-items` | Every item in its cell | Inline |
| `align-items` | Every item in its cell | Block |
Aligning the tracks
Tracks only move if there is spare space. Columns sized with fr absorb all free space, so justify-content has no visible effect on them. Fixed sizes leave space to distribute.
.grid {
display: grid;
grid-template-columns: repeat(3, 200px);
justify-content: center;
align-items: center;
justify-items: start;
}The three 200px columns total 600px. If the container is wider, justify-content: center places that block in the middle. Other values include start, end, space-between, space-around, and space-evenly.
Vertical track alignment with align-content needs a container taller than its rows, for example one with min-height: 100vh.
Aligning the items
By default, items use stretch, so they fill their cell. Setting another value makes the item shrink to its content size.
In the example above, align-items: center centers each item vertically in its row. justify-items: start pushes each item to the left edge of its column.
Overriding one item
Use justify-self and align-self on a single item to override the container's setting.
.badge {
justify-self: end;
align-self: start;
}The place-items shorthand sets align-items and justify-items together. Setting place-items: center centers every item in both directions. place-content does the same for the two *-content properties.
Common errors
- No visible change from
justify-content. The columns usefrorautoand already fill the container. Use fixed orminmax()maximums to leave free space. - Items look stretched. The default is
stretch. Setalign-itemsorjustify-itemstostart,center, orend. - Wrong property pair.
justify-itemsmoves items within cells.justify-contentmoves the whole track set.
Exercise
A grid has grid-template-columns: repeat(3, 1fr). You add justify-content: center, and nothing moves. Why, and which property would you use if you wanted each item to sit in the middle of its own column?
Show answer
The three 1fr columns take all the free space, so there is nothing left to distribute and justify-content has no effect. To center each item within its column, use justify-items: center.
Exercise
What does place-items: end start set?
Show answer
It sets align-items: end and justify-items: start. The first value is the block axis, and the second is the inline axis.