All Posts

How to use flexbox in Tailwind CSS

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.

Setting up flex containers and direction

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 bottom
  • flex-row-reverse - Items flow right to left
  • flex-col-reverse - Items stack bottom to top

Here's a simple horizontal layout:

```html
Item 1
Item 2
Item 3
```

And here's the same layout but vertical:

```html
Item 1
Item 2
Item 3
```

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.

Aligning and positioning flex items

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 start
  • justify-center - Center items
  • justify-end - Pack items to the end
  • justify-between - Space items evenly with no space at edges
  • justify-around - Space items with half-size space at edges
  • justify-evenly - Space items with equal space everywhere

Here's centering done right:

```html
Perfectly Centered
```

Align items utilities (cross axis positioning):

  • items-start - Align to start of cross axis
  • items-center - Center along cross axis
  • items-end - Align to end of cross axis
  • items-stretch - Stretch to fill container (default)
  • items-baseline - Align text baselines

Here's a practical example showing different alignment options:

```html
Navigation
```

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.

Controlling how flex items grow and shrink

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 size
  • flex-auto - Grows and shrinks based on content size
  • flex-initial - Shrinks but doesn't grow (default behavior)
  • flex-none - Doesn't grow or shrink at all

The 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:

```html
Sidebar
Fixed width sidebar
Main Content
This area grows to fill remaining space
```

You can also use multiple flex-1 items to create equal-width columns:

```html
Column 1
Column 2
Column 3
```

Each column automatically takes up an equal share of the available space, no matter the screen size.

Managing wrapping and spacing

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 lines
  • flex-wrap-reverse - Wrap in reverse order

Here's a tag list that wraps when it needs to:

```html
React TypeScript Tailwind CSS Next.js Node.js PostgreSQL
```

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:

```html
```

The gap utility is way cleaner than adding margins to every item, and it automatically handles edge cases.

Common layout patterns

Now let's put everything together with some real-world patterns you'll use all the time.

Card grid that adapts to screen size:

```html

Feature One

Description of the first feature goes here.

Feature Two

Description of the second feature goes here.

Feature Three

Description of the third feature goes here.

```

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:

```html
Header

Main Content

This area grows to fill available space.

© 2025 Your Company
```

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:

```html
Profile

Sarah Johnson

Product Designer

```

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!

Other posts you might like...

See more

Subscribe to our newsletter

Get notified when we add new templates, components, and more!

Thanks for subscribing!
Please check your email to confirm your subscription.

WindyBase Make development a breeze

WindyBase is a weekly curated Tailwind CSS template and tool directory built for the modern developer.

© 2025 WindyBase. All rights reserved.