Emmanuel Akinfulubi

HTML and CSSPractical II

justify-content vs align-items in CSS

19 The two axes, and how they swap

fair

Align 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. 1. justify-content works along the main axis, align-items across the cross axis. Neither is tied to a screen direction.
  2. 2. With row the main axis is horizontal. With column it is vertical, and the two properties swap what they appear to do.
  3. 3. That is why remembering them as “horizontal” and “vertical” breaks the first time you change direction.

index.html

<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>

style.css

.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;
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
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.

Checks for exercise 19

// 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.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.