LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
HTML & CSS

Justify Content – Mastering Main Axis Alignment

Use justify-content on a flex container to decide how the free space is shared along the main axis. The common values are flex-start, flex-end, center, space-between, space-around and space-evenly. In a row the main axis is horizontal, and in a column it is vertical. The property has no visible effect when the items leave no free space.

Justify Content – Mastering Main Axis Alignment

Quick Answer: Use justify-content on a flex container to decide how the free space is shared along the main axis. The common values are flex-start, flex-end, center, space-between, space-around and space-evenly. In a row the main axis is horizontal, and in a column it is vertical. The property has no visible effect when the items leave no free space.

S1. Why Do We Need Justify Content?

Imagine you are building a form with a toolbar at the bottom. It has three buttons: Back, Save and Next. You want Back on the left edge, Next on the right edge, and Save in the middle.

In the previous article, we learned how items wrap onto new lines. But an item size is rarely the same as the container size, so some space is usually left over on each line.

Would you calculate a margin for every button by hand? That works for one toolbar width, but it breaks as soon as the width changes.

CSS Flexbox solves this with one property on the parent. Let's first see what happens without it.

S2. The Problem: Free Space That Nobody Controls

Free space = the room left on the main axis after all the items and gaps have been sized.

Imagine a 600px toolbar with three buttons, each 120px wide. The three buttons use 360px, so 240px is left over. The developer makes the toolbar a flex container, but gives it no alignment rule.

Example 1: A toolbar with the default alignment

Assumption: Each output line below is drawn at roughly one character per 20px, so it is approximate.

<!-- HTML -->
<div class="toolbar">
    <button class="btn">Back</button>
    <button class="btn">Save</button>
    <button class="btn">Next</button>
</div>

/* CSS */
.toolbar {
    display: flex;
    width: 600px;
}

.btn {
    width: 120px;
    padding: 12px;
    box-sizing: border-box;
    background: #E2E8F0;
}

Expected output

|[Back][Save][Next]            |

The three buttons sit together at the start.
The 240px of free space is left empty on the right.

The page renders, but it is not the toolbar we wanted. A flex container packs its items at the start of the main axis unless we say otherwise.

What goes wrong?

  1. Lopsided layout: All the items bunch up on one side, and the other side stays empty.
  2. Manual margins: Developers calculate a margin for each item by hand.
  3. Fragile numbers: A different container width or a new button makes every calculation wrong.
  4. Extra markup: Empty spacer elements are added just to push items apart.
  5. Poor clarity: The CSS does not say how the leftover space should be shared.

Now think about many toolbars, navigation bars and card footers on one website. Repeating hand-made spacing becomes harder to maintain.

Remember: The container is not broken. It is using its default alignment, which packs items at the start. The real problem is that nobody told it what to do with the free space.

Animation 1: How unused free space creates workarounds

Three buttons, no alignment rule

The red highlights show items stuck at the start while the free space sits unused.

⬅️BackFlex itemStuck at the start
💾SaveFlex itemStuck at the start
➡️NextFlex itemStuck at the start
 
.toolbar (display: flex) Default start alignment · free space left unused
No alignment rule

The animation highlights the plain container and the items packed at its start.

S3. The Solution: Share the Free Space with justify-content

Flexbox gives us a simpler design.

justify-content = a property on the flex container that decides how items are placed and how the free space is shared along the main axis.

Instead of calculating margins, add one rule to the parent. The browser measures the free space and shares it for you.

  • flex-start packs items at the start of the main axis.
  • flex-end packs items at the end.
  • center packs items in the middle.
  • space-between puts the first item at the start, the last item at the end, and equal space between items.
  • space-around gives each item equal space on both sides.
  • space-evenly makes the space between items and at both edges equal.

Now the same three buttons spread across the toolbar.

Example 2: A toolbar with space-between

Assumption: This example uses standard CSS supported by all modern browsers, and assumes a left-to-right language.

.toolbar {
    display: flex;
    justify-content: space-between;
    width: 600px;
}

.btn {
    width: 120px;
    padding: 12px;
    box-sizing: border-box;
    background: #E2E8F0;
}

