Sub topic 13
Aligning items and tracks
This chapter covers the two levels of alignment in CSS Grid: placing an item inside its grid area, and placing the whole set of tracks inside the container. It assumes you can define columns and rows, use fr and repeat(), and place items with lines or areas.
Two levels of alignment
Grid alignment works at two separate levels, and most confusion comes from mixing them up.
- Item alignment positions each item inside its own grid area (the cell, or the span of cells, it occupies). The properties are
justify-itemsandalign-itemson the container, andjustify-selfandalign-selfon a single item. - Track alignment positions the grid's tracks inside the container when the tracks do not fill it. The properties are
justify-contentandalign-content, set on the container.
Both levels use the same two axes:
justify-*works on the inline axis, which is horizontal in left-to-right languages.align-*works on the block axis, which is vertical.
Aligning items inside their cells
By default, justify-items and align-items are normal, which behaves like stretch for most items. A stretched item fills its whole grid area. That is why grid items with no set size look like full-width blocks.
These values are the most useful:
| Value | Effect on the item |
|---|---|
| `stretch` | Fills the area on that axis (default for items without a fixed size) |
| `start` | Sits at the start edge of the area |
| `end` | Sits at the end edge of the area |
| `center` | Centered in the area |
| `baseline` | Aligns text baselines across items in the same row (`align-items` only in practice) |
Here is a container that applies the spine's alignment example:
.grid {
display: grid;
grid-template-columns: repeat(3, 200px);
justify-content: center;
align-items: center;
justify-items: start;
}In this rule, align-items: center centers every item vertically in its row. justify-items: start pushes every item to the left edge of its 200px column. The shorthand place-items: center start sets both in one line, with the align value first.
Overriding one item
align-self and justify-self take the same values but apply to a single item, so they override the container's setting:
.badge {
justify-self: end;
align-self: start;
}The shorthand is place-self.
Stretch has limits
An item stretches only if its size on that axis is auto. If you give it a width or height, it keeps that size and sits at the start edge. Images and other elements with an intrinsic aspect ratio also do not stretch to fill the area. Set justify-self or align-self explicitly when you need to position them.
.thumb {
width: 120px;
height: 120px;
place-self: center;
}Without place-self, this 120px box would sit in the top-left corner of a larger cell.
Aligning the tracks themselves
justify-content and align-content move the tracks as a group. They matter only when the tracks use less space than the container offers.
.layout {
display: grid;
grid-template-columns: repeat(3, 200px);
grid-template-rows: repeat(2, 100px);
height: 500px;
justify-content: space-between;
align-content: center;
}The three columns total 600px. If the container is wider, the leftover width is free space, and space-between puts it between the columns. The two rows total 100px × 2 = 200px, and the container is 500px tall. align-content: center puts the 300px of leftover height half above and half below the rows.
The distribution values are:
| Value | Where the free space goes |
|---|---|
| `start` / `end` / `center` | Packs the tracks at one edge or in the middle |
| `space-between` | Between tracks only, none at the edges |
| `space-around` | Around each track, so edge gaps are half the inner gaps |
| `space-evenly` | Equal space everywhere, including the edges |
| `stretch` | Grows `auto`-sized tracks to absorb the space |
The shorthand place-content sets align-content then justify-content.
Why justify-content sometimes does nothing
The most common mistake is setting justify-content: center on a grid whose columns are 1fr. A 1fr track absorbs all free space, so no free space is left to distribute. Track alignment only shows an effect when the tracks have fixed or content-based sizes, or when the sum of their sizes is smaller than the container. The same applies to align-content: the container needs a height, such as height or min-height: 100vh, larger than the rows.
Gap and distribution together
gap sets a minimum spacing. With space-between, space-around or space-evenly, the browser adds the free space on top of the gap. With justify-content: start, the gap is exactly what you set.
.toolbar {
display: grid;
grid-template-columns: repeat(4, 80px);
gap: 8px;
justify-content: space-between;
}Here the columns are always at least 8px apart, and they spread wider when the container allows it.
The stretch trap with auto tracks
With align-content: normal, which acts like stretch, tracks sized auto grow to fill extra space. Fixed tracks such as 100px and fr tracks do not change. If you give a tall container three auto rows and expect them to hug their content, they will spread out instead. Set align-content: start to keep them compact:
.page {
display: grid;
min-height: 100vh;
grid-template-rows: auto auto auto;
align-content: start;
}Auto margins in grid
Auto margins work on grid items as they do in flexbox. An item with margin-left: auto takes all the free space on its left within its area. Auto margins override justify-self and align-self on that axis. They are a quick way to push a single item to an edge without changing the container's rules.
Common errors
- Swapping the axes.
align-items: centercenters vertically, not horizontally. Usejustify-itemsfor horizontal. - Expecting
justify-contentto work withfrcolumns. There is no free space to distribute. - Using
align-itemson an item. The per-item properties arealign-selfandjustify-self. - Forgetting container height.
align-contentneeds room on the block axis. - Fixed-size items that refuse to stretch. An explicit
widthorheightturns off stretching on that axis.
Exercise
A container has display: grid; grid-template-columns: 1fr 1fr; justify-content: center;, and the columns still fill the full width. Why, and what would you change so the columns are centered with free space on both sides?
Show answer
Each 1fr track absorbs all of the free space, so the tracks fill the container and nothing is left for justify-content to distribute. Give the columns a size that totals less than the container, for example grid-template-columns: repeat(2, 240px);. Then justify-content: center centers the pair and splits the leftover width evenly on each side.
Exercise
You have a grid with a fixed height of 600px and three rows of 100px each. You want the rows spread out with no space above the first row or below the last. Which declaration do you add, and which value would add space at the edges as well?
Show answer
Add align-content: space-between;. It places the leftover 300px between the rows only. space-evenly (or space-around) would also add space at the top and bottom edges.