align-items: Cross Axis Alignment
You'll learn how to position flex items perpendicular to the main axis using the align-items property.
The Core Idea
While justify-content aligns items along the main axis (the direction they flow), align-items controls alignment on the cross axis—the perpendicular direction. Think of it like organizing books on a shelf: justify-content spreads them left-to-right, while align-items determines whether they sit at the top, bottom, or center of the shelf's height.
The align-items property is set on the flex container and affects all flex items inside.
Key Values
stretch (default)
Items stretch to fill the container's height (or width, if using column direction). This is why flex items often appear equal in height automatically.
flex-start
Items align to the start of the cross axis—top for row layouts, left for column layouts.
flex-end
Items align to the end of the cross axis—bottom for rows, right for columns.
center
Items align to the cross axis center, perfect for vertically centering content.
baseline
Items align so their text baselines match, useful when items contain text of different sizes.
Practical Example
.container {
display: flex;
height: 200px;
align-items: center; /* vertically centers all items */
}
In this row-direction flex container, all items will be vertically centered regardless of their individual heights.
Visual Analogy
Imagine parking cars in parallel spaces. justify-content determines spacing between cars along the parking lane, while align-items decides whether all cars pull forward to the curb, stay centered in their spaces, or back up to the rear line.
Key Takeaway: align-items aligns flex items along the cross axis, with values like stretch, flex-start, center, and baseline controlling how items position themselves perpendicular to the main flow direction.