This property aligns flex items along the cross axis of the current line of the flex container. The cross axis is perpendicular to the main axis.
Example 1: Stretching to Fill (Default)
HTML
<div class="flex-container align-stretch">
<div class="item">One</div>
<div class="item" style="font-size: 24px;">Two</div>
<div class="item">Three</div>
</div>
CSS
.flex-container {
display: flex;
height: 150px; /* Container needs height for alignment to be visible. */
border: 2px solid #000;
}
.align-stretch {
align-items: stretch; /* Stretches items to fill the container's height (default). */
}
Explanation
align-items: stretch
is the default value. It causes flex items to stretch to fill the height (or width, in a column layout) of the flex container along the cross axis.
Example 2: Aligning to Flex Start
HTML
<div class="flex-container align-flex-start">
<div class="item">One</div>
<div class="item" style="font-size: 24px;">Two</div>
<div class="item">Three</div>
</div>
CSS
.flex-container {
display: flex;
height: 150px;
border: 2px solid #000;
}
.align-flex-start {
align-items: flex-start; /* Aligns items to the start of the cross axis. */
}
Explanation
align-items: flex-start
aligns all items to the start of the cross axis (the top edge in a standard row layout).
Example 3: Aligning to Flex End
HTML
<div class="flex-container align-flex-end">
<div class="item">One</div>
<div class="item" style="font-size: 24px;">Two</div>
<div class="item">Three</div>
</div>
CSS
.flex-container {
display: flex;
height: 150px;
border: 2px solid #000;
}
.align-flex-end {
align-items: flex-end; /* Aligns items to the end of the cross axis. */
}
Explanation
align-items: flex-end
aligns all items to the end of the cross axis (the bottom edge in a standard row layout).
Example 4: Aligning to the Center
HTML
<div class="flex-container align-center">
<div class="item">One</div>
<div class="item" style="font-size: 24px;">Two</div>
<div class="item">Three</div>
</div>
CSS
.flex-container {
display: flex;
height: 150px;
border: 2px solid #000;
}
.align-center {
align-items: center; /* Centers items along the cross axis. */
}
Explanation
align-items: center
is a very common value used to vertically center a group of items within a container.
Example 5: Aligning by Baseline
HTML
<div class="flex-container align-baseline">
<div class="item" style="font-size: 12px;">Small</div>
<div class="item" style="font-size: 24px;">Medium</div>
<div class="item" style="font-size: 36px;">Large</div>
</div>
CSS
.flex-container {
display: flex;
height: 150px;
border: 2px solid #000;
}
.align-baseline {
align-items: baseline; /* Aligns items based on their text's baseline. */
}
Explanation
align-items: baseline
aligns items so that their text baselines are aligned. This is useful for ensuring text across different-sized elements lines up properly.
Example 6: Align Items in a Column Layout
HTML
<div class="flex-container column-align">
<div class="item">Item 1</div>
<div class="item">Longer Item 2</div>
<div class="item">Item 3</div>
</div>
CSS
.flex-container {
display: flex;
flex-direction: column; /* Main axis is vertical. */
height: 200px;
border: 2px solid #d9534f;
}
.column-align {
align-items: flex-end; /* Aligns items to the right side of the container. */
}
Explanation
When flex-direction
is column
, the cross axis becomes horizontal. Therefore, align-items: flex-end
aligns the items to the right edge of the container.
Example 7: Overriding align-items
with align-self
HTML
<div class="flex-container override-align">
<div class="item">Start</div>
<div class="item self-center">Center</div>
<div class="item self-end">End</div>
</div>
CSS
.flex-container {
display: flex;
height: 150px;
border: 2px solid #337ab7;
}
.override-align {
align-items: flex-start; /* All items default to aligning at the top. */
}
.self-center { align-self: center; } /* This item overrides the parent and centers itself. */
.self-end { align-self: flex-end; } /* This item aligns itself to the bottom. */
Explanation
While align-items
sets the default alignment for all items, the align-self
property can be applied to an individual flex item to override its alignment.