Skip to main content
Syllabus
On this page

CSS justify-content: Align Flex Items on the Main Axis

Badar KhalilUpdated October 1, 2026 4 min read

What Is justify-content in CSS?

The justify-content property aligns flex items along the main axis of a flex container and distributes the free space between and around them. It is one of the most used flexbox properties for building navigation bars, headers, toolbars, button groups and centered layouts. If you want to center items horizontally with CSS flexbox, justify-content: center is the answer.

Syntax

Code
.container {
  display: flex;
  justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;
}

The default value is flex-start (the initial value is normal, which behaves like flex-start in flexbox).

How justify-content Works

justify-content only distributes free space, which is the leftover space in the main axis after all items have taken their size. If the items already fill the whole line, there is no free space and justify-content will seem to do nothing. Also remember that the main axis depends on flex-direction. In a row it is horizontal, in a column it is vertical.

justify-content Values Explained

ValueResult
flex-startItems are packed at the start of the main axis.
flex-endItems are packed at the end of the main axis.
centerItems are centered with equal free space on both sides.
space-betweenFirst item at the start, last item at the end, equal space between items.
space-aroundEqual space around each item. The edge gaps are half the size of the gaps between items.
space-evenlyEqual space between items and at both edges.

justify-content: flex-start

The default behavior. Items are grouped at the beginning of the line, so with a row direction they sit on the left in left-to-right languages.

justify-content: flex-end

Items are grouped at the end of the line. Useful for right-aligned action buttons, such as Save and Cancel in a form footer.

justify-content: center

Items are grouped in the middle. Combine it with align-items: center to center anything both horizontally and vertically, which is the modern way to center a div in CSS.

justify-content: space-between

The first item touches the start edge, the last item touches the end edge, and the remaining space is split between items. This is the classic navigation bar pattern with a logo on the left and links on the right.

justify-content: space-around

Each item gets equal space on both sides. This results in half-size gaps at the container edges and full-size gaps between items.

justify-content: space-evenly

Every gap, including the first and last, has exactly the same size. It gives the most visually balanced result for icon rows and feature lists.

Other Values You Should Know

  • start and end: logical values that follow the writing mode instead of the flex direction.
  • left and right: physical values that ignore flex-direction.
  • stretch: has no effect in flexbox, but works in CSS Grid.
  • safe center and unsafe center: with safe, an overflowing item is aligned to start instead of being cut off on both sides.

Centering with justify-content

Code
.center {
  display: flex;
  justify-content: center; /* horizontal in row */
  align-items: center;     /* vertical in row */
  min-height: 100vh;
}

justify-content in a Column Layout

When flex-direction is column, the main axis is vertical, so justify-content controls vertical positioning. The container needs a height larger than its content, otherwise there is no free space to distribute.

justify-content and the gap Property

The gap property sets the minimum space between items. justify-content then distributes whatever free space remains. With space-between, the final space between items is at least the gap value.

justify-content Cheat Sheet

  • Left aligned: flex-start
  • Right aligned: flex-end
  • Centered: center
  • Logo left, menu right: space-between
  • Balanced icon row: space-evenly

Common Mistakes

  • Using justify-content when there is no free space. Add width to the container or reduce item sizes.
  • Confusing justify-content and align-items. justify-content is main axis, align-items is cross axis.
  • Forgetting the height in column layouts.
  • Using justify-content on the items. It is a container property.

FAQ

How do I center a div horizontally with flexbox?

Set display: flex and justify-content: center on the parent.

What is the difference between space-around and space-evenly?

space-around gives half-size gaps at the edges, space-evenly gives all gaps the same size.

Why is justify-content not working?

Check that the parent has display: flex, that there is free space on the main axis, and that you are not mixing up row and column direction.

Key Takeaways

  • justify-content aligns items on the main axis.
  • Six main values: flex-start, flex-end, center, space-between, space-around, space-evenly.
  • It distributes free space only.
  • Pair it with align-items to center in both directions.
Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Exercise: Build a Header with justify-contentHTML

Build a header with a logo on the far left and three links on the far right. Use display: flex on the header and the correct justify-content value to push the logo and the links to opposite edges. Then create a footer button group where two buttons are aligned to the right side.

Try it Yourself HTML
Output

Press Run to execute.

Show expected output
The logo sits on the far left and the links on the far right of the header. The Cancel and Save buttons are aligned to the right edge of the footer.

This is a self-check — compare your result with the expected output above.

Quiz: justify-content Quiz Pass 70%
Loading quiz…

Was this page helpful?