CSS justify-content: Align Flex Items on the Main Axis
What is justify-content in CSS Flexbox?
The CSS justify-content property aligns flex items along the main axis of the container and distributes the leftover free space. In a default row layout it controls horizontal alignment; in a column layout it controls vertical alignment. It is the go-to property for centering, navbar layouts and spacing items evenly.
Syntax
.container {
display: flex;
justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;
}The default value is flex-start (the browser treats it as normal, which behaves like start for flex items).
Values explained
| Value | Behavior | Typical use |
|---|---|---|
| flex-start | Items packed at the start of the main axis | Default left-aligned toolbars |
| flex-end | Items packed at the end of the main axis | Right-aligned action buttons |
| center | Items centered on the main axis | Hero content, centered buttons |
| space-between | First item at start, last at end, equal space between items | Navbar with logo left and links right |
| space-around | Equal space around each item (half-size space at the edges) | Icon bars, tab bars |
| space-evenly | Equal space between items and at both edges | Feature rows, evenly spaced stats |
Detailed explanation
flex-start and flex-end
These follow the flex-direction. With row-reverse, flex-start is on the right side. If you want physical sides regardless of direction, use left or right. Modern CSS also supports start and end, which follow writing mode.
center
Centers the items as a group. Combined with align-items: center it gives you perfect centering both horizontally and vertically, the famous modern CSS centering technique.
space-between
The most used distribution value. If there is only one item it behaves like flex-start. Great for headers where the logo stays at one side and menu stays at the other.
space-around vs space-evenly
space-around gives each item equal space on both sides, so the gap between two items is twice the gap at the edges. space-evenly makes every gap, including the edges, exactly equal.
Important rule: free space is required
justify-content only works when there is extra space on the main axis. If items already fill the container (for example with flex-grow: 1), there is no free space to distribute and the property seems to do nothing.
Perfect centering recipe
.center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}Safe alignment
Using justify-content: safe center prevents items from being cut off when they are bigger than the container, by falling back to start alignment. Plain center can push content out of view on the start side where scrolling is not possible.
Real-world use cases
- Navbar: logo and menu with space-between.
- Hero section: center content horizontally.
- Pagination and button groups: center or flex-end.
- Footer layouts and pricing cards.
- Modal dialogs and login screens centered on the page.
Common mistakes
- Using justify-content in a column and expecting horizontal movement. In a column it moves items vertically.
- Forgetting container height in column layouts, so there is no free space.
- Confusing justify-content (main axis) with align-items (cross axis).
- Using margin: auto on items and justify-content together, since auto margins absorb free space first.
Best practices
- Always remember: justify-content = main axis, align-items = cross axis.
- Use gap alongside space-between to guarantee a minimum distance.
- Prefer space-evenly when you want visually balanced spacing.
- Test with RTL direction if your site is multilingual.
Tailwind CSS equivalents
justify-start, justify-end, justify-center, justify-between, justify-around, justify-evenly.
Trending keywords related to this topic
justify-content center, justify-content space-between, how to center a div with flexbox, CSS flexbox alignment, justify-content vs align-items, Tailwind justify-between, space-evenly vs space-around, CSS centering 2026, flexbox navbar tutorial.
Frequently Asked Questions
Which axis does justify-content control?
The main axis, which depends on flex-direction.
Why is justify-content not working?
Most often the container has no free space, display: flex is missing, or you are thinking of the wrong axis.
Difference between space-around and space-evenly?
space-around has half-size gaps at the edges; space-evenly has equal gaps everywhere.
Can justify-content be used in CSS Grid?
Yes, in Grid it aligns the whole grid tracks inside the container along the inline axis.
One-click copy
Neeche code blocks ke Copy button se code copy karein. Cheat Sheet example mein saari values ready-to-use hain.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Place the logo at the far left and the links group at the far right of the header using justify-content. Also make them vertically centered.
Press Run to execute.
Show expected output
Logo is on the far left, links on the far right and both are vertically centered (justify-content: space-between; align-items: center).This is a self-check — compare your result with the expected output above.
Was this page helpful?