π Introduction
The align-items property in Flexbox controls how flex items are alignedalong the cross-axis (the axis perpendicular to the flex-direction). This is one of the most important Flexbox properties for vertical or horizontal centering, depending on layout direction. Mastering align-items helps you create clean, balanced UI layouts effortlessly. π¨β¨
π― What Does align-items Do?
- Aligns all flex items along the cross-axis
- Applies to the flex container (not individual items)
- Works together with
flex-directionto determine orientation - Helps with vertical centering when the flex-direction is row
β¨ Syntax
Syntax
.container {
display: flex;
align-items: stretch | flex-start | flex-end | center | baseline;
}π§© Understanding the Cross Axis
The cross axis depends on the flex-direction:
- flex-direction: row β cross-axis is vertical (topβbottom)
- flex-direction: column β cross-axis is horizontal (leftβright)
π§© align-items Values
1. stretch (default)
Items stretch to fill the container along the cross-axis.
stretch
.container {
display: flex;
align-items: stretch;
}2. flex-start
Items align to the start of the cross-axis.
flex-start
.container {
display: flex;
align-items: flex-start;
}3. flex-end
Items align to the end of the cross-axis.
flex-end
.container {
display: flex;
align-items: flex-end;
}4. center
Items align to the center of the cross-axis. This is how Flexbox makes vertical centering incredibly easy! π
center
.container {
display: flex;
align-items: center;
}5. baseline
Items align based on their text baseline. Useful when text content must align naturally across different-sized items.
baseline
.container {
display: flex;
align-items: baseline;
}π₯ Practical Examples
1. Vertically Center Items (Common Use Case)
Vertical Center
.container {
display: flex;
align-items: center; /* vertical center when flex-direction: row */
}2. Top Align Items
Top Alignment
.container {
display: flex;
align-items: flex-start;
}3. Bottom Align Items
Bottom Alignment
.container {
display: flex;
align-items: flex-end;
}4. Using align-items with Column Direction
Column Alignment
.container {
display: flex;
flex-direction: column;
align-items: center; /* horizontally centers items now */
}5. Align Items by Text Baseline
Baseline Example
.container {
display: flex;
align-items: baseline;
}π Comparison Table
| Value | Meaning | Cross-Axis Effect |
|---|---|---|
| stretch | Items stretch to fill | Full height (row) / full width (column) |
| flex-start | Align at start | Top (row) / left (column) |
| flex-end | Align at end | Bottom (row) / right (column) |
| center | Align at center | Vertical center (row) / horizontal center (column) |
| baseline | Align text baselines | Matches typography lines |
π§ Tips & Best Practices
- Use align-items: center for easy vertical centering in flex row layouts
- For per-item control, use align-self
- Remember: cross-axis changes if flex-direction changes
- Use baseline when text alignment matters
π Conclusion
The align-items property is a key part of Flexbox layout control. It determines how items align along the cross-axis, enabling easy vertical or horizontal centering and precise alignment behaviors. Mastering align-items unlocks smooth, clean, and flexible UI designs. π