Flexbox provides powerful alignment options along both the main and cross axes.
Main Axis Alignment
Control distribution along the main axis with justify-*
utilities:
justify-start
- Pack items at start (default)justify-end
- Pack items at endjustify-center
- Center itemsjustify-between
- Equal space between itemsjustify-around
- Equal space around itemsjustify-evenly
- Equal space between items and edges
Page 1 of 10
<!-- Center navigation items -->
<nav class="flex justify-center gap-4">
<a href="#" class="rounded-lg bg-blue-500 px-4 py-2 text-white hover:bg-blue-600">Home</a>
<a href="#" class="rounded-lg bg-blue-500 px-4 py-2 text-white hover:bg-blue-600">About</a>
<a href="#" class="rounded-lg bg-blue-500 px-4 py-2 text-white hover:bg-blue-600">Contact</a>
</nav>
<!-- Space between items -->
<div class="flex justify-between rounded-lg bg-gray-100 p-4">
<button class="rounded bg-gray-500 px-4 py-2 text-white hover:bg-gray-600">Previous</button>
<span class="self-center font-medium text-gray-700">Page 1 of 10</span>
<button class="rounded bg-gray-500 px-4 py-2 text-white hover:bg-gray-600">Next</button>
</div>
Cross Axis Alignment
Align items perpendicular to the main axis:
items-start
- Align to start of cross axisitems-end
- Align to end of cross axisitems-center
- Center on cross axisitems-baseline
- Align text baselinesitems-stretch
- Stretch to fill (default)
Centered content
<!-- Vertically center items -->
<div class="flex h-20 items-center gap-3 rounded-lg bg-gray-100 px-4">
<div class="h-8 w-8 flex-shrink-0 rounded-full bg-blue-500"></div>
<span class="font-medium text-gray-700">Centered content</span>
</div>
Individual Alignment
Override container alignment for specific items:
self-auto
- Use container’s alignmentself-start
- Align item to startself-end
- Align item to endself-center
- Center itemself-stretch
- Stretch item
Top aligned
Centered
Bottom aligned
<div class="flex h-32 items-start gap-4 rounded-lg bg-gray-100 p-4">
<div class="rounded bg-red-500 p-3 text-white">Top aligned</div>
<div class="self-center rounded bg-green-500 p-3 text-white">Centered</div>
<div class="self-end rounded bg-blue-500 p-3 text-white">Bottom aligned</div>
</div>
Multi-line Alignment
For wrapped flex items, align entire lines:
content-start
- Pack lines at startcontent-end
- Pack lines at endcontent-center
- Center linescontent-between
- Space between linescontent-around
- Space around linescontent-evenly
- Even spacing
Item 1
Item 2
Item 3
Item 4
Item 5
<div class="flex h-64 flex-wrap content-center gap-4 rounded-lg bg-gray-100 p-4">
<div class="h-20 w-32 rounded bg-purple-500 p-4 text-white">Item 1</div>
<div class="h-20 w-32 rounded bg-pink-500 p-4 text-white">Item 2</div>
<div class="h-20 w-32 rounded bg-indigo-500 p-4 text-white">Item 3</div>
<div class="h-20 w-32 rounded bg-blue-500 p-4 text-white">Item 4</div>
<div class="h-20 w-32 rounded bg-green-500 p-4 text-white">Item 5</div>
</div>