Expected output

|[Back]      [Save]      [Next]|

Back sits at the start and Next sits at the end.
The 240px of free space is split into two equal gaps of 120px.

Notice the difference. The HTML stays the same, and one rule on the parent shares the free space between the items.

Animation 2: The justify-content design

One rule, shared space

The container measures the free space and shares it along the main axis.

📦ItemsBack, Save, Next3 x 120px
🕳️Free space240px left over
⚖️Sharing ruleChosen by the container
 
 
 
.toolbarjustify-content: space-between
Main axisFree space is shared along it
ResultFirst item at start, last item at end
Container-controlled alignment

The animation highlights the free space and the container rule that shares it.

Compare both approaches

Default alignment compared with justify-content
Without justify-content With justify-content
Items pack at the start. You choose start, end, center or spaced.
Margins are calculated by hand. The browser shares the free space.
A new width breaks the numbers. The layout adapts to the container width.
Spacer elements may be needed. No extra markup is needed.
Layout intent is hidden. Layout intent is clear.

The idea to remember

  • justify-content = a property set on the flex container, not on the items.
  • Main axis = the axis it works on. flex-direction decides which one that is.
  • Free space = container size minus item sizes and gaps. No free space means no visible effect.
  • Default = normal, which behaves like flex-start in a flex container.
  • Each flex line = shares its own free space when items wrap.

S4. Step-by-Step Code Breakdown

1. Start with a flex container

justify-content only works on a flex container. Add display: flex or display: inline-flex to the parent first. Without it, the property has no effect.

2. Understand free space

The browser first sizes the items and the gaps. Whatever room remains on the main axis is the free space. justify-content only decides where that free space goes. In our toolbar, 600px minus 3 x 120px leaves 240px.

3. Know the six values

Each value places the free space differently. The table uses the toolbar example with 240px of free space.

The common values of justify-content
Value Where the free space goes In the 240px example Typical use
flex-start After the last item 240px at the end Left-aligned toolbars
flex-end Before the first item 240px at the start Dialog buttons at the end
center Half before, half after 120px on each side Centered content
space-between Between the items only 120px between each pair Navigation bars and toolbars
space-around Around each item 40px at each edge, 80px between items Evenly padded item groups
space-evenly Between the items and at both edges 60px everywhere Equal visual rhythm

4. Compare all six values

The same three items behave differently under each value. Each container below has the same children. Only justify-content changes.

Example 3: All six values side by side

<!-- HTML (six containers with the same children) -->
<div class="demo start"><span>A</span><span>B</span><span>C</span></div>
<div class="demo end"><span>A</span><span>B</span><span>C</span></div>
<div class="demo center"><span>A</span><span>B</span><span>C</span></div>
<div class="demo between"><span>A</span><span>B</span><span>C</span></div>
<div class="demo around"><span>A</span><span>B</span><span>C</span></div>
<div class="demo evenly"><span>A</span><span>B</span><span>C</span></div>

/* CSS */
.demo {
    display: flex;
    width: 600px;
}

.demo span {
    width: 120px;
    padding: 10px;
    box-sizing: border-box;
}

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

Expected output

flex-start:
|[ A  ][ B  ][ C  ]            |

flex-end:
|            [ A  ][ B  ][ C  ]|

center:
|      [ A  ][ B  ][ C  ]      |

space-between:
|[ A  ]      [ B  ]      [ C  ]|

space-around:
|  [ A  ]    [ B  ]    [ C  ]  |

space-evenly:
|   [ A  ]   [ B  ]   [ C  ]   |

One character is about 20px. The free space is 240px.

Compare space-around with space-evenly. In space-around, each item has the same space on both sides, so the gap between two items is twice the gap at an edge. In space-evenly, every gap is the same.

5. Remember that it follows the direction

justify-content works on the main axis, so flex-direction changes what it means. In row-reverse, the start of the axis is on the right. In a column, the main axis is vertical, so the container needs extra height before there is any free space to share.

Example 4: justify-content in row-reverse and column

<!-- HTML (two containers with the same children) -->
<div class="rev"><span>A</span><span>B</span><span>C</span></div>
<div class="col"><span>A</span><span>B</span><span>C</span></div>

