Lesson 22 +10 XP

Flex Alignment & Spacing

Flex Alignment & Spacing

Position flex items along both axes and distribute free space.

Justify content (main axis)

ClassCSS
justify-startstart
justify-endend
justify-centercenter
justify-betweenspace-between
justify-aroundspace-around
justify-evenlyspace-evenly
<div class="flex justify-between">
  <span>Left</span>
  <span>Right</span>
</div>

Align items (cross axis)

ClassCSS
items-startstart
items-endend
items-centercenter
items-baselinebaseline
items-stretchstretch (default)

Perfect centering

flex items-center justify-center centers content both ways:

<div class="flex h-32 items-center justify-center">Centered</div>

Gap

Space between items without relying on margins:

<div class="flex gap-4">
  <div>One</div>
  <div>Two</div>
</div>
  • gap-<n> - both axes.
  • gap-x-<n> - horizontal only.
  • gap-y-<n> - vertical only.

TL;DR

  • justify-* works along the main axis.
  • items-* works along the cross axis.
  • items-center justify-center centers perfectly.
  • gap-* spaces items out cleanly.