Master the power of flexible layouts with Tailwind's utility-first flexbox approach
Flexbox in Tailwind CSS provides a powerful system for creating dynamic layouts that adapt to different screen sizes. It offers intuitive ways to align, distribute, and order elements without complex CSS.
Tailwind's flexbox utilities make it easy to build everything from simple centered components to sophisticated responsive page layouts with just a few classes.
flex
Display as flex container
flex-{direction}
Set flex direction
justify-{position}
Horizontal alignment
Start by adding flex to create a flex container. This enables all flexbox features for that element's children.
flex gap-3Control the direction of flex items with flex-{direction} utilities.
flex flex-col gap-3Align items horizontally with justify-{position} utilities.
justify-betweenAlign items vertically with items-{position} utilities.
items-centerControl how flex items grow and shrink with flex-{value} utilities.
Control how items are positioned along the main axis:
justify-start
Items aligned to the start
justify-end
Items aligned to the end
justify-center
Items centered
justify-between
Space between items
justify-around
Equal space around items
justify-evenly
Equal space between items
justify-start justify-content: flex-start; justify-end justify-content: flex-end; justify-center justify-content: center; justify-between justify-content: space-between; justify-around justify-content: space-around; justify-evenly justify-content: space-evenly;
Control how items are positioned along the cross axis:
items-start
Items aligned to the start of cross axis
items-end
Items aligned to the end of cross axis
items-center
Items centered on cross axis
items-baseline
Items aligned by their baselines
items-stretch
Items stretched to fill container height
items-start align-items: flex-start; items-end align-items: flex-end; items-center align-items: center; items-baseline align-items: baseline; items-stretch align-items: stretch;
Control whether flex items are forced onto a single line or can wrap onto multiple lines:
flex-wrap
Items wrap to next line when needed
flex-wrap-reverse
Items wrap in reverse direction
flex-nowrap
Items forced into a single line (may overflow)
flex-wrap flex-wrap: wrap; flex-wrap-reverse flex-wrap: wrap-reverse; flex-nowrap flex-wrap: nowrap;
Control how flex items grow and shrink:
flex-none
Prevent item from growing or shrinking (flex: none;)
flex-initial
Allow item to shrink but not grow (flex: 0 1 auto;)
flex-auto
Allow item to grow and shrink, considering initial size (flex: 1 1 auto;)
flex-1
Grow and shrink as needed, ignoring initial size (flex: 1 1 0%;)
flex-none flex: none; flex-initial flex: 0 1 auto; flex-auto flex: 1 1 auto; flex-1 flex: 1 1 0%;
When building responsive layouts, combine flex utilities with responsive prefixes like md:flex-row to create
layouts that change based on screen size. For example, flex flex-col md:flex-row creates a column layout on mobile
that becomes a row layout on medium-sized screens and larger.
Flex container with items centered and some individual item alignment:
<div class="flex flex-wrap h-72 w-100 bg-gray-100 justify-around items-center"> <div class="p-10 border border-blue-600 bg-blue-100">01</div> <div class="p-10 border border-blue-600 bg-blue-100">02</div> <div class="self-start p-10 border border-blue-600 bg-blue-100">03</div> <div class="self-end p-10 border border-blue-600 bg-blue-100">04</div> </div>
Vertical column with gap spacing and reordered items:
<div class="flex flex-col gap-4 w-100 bg-gray-200 justify-around items-center"> <div class="order-4 p-10 border border-blue-600 bg-blue-100">01</div> <div class="order-1 p-10 border border-blue-600 bg-blue-100">02</div> <div class="p-10 border border-blue-600 bg-blue-100">03</div> <div class="p-10 border border-blue-600 bg-blue-100">04</div> </div>
Demonstration of different flex item growth behaviors:
<div class="flex w-100 bg-gray-300">
<!-- flex-none: Prevent item from growing or shrinking -->
<div class="w-64 flex-none p-10 border border-blue-600 bg-blue-100">
01
</div>
<!-- flex-initial: Allow item to shrink but not grow -->
<div class="w-64 flex-initial p-10 border border-blue-600 bg-blue-100">
02
</div>
<!-- flex-auto: Allow item to grow and shrink, taking into account its initial size -->
<div class="w-64 flex-auto p-10 border border-blue-600 bg-blue-100">
03
</div>
<!-- flex-1: Allow item to grow and shrink as needed, ignoring its initial size -->
<div class="w-64 flex-1 p-10 border border-blue-600 bg-blue-100">04</div>
<div class="p-10 border border-blue-600 bg-blue-100">05</div>
<div class="p-10 border border-blue-600 bg-blue-100">06</div>
<div class="p-10 border border-blue-600 bg-blue-100">07</div>
</div>