/* CSS */
.rev {
    display: flex;
    flex-direction: row-reverse;
    justify-content: flex-start;
    width: 600px;
}

.col {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    height: 200px;
}

.rev span { width: 120px; }
.col span { height: 40px; }

Expected output

row-reverse with flex-start (600px wide):
|            [ C  ][ B  ][ A  ]|

column with space-between (200px tall):
A is at the top edge.
B is centered between A and C (40px gaps).
C is at the bottom edge.

In row-reverse, flex-start packs the items against the right edge, because that is where the main axis starts. In the column, the free space is 200px minus 3 x 40px, which leaves 80px, shared as two 40px gaps.

6. Know how it works with wrapping

When items wrap onto several lines, each line shares its own free space. A line with fewer items has more free space, so it can look different from the line above it.

Example 5: space-between on wrapped lines

<!-- HTML -->
<div class="wrapped">
    <span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
</div>

/* CSS */
.wrapped {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    width: 600px;
}

.wrapped span {
    width: 160px;
    padding: 10px;
    box-sizing: border-box;
}

Expected output

Line 1 (3 items, 120px free):
|[  1   ]   [  2   ]   [  3   ]|

Line 2 (2 items, 280px free):
|[  4   ]              [  5   ]|

Each line shares its own free space.
Items 4 and 5 are pushed to opposite edges.

This is still correct behaviour, but it can surprise you. If the last line looks too spread out, use flex-start with a gap instead.

7. Combine it with gap

The gap property is subtracted before the free space is calculated, so it acts as a minimum space between items. justify-content then shares what is left.

Example 6: gap together with space-evenly

.toolbar {
    display: flex;
    justify-content: space-evenly;
    gap: 20px;
    width: 600px;
}

.btn {
    width: 120px;
    box-sizing: border-box;
}

Expected output

Used space:     3 x 120px + 2 x 20px gap = 400px
Free space:     600px - 400px = 200px
space-evenly:   200px / 4 = 50px added at each of 4 positions

Left edge:      50px
Between items:  20px gap + 50px = 70px
Right edge:     50px

S5. Architecture and Real-World Trade-Offs

The alignment rule is set per container, so each part of a page can share its free space in its own way. This is useful in navigation bars, card footers, hero sections and toolbars.

How the design fits a website

Imagine a website with a navigation bar, a card footer and a hero section. Each one has its own parent with its own alignment rule.

A focused alignment flow

Each website area shares its free space in the way that suits its content.

🧭Navigation barLogo at the start, links at the end
🃏Card footerButtons pushed to the end
🎯Hero sectionContent in the middle
 
 
 
.navjustify-content: space-between
.card-footerjustify-content: flex-end
.herojustify-content: center

The animation traces each website area to its own justify-content value.

Benefits for larger websites

  • Maintainability: The alignment rule lives on the parent, so changes happen in one place.
  • Adaptive spacing: The free space is measured again whenever the container width changes.
  • Less markup: You do not need spacer elements or hand-made margins.
  • Clear ownership: The container decides how its items share the space.
  • Direction aware: The same property works for rows, columns and reversed directions.

Important trade-offs

  • It has no visible effect when there is no free space, for example when the items fill the container or overflow it.
  • Items that grow with flex-grow use up the free space, so justify-content has nothing left to share.
  • Auto margins on an item take the free space before justify-content does.
  • In a column, the container needs extra height, or there is no free space to share.
  • On wrapped containers, each line shares its own space, so a short last line can look uneven.

Important: justify-content aligns items on the main axis only. It does not control how items sit on the cross axis. That is the job of another property, which is covered in the next article.

S6. Common Mistakes and Best Practices

Mistake 1: Using justify-content without display: flex

Wrong: Add justify-content: space-between to a normal block element.

Why it fails: The property only affects flex containers (and grid containers), so on a normal block it does nothing.

Better: Add display: flex to the parent first.

Rule: Create the container before using any flex property.

Mistake 2: Expecting an effect when there is no free space

Wrong: Use justify-content: center on a container whose items already fill its full width.

