CSS justify-content: compare alignment and spacing in Flexbox
Flexbox justify-content uses free space on the main axis to position a group or distribute space between and around items. In ordinary horizontal writing the main axis is horizontal for row and vertical for column, so check flex-direction first.
Distinguish alignment from space distribution
| Value | Layout with available space |
|---|---|
flex-start |
Group at the main-axis start |
flex-end |
Group at the main-axis end |
center |
Center the group |
space-between |
Distribute space only between the items |
space-around |
Edge spaces are half the inner spaces (without gap) |
space-evenly |
Give the edges and inner spaces equal room |
flex-start points toward the start of the main axis, which is on the right for row-reverse in left-to-right horizontal writing. center centers the group. Measure outer spaces from the container’s content area inside its padding. See CSS Flexbox · justify-content.
Run the six-layout comparison
Save the code as justify-content.html and open it. The example compares six layouts using three 40px items. flex: none keeps the items from growing or shrinking. No fixed gap is set.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare main-axis alignment and spacing</title>
<style>
body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
* { box-sizing: border-box; }
h1 { font-size: 24px; }
h2 { margin: 14px 0 6px; font-size: 16px; }
.samples { max-width: 560px; }
.box { display: flex; height: 56px; padding: 8px;
border: 1px solid #84a3b7; background: #edf4f8; }
.box > span { display: grid; place-items: center; flex: none;
width: 40px; height: 32px; color: white; background: #176b86; }
.flex-start { justify-content: flex-start; }
.flex-end { justify-content: flex-end; }
.center { justify-content: center; }
.space-between { justify-content: space-between; }
.space-around { justify-content: space-around; }
.space-evenly { justify-content: space-evenly; }
</style>
</head>
<body>
<h1>Compare justify-content values</h1>
<div class="samples">
<h2>flex-start</h2>
<div class="box flex-start"><span>A</span><span>B</span><span>C</span></div>
<h2>flex-end</h2>
<div class="box flex-end"><span>A</span><span>B</span><span>C</span></div>
<h2>center</h2>
<div class="box center"><span>A</span><span>B</span><span>C</span></div>
<h2>space-between</h2>
<div class="box space-between"><span>A</span><span>B</span><span>C</span></div>
<h2>space-around</h2>
<div class="box space-around"><span>A</span><span>B</span><span>C</span></div>
<h2>space-evenly</h2>
<div class="box space-evenly"><span>A</span><span>B</span><span>C</span></div>
</div>
</body>
</html>
With a 360px content width, the items use 120px and leave 240px. space-between creates two inner spaces of 120px. space-around gives 40px at each edge and 80px between items. space-evenly gives all four spaces 60px. To reproduce the numbers, use a wide window and temporarily set width: 378px on .box. The 8px padding and 1px border on both sides leave a 360px content width.
Keep only the first item in each box. space-between puts it at the start, while space-around and space-evenly center it. Restore the three items and the original width after comparing.
See how gap, auto margins, and flex-grow use space
Add gap: 12px to .box. The two fixed gaps use 24px first. At a 360px content width, space-around distributes the remaining 216px, creating 36px at the edges and 84px between items, including the 12px gap. See CSS Box Alignment · Gaps. Remove the declaration afterward.
Change flex: none to flex: 1 and the items in this example use all the spare width, so the alignment difference disappears. Restore it and try margin-inline-start: auto on the last item. In a horizontal row, that margin takes up the free space. See CSS Flexbox · Auto Margins for the order in which margins are resolved.
Center vertically and diagnose missing alignment
Add the rules below at the end of the stylesheet. The .center example now uses justify-content: center vertically. At 240px outer height, the content height is 222px. Three 32px items leave 63px of free space above and below. align-items now handles horizontal alignment.
.center {
flex-direction: column;
height: 240px;
align-items: flex-start;
}
If alignment makes no visible difference, check whether there is free space on the main axis and whether flex-grow or an auto margin already takes it. With multiple flex lines, justify-content works on each line. align-content aligns the lines on the cross axis. See the flex-direction comparison to check the direction of the axes.




