This property is used to align the lines of a flex container when there is extra space in the cross-axis. It only has an effect on multi-line flex containers (when flex-wrap
is wrap
or wrap-reverse
) and has no effect on single-line containers.
Example 1: Aligning Lines to the Start
HTML
<div class="flex-container content-start">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
<div class="item">4</div> <div class="item">5</div>
</div>
CSS
.flex-container {
display: flex;
flex-wrap: wrap; /* Must be a multi-line container. */
height: 200px; /* Must have extra space in the cross-axis. */
border: 2px solid #000;
}
.content-start {
align-content: flex-start; /* Packs lines to the start of the container. */
}
Explanation
align-content: flex-start
packs all the lines of flex items toward the start of the cross axis (the top of the container).
Example 2: Aligning Lines to the Center
HTML
<div class="flex-container content-center">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
<div class="item">4</div> <div class="item">5</div>
</div>
CSS
.flex-container {
display: flex;
flex-wrap: wrap;
height: 200px;
border: 2px solid #000;
}
.content-center {
align-content: center; /* Packs lines to the center of the container. */
}
Explanation
align-content: center
groups all the lines and centers them vertically within the flex container, with equal space above and below the group.
Example 3: Aligning Lines to the End
HTML
<div class="flex-container content-end">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
<div class="item">4</div> <div class="item">5</div>
</div>
CSS
.flex-container {
display: flex;
flex-wrap: wrap;
height: 200px;
border: 2px solid #000;
}
.content-end {
align-content: flex-end; /* Packs lines to the end of the container. */
}
Explanation
align-content: flex-end
packs all the lines of flex items toward the end of the cross axis (the bottom of the container).
Example 4: Distributing Lines with Space Between
HTML
<div class="flex-container content-between">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
<div class="item">4</div> <div class="item">5</div>
</div>
CSS
.flex-container {
display: flex;
flex-wrap: wrap;
height: 200px;
border: 2px solid #000;
}
.content-between {
align-content: space-between; /* Distributes lines with space between them. */
}
Explanation
align-content: space-between
takes all the extra vertical space and distributes it evenly between the lines of flex items.
Example 5: Distributing Lines with Space Around
HTML
<div class="flex-container content-around">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
<div class="item">4</div> <div class="item">5</div>
</div>
CSS
.flex-container {
display: flex;
flex-wrap: wrap;
height: 200px;
border: 2px solid #000;
}
.content-around {
align-content: space-around; /* Distributes space around each line. */
}
Explanation
align-content: space-around
distributes the space such that each line has equal space above and below it.
Example 6: Stretching Lines to Fill Space
HTML
<div class="flex-container content-stretch">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
<div class="item">4</div> <div class="item">5</div>
</div>
CSS
.flex-container {
display: flex;
flex-wrap: wrap;
height: 200px;
border: 2px solid #000;
}
.content-stretch {
align-content: stretch; /* Stretches the lines to take up remaining space (default). */
}
Explanation
align-content: stretch
is the default value. It causes the lines themselves to stretch and consume any extra space in the cross axis.
Example 7: Align Content vs. Align Items
HTML
<div class="flex-container align-items-demo">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
</div>
<div class="flex-container align-content-demo">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
<div class="item">4</div> <div class="item">5</div>
</div>
CSS
.align-items-demo {
align-items: center; /* `align-items` works on a single line. */
height: 150px;
border: 2px solid #f0ad4e;
}
.align-content-demo {
flex-wrap: wrap;
align-content: center; /* `align-content` works on multiple lines. */
height: 250px;
border: 2px solid #5bc0de;
}
Explanation
This example highlights the difference: align-items
aligns items within a line, while align-content
aligns the lines themselves within the container. The first container centers the single line of items; the second container centers the two lines of items as a block.