Main Axis Alignment with justify-content
What you'll learn: How to distribute flex items along the main axis using the justify-content property.
The Core Idea
Once you've created a flex container and set its main axis direction with flex-direction, you control how items spread out along that main axis using justify-content. Think of it like arranging books on a shelf—you can push them all to the left, center them, spread them evenly, or space them out with gaps.
The Five Key Values
flex-start (default): Packs all items at the beginning of the main axis. If your main axis runs left-to-right, items huddle on the left side.
flex-end: Packs all items at the end of the main axis. Items move to the right side (in a left-to-right main axis).
center: Groups all items in the middle of the main axis, with equal space on both sides.
space-between: Distributes items evenly with the first item at the start and the last item at the end. The space between items is equal, but there's no space at the edges.
space-around: Distributes items with equal space around each item. Each item gets equal left and right margins, so the space between items is double the space at the edges.
space-evenly: Distributes items so all spaces (between items and at edges) are exactly equal.
When to Use Each
- Use
centerfor navigation menus or button groups - Use
space-betweenfor headers with logo-left, nav-right layouts - Use
flex-startorflex-endfor alignment to specific sides - Use
space-aroundorspace-evenlyfor evenly distributed cards or icons
Key Takeaway: justify-content controls distribution along the main axis (set by flex-direction), letting you align flex items at the start, end, center, or spread them with various spacing patterns.