Justify Content & Align Items
Tailwind CSS gives you utility classes to control alignment of flex items along both the main axis and the cross axis.
1οΈβ£ Justify Content
Controls alignment along the main axis (row or column direction). Use these classes on the flex container:
justify-start
: align items at the startjustify-center
: center itemsjustify-end
: align at the endjustify-between
: even spacing between itemsjustify-around
: equal space around itemsjustify-evenly
: equal space between and around
<div class="flex justify-center space-x-2">
<div class="bg-blue-500 text-white p-4">1</div>
<div class="bg-green-500 text-white p-4">2</div>
<div class="bg-red-500 text-white p-4">3</div>
</div>
1
2
3
2οΈβ£ Align Items
Controls alignment on the cross axis (perpendicular to main axis). These classes are also applied on the flex container:
items-start
: align items to top/leftitems-center
: center itemsitems-end
: align to bottom/rightitems-stretch
: stretch to fill (default)items-baseline
: align text baselines
<div class="flex items-end space-x-2 h-32 bg-gray-100">
<div class="bg-blue-500 text-white p-2 h-12">Short</div>
<div class="bg-green-500 text-white p-2 h-20">Tall</div>
</div>
Short
Tall
3οΈβ£ Align Self (on item)
Use self-start
, self-end
, self-center
etc. on individual flex items to override the containerβs alignment.
<div class="flex items-center h-32 bg-gray-100 space-x-2">
<div class="self-start bg-red-500 text-white p-2">Top</div>
<div class="bg-blue-500 text-white p-2">Center</div>
<div class="self-end bg-green-500 text-white p-2">Bottom</div>
</div>
Top
Center
Bottom
π§ Summary
Use justify-* for horizontal alignment and items-* (or self-*) for vertical alignment. Flexbox makes it simple to center, distribute, or stretch elements!