Why it fails: With no free space, there is nothing to distribute, so the layout does not change.

Better: Check the item widths. Remove flex-grow or a full-width setting if you want the space shared by justify-content.

Rule: Free space must exist before it can be shared.

Mistake 3: Assuming justify-content is always horizontal

Wrong: Keep using justify-content to center items horizontally after switching to flex-direction: column.

Why it fails: In a column, the main axis is vertical, so the property now works from top to bottom.

Better: Think in main axis and cross axis, not horizontal and vertical.

Rule: justify-content follows the main axis, whichever way it points.

Mistake 4: Putting justify-content on the items

Wrong: Add justify-content: flex-end to a button to push it to the right.

Why it fails: The property belongs on the container. On a button, it only affects the button's own children, and only if the button is itself a flex container.

Better: Set justify-content on the parent that holds the buttons.

Rule: The container shares the space. The items do not.

Mistake 5: Expecting space-around to give equal edge space

Wrong: Use space-around and expect the space at the edges to match the space between the items.

Why it fails: Each item gets equal space on both sides, so the space between two items is double the space at an edge.

Better: Use space-evenly when every gap should be the same.

Rule: Choose the value by how the edges should look.

S7. How This Comes Up in Interviews

Question 1: What does justify-content do, and on which axis?

What the interviewer wants: They want to know whether you understand the main axis.

Strong answer:

"It shares the free space along the main axis of a flex container. In a row, that is horizontal, and in a column, it is vertical, because the main axis comes from flex-direction."

Question 2: What is the difference between space-between, space-around and space-evenly?

Strong answer:

"With space-between, the first and last items touch the edges and the gaps between items are equal. With space-around, each item has equal space on both sides, so the edge space is half of the gap between items. With space-evenly, the gaps between items and at the edges are all the same."

Question 3: Why does justify-content sometimes have no effect?

Strong answer:

"There are a few common reasons. The parent may not be a flex container, there may be no free space because the items fill the line, or items with flex-grow may be using up the free space. Auto margins also take free space before justify-content does."

Question 4: How do flex-direction and wrapping affect justify-content?

Strong answer:

"flex-direction decides which axis is the main axis and where it starts, so flex-start is on the right in row-reverse and at the top in a column. When items wrap, each flex line shares its own free space separately."

What should different experience levels explain?

  • Junior developer: Explain justify-content and show center and space-between on a row.
  • Senior developer: Explain free space, the difference between the three space-* values, and why direction, wrapping and flex-grow change the result.
  • Technical architect: Explain how alignment conventions keep toolbars, navigation and cards consistent across a design system.

S8. Hands-On Practice

Beginner: Align a toolbar

Create a 600px flex container with three 120px buttons. Try flex-start, center and flex-end one at a time and observe the result.

Expected result: The three buttons sit at the start, in the middle and at the end of the toolbar.

Intermediate: Compare all six values

Create six containers with the same three children. Give each one a different justify-content value. Work out the space between the items and at the edges by hand, then check it in your browser tools.

Hint: Free space = container width minus 3 x item width. Compare space-around with space-evenly.

Advanced: Combine alignment with direction, wrapping and gap

Build a navigation bar with space-between. Then build a wrapped container of five 160px items and observe how each line shares its space. Finally, build a 200px-tall column with space-between and add a gap to the toolbar with space-evenly.

Expected result: The navigation bar spreads its items. Each wrapped line shares its own free space. The column spreads its items from top to bottom, and the gap acts as a minimum space.

S9. Next Steps and Community

Previous article: Flex Wrap and Flex Flow – Managing Multiple Rows.

Next article: Align Items and Align Content – Cross Axis Alignment.

Continue the series by learning how align-items positions items along the cross axis, and how align-content shares the space between multiple flex lines.

Try the examples in your own HTML and CSS project. Change the container width, switch between all six values, try them with different directions, and share your layouts with the Upgradix community. Help other developers understand Flexbox through practical examples.

Find the free space first. Then share it.

Upgradix by Shrash Studio
Learn Technology. Build Your Future.
Quality Technology Education. Completely Free.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.