justify-content vs align-items in CSS
19 The two axes, and how they swap
fairAlign items along the main axis and across the cross axis, then flip the direction to column and predict which property now controls which direction.
Stuck? Three hints, smallest first
- 1.
justify-contentworks along the main axis,align-itemsacross the cross axis. Neither is tied to a screen direction. - 2. With
rowthe main axis is horizontal. Withcolumnit is vertical, and the two properties swap what they appear to do. - 3. That is why remembering them as “horizontal” and “vertical” breaks the first time you change direction.
<div class="frame as-row">
<div class="box">A</div>
<div class="box">B</div>
</div>
<div class="frame as-col">
<div class="box">A</div>
<div class="box">B</div>
</div>
.frame {
display: flex;
height: 110px;
margin-bottom: 10px;
border: 2px dashed #3055d8;
border-radius: 8px;
justify-content: space-between; /* along the main axis */
align-items: center; /* across the cross axis */
}
/* Main axis horizontal: space-between spreads left to right, centre is vertical. */
.as-row { flex-direction: row; }
/* Main axis vertical: the very same two declarations now spread top to bottom
and centre horizontally. */
.as-col { flex-direction: column; }
.box {
padding: 8px 14px;
background: #3055d8;
color: #ffffff;
border-radius: 6px;
font-size: 0.85rem;
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
// Which screen direction each property controls, given a direction.
const axes = (dir) =>
dir.startsWith('row')
? { justifyContent: 'horizontal', alignItems: 'vertical' }
: { justifyContent: 'vertical', alignItems: 'horizontal' };
test('in a row, justify-content spreads horizontally', axes('row').justifyContent, 'horizontal');
test('and align-items centres vertically', axes('row').alignItems, 'vertical');
test('in a column the pair swap over', axes('column'), { justifyContent: 'vertical', alignItems: 'horizontal' });
test('so neither property is tied to a screen direction', axes('row').justifyContent === axes('column').justifyContent, false);
test('one shared rule declares both alignments', /justify-content:\s*space-between/.test(css) && /align-items:\s*center/.test(css), true);
test('only the direction differs between the two frames', /\.as-row\s*\{\s*flex-direction:\s*row/.test(css), true);
test('and the second is a column', /\.as-col\s*\{\s*flex-direction:\s*column/.test(css), true);
test('two boxes in each frame', countTag(html, 'div'), 6);
Output
Both frames share one alignment rule and differ only in flex-direction, which is what makes the point visible: the same two declarations produce a horizontal spread in the first and a vertical one in the second. Learn them as main and cross rather than horizontal and vertical, or every switch to column feels like the properties broke. The same holds for row-reverse, where the main axis still runs horizontally but starts from the other end.