Course contentsShow
← Web Development
Lesson 258 of 2,9825. FlexboxFree lesson

Main Axis Alignment with justify-content

Distributing flex items along the main axis with flex-start, flex-end, center, and space values.

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 center for navigation menus or button groups
  • Use space-between for headers with logo-left, nav-right layouts
  • Use flex-start or flex-end for alignment to specific sides
  • Use space-around or space-evenly for 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.