Cross Axis Alignment with align-items
You'll learn how to position flex items along the cross axis (perpendicular to your 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. If your flex items flow left-to-right, align-items positions them vertically within the container. If they flow top-to-bottom, it positions them horizontally.
Think of it like organizing books on a shelf: justify-content spaces them along the shelf's length, while align-items controls whether they sit at the bottom, stand upright centered, or lean against the top.
The Values
stretch (default) — Items expand to fill the container's cross-axis height/width. Your flex items will grow to match the tallest/widest item or the container size.
flex-start — Items align to the start of the cross axis (top for horizontal layouts, left for vertical layouts).
flex-end — Items align to the end of the cross axis (bottom for horizontal, right for vertical).
center — Items center along the cross axis. Perfect for vertically centering content!
baseline — Items align so their text baselines line up. Useful when flex items contain text of different sizes but you want a consistent text line.
Usage
Apply align-items to the flex container, not individual items:
.container {
display: flex;
align-items: center; /* centers all items on cross axis */
}
Key Takeaway: align-items controls how flex items align perpendicular to the main axis, with options including stretch, flex-start, flex-end, center, and baseline—applied to the flex container itself.