Sub topic 13
Distributing multiple lines with align-content
This chapter assumes you know display: flex, the main and cross axes, align-items, and flex-wrap. Here you will learn what align-content does and why it often seems to do nothing.
What align-content controls
When flex-wrap: wrap is set, items break into several flex lines. align-items positions items inside their own line. align-content positions the lines themselves along the cross axis, inside the container.
The property has no visible effect unless two conditions hold:
- The container has more than one flex line.
- The container has extra cross-axis space, such as a fixed
heightormin-heightlarger than the lines need.
Without extra space, the lines fill the container and there is nothing to distribute.
Values
| Value | Effect on the lines |
|---|---|
| `stretch` (default) | Lines grow equally to fill the container. |
| `flex-start` | Lines pack at the cross-start edge. |
| `flex-end` | Lines pack at the cross-end edge. |
| `center` | Lines pack in the middle. |
| `space-between` | First and last lines touch the edges, with equal space between lines. |
| `space-around` | Equal space around each line. |
| `space-evenly` | Equal space between lines and at both edges. |
A worked example
.tags {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
height: 300px;
align-content: flex-start;
}
.tag {
flex: 0 0 120px;
height: 40px;
}Suppose the container is 400px wide. Three tags fit on each line, so six tags make two lines. Each line is 40px tall, and the gap adds 0.5rem between them. That leaves most of the 300px unused.
With the default stretch, the free space is split between the lines. Each line becomes taller, and the gap between the tags' rows looks larger than you asked for. With flex-start, the lines stay at their natural height at the top. The leftover space collects at the bottom.
align-content versus align-items
The two properties work at different levels. If you mix them up, adjustments appear to have no effect.
align-itemsandalign-selfplace an item within its flex line.align-contentplaces whole lines within the container.
With stretch as the default, each line expands to absorb free space. Items with align-items: stretch then grow with their line. If you set align-content: flex-start, the lines shrink to fit their content, and the stretched items are only as tall as the tallest item in each line.
Common mistakes
- Setting
align-contenton a container with noflex-wrap. There is only one line, so nothing changes. - Setting it on a container whose height comes from its content. No free space exists.
- Expecting it to change spacing between items in the same line. Use
gaporjustify-contentfor that.
Exercise
A wrapped flex container has height: 400px and two lines of items. You want the first line at the very top and the second line at the very bottom, with the free space between them. Which align-content value do you use, and why does align-items not solve this?
Show answer
Use align-content: space-between. It distributes the lines themselves across the cross axis, placing the first line at the start edge and the last at the end edge. align-items only positions items inside each line, so it cannot move the lines apart.