Flexbox Utilities: Container and Item Properties
What you'll learn: How to build flexible layouts using Tailwind's flexbox utilities for both containers and their child items.
Understanding Flexbox in Tailwind
Tailwind provides utility classes that map directly to CSS flexbox properties, letting you create responsive layouts without writing custom CSS. Think of flexbox like arranging books on a shelf—you control both how the shelf behaves (the container) and how each book fits (the items).
Container Properties
Start by making an element a flex container with the flex class. Then control its direction:
flex-row– items flow horizontally (default)flex-col– items stack vertically
Control how items are distributed along the main axis with justify-*:
justify-start,justify-center,justify-endjustify-between– space between itemsjustify-around– space around items
Control cross-axis alignment with items-*:
items-start,items-center,items-enditems-stretch– items fill the container height
Add spacing between items with gap-* utilities: gap-4, gap-8, etc.
Item Properties
Individual flex items can control their own behavior:
flex-grow– item expands to fill available spaceflex-shrink– item shrinks when space is tightflex-1– shorthand for grow and shrink equally
These properties give you fine-grained control without leaving your HTML.
Example Pattern
A typical navigation bar might use: <nav class="flex justify-between items-center gap-4"> to space links evenly while keeping them vertically centered with consistent gaps.
Key Takeaway: Tailwind's flexbox utilities let you build complex, responsive layouts by combining container properties (flex, flex-row/col, justify-*, items-*, gap-*) with item properties (flex-grow, flex-shrink, flex-1) directly in your markup.