Flexbox changed how we build layouts on the web. Before flexbox, creating responsive layouts meant dealing with floats, clearfix hacks, and way too much custom CSS.
With Tailwind CSS, flexbox becomes even more straightforward. You get utility classes that map directly to CSS flexbox properties, so you can build layouts fast without switching between your HTML and CSS files.
Whether you're building a navigation bar, a card grid, or centering content (yes, centering things is finally easy), flexbox utilities in Tailwind have you covered.
Let me show you exactly how to use them.
To use flexbox in Tailwind, start by adding the flex class to your container. This turns on flexbox for all the direct children inside that container.
By default, flex arranges items horizontally in a row. But you can control the direction with these utilities:
flex-row - Items flow left to right (the default)flex-col - Items stack vertically top to bottomflex-row-reverse - Items flow right to leftflex-col-reverse - Items stack bottom to topHere's a simple horizontal layout:
<div class='flex flex-row gap-4'>
<div class='bg-blue-500 text-white px-6 py-4 rounded-lg'>Item 1</div>
<div class='bg-blue-500 text-white px-6 py-4 rounded-lg'>Item 2</div>
<div class='bg-blue-500 text-white px-6 py-4 rounded-lg'>Item 3</div>
</div>And here's the same layout but vertical:
<div class='flex flex-col gap-4'>
<div class='bg-green-500 text-white px-6 py-4 rounded-lg'>Item 1</div>
<div class='bg-green-500 text-white px-6 py-4 rounded-lg'>Item 2</div>
<div class='bg-green-500 text-white px-6 py-4 rounded-lg'>Item 3</div>
</div>The reverse variants are useful when you need to flip the order without changing your HTML. This comes in handy for responsive designs where you want content to appear in different orders on mobile vs desktop.
This is where flexbox really shines. You get two main ways to position items - along the main axis with justify-* utilities, and along the cross axis with items-* utilities.
The main axis is the direction your items flow (horizontal for flex-row, vertical for flex-col). The cross axis is perpendicular to that.
Justify content utilities (main axis positioning):
justify-start - Pack items to the startjustify-center - Center itemsjustify-end - Pack items to the endjustify-between - Space items evenly with no space at edgesjustify-around - Space items with half-size space at edgesjustify-evenly - Space items with equal space everywhereHere's centering done right:
<div class='flex justify-center items-center h-64 bg-gray-100 rounded-lg'>
<div class='bg-purple-500 text-white px-8 py-4 rounded-lg text-xl font-semibold'>
Perfectly Centered
</div>
</div>Align items utilities (cross axis positioning):
items-start - Align to start of cross axisitems-center - Center along cross axisitems-end - Align to end of cross axisitems-stretch - Stretch to fill container (default)items-baseline - Align text baselinesHere's a practical example showing different alignment options:
<div class='flex justify-between items-center p-6 bg-white rounded-lg shadow-md'>
<div class='text-lg font-semibold text-gray-800'>Navigation</div>
<div class='flex gap-6'>
<a href='#' class='text-gray-600 hover:text-gray-900'>Home</a>
<a href='#' class='text-gray-600 hover:text-gray-900'>About</a>
<a href='#' class='text-gray-600 hover:text-gray-900'>Contact</a>
</div>
<button class='bg-blue-500 text-white px-4 py-2 rounded-lg hover:bg-blue-600'>Sign Up</button>
</div>The combination of justify-between and items-center creates that classic navbar layout where the logo is on the left, navigation in the middle, and action button on the right - all vertically centered.
Sometimes you want certain flex items to take up more space than others. That's where the flex sizing utilities come in.
Here's what each one does:
flex-1 - Grows and shrinks equally, ignoring initial sizeflex-auto - Grows and shrinks based on content sizeflex-initial - Shrinks but doesn't grow (default behavior)flex-none - Doesn't grow or shrink at allThe most common one you'll use is flex-1. It tells an item to take up all available space.
Here's a classic sidebar layout where the main content area grows to fill space:
<div class='flex gap-4 h-64'>
<div class='flex-none w-48 bg-gray-800 text-white p-4 rounded-lg'>
<div class='font-semibold mb-4'>Sidebar</div>
<div class='text-sm text-gray-300'>Fixed width sidebar</div>
</div>
<div class='flex-1 bg-blue-50 p-6 rounded-lg'>
<div class='font-semibold text-gray-800 mb-2'>Main Content</div>
<div class='text-gray-600'>This area grows to fill remaining space</div>
</div>
</div>You can also use multiple flex-1 items to create equal-width columns:
<div class='flex gap-4'>
<div class='flex-1 bg-red-100 p-6 rounded-lg text-center'>
<div class='font-semibold text-red-800'>Column 1</div>
</div>
<div class='flex-1 bg-green-100 p-6 rounded-lg text-center'>
<div class='font-semibold text-green-800'>Column 2</div>
</div>
<div class='flex-1 bg-blue-100 p-6 rounded-lg text-center'>
<div class='font-semibold text-blue-800'>Column 3</div>
</div>
</div>Each column automatically takes up an equal share of the available space, no matter the screen size.
By default, flex items try to fit on one line. But sometimes you want them to wrap to multiple lines when space runs out.
That's where flex-wrap comes in:
flex-nowrap - Keep everything on one line (default)flex-wrap - Let items wrap to new linesflex-wrap-reverse - Wrap in reverse orderHere's a tag list that wraps when it needs to:
<div class='flex flex-wrap gap-3'>
<span class='bg-gray-200 text-gray-800 px-4 py-2 rounded-full text-sm'>React</span>
<span class='bg-gray-200 text-gray-800 px-4 py-2 rounded-full text-sm'>TypeScript</span>
<span class='bg-gray-200 text-gray-800 px-4 py-2 rounded-full text-sm'>Tailwind CSS</span>
<span class='bg-gray-200 text-gray-800 px-4 py-2 rounded-full text-sm'>Next.js</span>
<span class='bg-gray-200 text-gray-800 px-4 py-2 rounded-full text-sm'>Node.js</span>
<span class='bg-gray-200 text-gray-800 px-4 py-2 rounded-full text-sm'>PostgreSQL</span>
<span class='bg-gray-200 text-gray-800 px-4 py-2 rounded-full text-sm'>GraphQL</span>
<span class='bg-gray-200 text-gray-800 px-4 py-2 rounded-full text-sm'>MongoDB</span>
</div>Now for spacing. The gap utility is your best friend here. It adds space between flex items without needing margins on each item.
Use gap-* for equal spacing, or gap-x-* and gap-y-* for different horizontal and vertical spacing:
<div class='flex flex-wrap gap-x-6 gap-y-4'>
<div class='w-32 h-32 bg-gradient-to-br from-pink-400 to-purple-500 rounded-lg'></div>
<div class='w-32 h-32 bg-gradient-to-br from-blue-400 to-cyan-500 rounded-lg'></div>
<div class='w-32 h-32 bg-gradient-to-br from-green-400 to-emerald-500 rounded-lg'></div>
<div class='w-32 h-32 bg-gradient-to-br from-yellow-400 to-orange-500 rounded-lg'></div>
<div class='w-32 h-32 bg-gradient-to-br from-red-400 to-pink-500 rounded-lg'></div>
<div class='w-32 h-32 bg-gradient-to-br from-indigo-400 to-blue-500 rounded-lg'></div>
</div>The gap utility is way cleaner than adding margins to every item, and it automatically handles edge cases.
Now let's put everything together with some real-world patterns you'll use all the time.
Card grid that adapts to screen size:
<div class='flex flex-wrap gap-6'>
<div class='flex-1 min-w-64 bg-white p-6 rounded-lg shadow-md'>
<div class='size-12 bg-blue-500 rounded-lg mb-4'></div>
<h3 class='font-semibold text-gray-800 mb-2'>Feature One</h3>
<p class='text-gray-600 text-sm'>Description of the first feature goes here with some additional content to show how cards handle different text lengths.</p>
</div>
<div class='flex-1 min-w-64 bg-white p-6 rounded-lg shadow-md'>
<div class='size-12 bg-green-500 rounded-lg mb-4'></div>
<h3 class='font-semibold text-gray-800 mb-2'>Feature Two</h3>
<p class='text-gray-600 text-sm'>Description of the second feature goes here.</p>
</div>
<div class='flex-1 min-w-64 bg-white p-6 rounded-lg shadow-md'>
<div class='size-12 bg-purple-500 rounded-lg mb-4'></div>
<h3 class='font-semibold text-gray-800 mb-2'>Feature Three</h3>
<p class='text-gray-600 text-sm'>Description of the third feature goes here.</p>
</div>
</div>The cards grow to fill space but wrap to new rows when needed. The min-w-64 prevents them from getting too narrow.
Sticky footer layout:
<div class='flex flex-col h-96'>
<header class='flex-none bg-gray-800 text-white p-4'>
<div class='text-xl font-semibold'>Header</div>
</header>
<main class='flex-1 p-8 bg-gray-50 overflow-auto'>
<h1 class='text-2xl font-bold text-gray-800 mb-4'>Main Content</h1>
<p class='text-gray-600 mb-4'>This area grows to fill available space, pushing the footer to the bottom.</p>
<p class='text-gray-600'>Even with minimal content, the footer stays at the bottom of the viewport.</p>
</main>
<footer class='flex-none bg-gray-800 text-white p-4 text-center text-sm'>
© 2025 Your Company
</footer>
</div>The footer stays at the bottom no matter how much content you have. The main content area uses flex-1 to fill the space.
Profile card with mixed alignment:
<div class='flex items-center gap-4 bg-white p-6 rounded-lg shadow-md max-w-md'>
<div class='size-16 rounded-full bg-gradient-to-br from-blue-400 to-purple-500 flex-shrink-0'></div>
<div class='flex-1'>
<h3 class='font-semibold text-gray-800'>Sarah Johnson</h3>
<p class='text-gray-600 text-sm'>Product Designer</p>
</div>
<button class='flex-none bg-blue-500 text-white px-4 py-2 rounded-lg text-sm hover:bg-blue-600'>
Follow
</button>
</div>This combines everything - fixed-size avatar, flexible text area, and a button that stays put.
That's flexbox in Tailwind CSS. With just a handful of utility classes, you can build almost any layout you need. The best part is how readable your code stays - you can see exactly what your layout does just by looking at the class names.
Start with flex, add direction with flex-row or flex-col, position with justify-* and items-*, and control sizing with flex-1. Mix in some gap for spacing and flex-wrap when needed, and you're set.
Go build something awesome!
Get notified when we add new templates, components, and more!
WindyBase Make development a breezeWindyBase is a weekly curated Tailwind CSS template and tool directory built for the modern developer.
© 2026 WindyBase. All rights reserved.