Sub topic 13
Distributing Multiple Lines with align-content
This chapter assumes you know display: flex, the main and cross axes, flex-wrap, and the difference between justify-content and align-items. Here you will learn how align-content positions whole flex lines inside a container, and why it often seems to do nothing.
What align-content controls
When flex-wrap: wrap is set, items that do not fit on one row move to a new row. Each row is a flex line. Two properties then align things on the cross axis:
align-itemsplaces each item inside its own line.align-contentplaces the lines inside the container.
align-content is to flex lines what justify-content is to items on the main axis. It distributes free space in the cross direction, between and around lines.
Conditions for it to work
align-content has an effect only when both of these are true:
- The container has more than one flex line. A container with
flex-wrap: nowraphas exactly one line, soalign-contentis ignored. - The container is larger on the cross axis than its lines combined. If the height is
auto, the container shrinks to fit its lines and there is no free space to distribute.
A runnable example
The container is 300px wide and 300px tall. Six items are 100px wide and 60px tall, so three fit per line. That gives two lines of 60px each, which leaves 180px of free vertical space.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>align-content demo</title>
<style>
.box {
display: flex;
flex-wrap: wrap;
width: 300px;
height: 300px;
outline: 2px solid #888;
align-content: center;
}
.box div {
width: 100px;
height: 60px;
background: steelblue;
box-sizing: border-box;
border: 1px solid white;
}
</style>
</head>
<body>
<div class="box">
<div></div><div></div><div></div>
<div></div><div></div><div></div>
</div>
</body>
</html>With center, 90px of free space sits above the first line and 90px below the second. Change the align-content value and reload to compare the results below.
The values
Offsets below are measured from the container's top edge for the example above. The container is 300px tall and each line is 60px.
| Value | Line 1 starts at | Line 2 starts at | Behavior |
|---|---|---|---|
| `stretch` | 0px | 150px | Each line grows by an equal share of the free space (90px). |
| `flex-start` | 0px | 60px | Lines pack at the start. |
| `flex-end` | 180px | 240px | Lines pack at the end. |
| `center` | 90px | 150px | Lines pack in the middle. |
| `space-between` | 0px | 240px | First and last lines touch the edges. Free space goes between lines. |
| `space-around` | 45px | 195px | Each line gets equal space on both sides, so edge gaps are half the gap between lines. |
| `space-evenly` | 60px | 180px | Every gap, including the edges, is the same. |
start and end also work and follow the writing mode, while flex-start and flex-end follow the flex direction.
The default is not "start"
The initial value is normal, which behaves like stretch in flex containers. If you give a wrapping container extra height and set nothing else, the lines already spread out. The free space is added to each line's height instead of appearing as gaps.
Whether the items fill their grown line depends on align-items:
- With the default
align-items: stretchand no explicit height, items grow to fill the taller line. - With a fixed height, as in the example, items keep that height and sit at the top of their line. This is why the default layout can look like uneven empty space between rows.
Interaction with gap
gap (or row-gap) is added between lines first. align-content then distributes the space that remains. With space-between, the visible distance between lines is row-gap plus the distributed share.
.box {
display: flex;
flex-wrap: wrap;
height: 300px;
row-gap: 10px;
align-content: flex-start; /* lines stay exactly 10px apart */
}If the total of line sizes and gaps is larger than the container, there is no free space. Packing values like center can then push lines outside the container on both sides, which makes content hard to scroll to.
Column containers
align-content always works on the cross axis. For flex-direction: column, the cross axis is horizontal. The columns become the flex lines, so you need a fixed width, and the container needs a height that forces wrapping:
.columns {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 200px;
width: 500px;
align-content: space-between;
}Here the container wraps into columns once items exceed 200px of height. align-content then spreads those columns across the 500px width.
Common mistakes
- Using it on a one-line container. Without
flex-wrap, usealign-items. Settingalign-contentwill do nothing. - Forgetting the container size. With
height: auto, there is nothing to distribute. Setheight,min-height, or let a parent stretch the container. - Confusing it with align-items. To center the content of each row, use
align-items. To center the rows as a block, usealign-content. For wrapping content that must be centered both ways, you often set both. - Expecting per-line control. There is no way to align one line differently.
align-selftargets items, not lines.
Exercise
A container has display: flex; flex-wrap: wrap; align-content: center; and contains eight cards that wrap onto three rows. The rows sit at the top of the container instead of in the middle. The container has no height set. Why, and what is the smallest change that makes the rows center?
Show answer
With height: auto, the container is exactly as tall as its three lines, so there is no free cross-axis space to distribute and align-content: center has nothing to move. Give the container extra height, for example min-height: 400px. The rows then center vertically. If you keep a fixed height, make sure it is larger than the combined row heights and gaps.
Exercise
In a 300px-tall wrapping container with two 60px lines and no gap, which align-content value places the second line starting at 180px?
Show answer
space-evenly. The 180px of free space is split into three equal 60px gaps, one above, one between, and one below. The first line starts at 60px and the second at 60 + 60 + 60 = 180px.