On this page
CSS justify-content: Align Flex Items on the Main Axis
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
.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
| Value | Result |
|---|---|
flex-start | Items are packed at the start of the main axis. |
flex-end | Items are packed at the end of the main axis. |
center | Items are centered with equal free space on both sides. |
space-between | First item at the start, last item at the end, equal space between items. |
space-around | Equal space around each item. The edge gaps are half the size of the gaps between items. |
space-evenly | Equal 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
startandend: logical values that follow the writing mode instead of the flex direction.leftandright: physical values that ignore flex-direction.stretch: has no effect in flexbox, but works in CSS Grid.safe centerandunsafe center: with safe, an overflowing item is aligned to start instead of being cut off on both sides.
Centering with justify-content
.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.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
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.
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.
Was this page helpful?