| |

Tailwind CSS 13 🎨 Modern Flexbox Layouts (Direction, Wrap, Justify, Items, Self Alignment)

Flexbox is the layout model that made one-dimensional alignment practical. Before flexbox, centering an element vertically required hacks with tables, absolute positioning, or negative margins. With flexbox, a container declares its direction and alignment, and the children distribute themselves accordingly. Tailwind wraps every flexbox property in a utility, so the CSS becomes a set of class names that read like the intent: flex, justify-between, items-center, gap-4.

The utilities fall into two groups: container properties that control the layout of all children, and item properties that control individual children. Direction and wrap determine the axis and whether children flow onto multiple lines. Justify-content distributes children along the main axis. Align-items controls their position along the cross axis. Align-self overrides align-items for a single child. Gap adds space between children without margin. Grow and shrink determine how children absorb or yield space. Order changes their visual sequence.

This chapter covers the container properties, the item properties, the interaction between them, the v4 alias changes that shortened flex-grow and flex-shrink, and the patterns for building common layouts like navbars, card grids, and centered content.

Key point: flex establishes a flex container; flex-row and flex-col set the direction. justify-* distributes along the main axis; items-* aligns along the cross axis. gap-* spaces children without margin. grow-* and shrink-* control how children absorb or yield space. In v4, flex-grow-* and flex-shrink-* are aliased to grow-* and shrink-*.


Why flexbox utilities exist

The alignment problem. Centering, spacing, and distributing elements along an axis are among the most common layout requirements. Flexbox solves them with a few properties, and Tailwind exposes those properties as utilities so the layout is expressed in the markup.

The direction problem. A navbar arranges items horizontally; a sidebar arranges them vertically. The same children, different direction. flex-row and flex-col switch between them, and responsive variants let the direction change at breakpoints.

The distribution problem. Space between items can be distributed in several ways: packed at the start, packed at the end, centered, pushed apart, or spaced evenly. Each produces a different visual rhythm. justify-* covers all of them.

The sizing problem. Some children should be fixed; others should grow to fill available space; others should shrink when space is tight. grow-*, shrink-*, and flex-basis control this behavior, and flex-1 is the shorthand for “grow and shrink equally.”

The wrapping problem. A row of items may not fit on one line. flex-wrap allows them to flow onto multiple lines, and gap controls the spacing both horizontally and vertically. Without wrap, items overflow; with wrap, the layout adapts.


a. Establishing a flex container

The flex utility sets display: flex. It is the starting point for any flex layout.

<div class="flex">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>

The inline-flex variant sets display: inline-flex, making the container participate in inline flow while its children are laid out with flexbox.

<span class="inline-flex items-center gap-2">
  <svg>...</svg>
  <span>Label</span>
</span>

Without flex or inline-flex on the parent, the child utilities (justify-*, items-*, gap-*) have no effect, because they depend on the flex formatting context.


b. Flex direction

The flex-* direction utilities set flex-direction, which determines the main axis. The main axis is the direction along which children are laid out, and it is the axis that justify-* distributes along.

UtilityCSSLayout
flex-rowflex-direction: rowHorizontal (default)
flex-row-reverseflex-direction: row-reverseHorizontal, reversed
flex-colflex-direction: columnVertical
flex-col-reverseflex-direction: column-reverseVertical, reversed
<div class="flex flex-row">Horizontal items</div>
<div class="flex flex-col">Vertical items</div>
<div class="flex flex-row-reverse">Horizontal, reversed</div>

The reversed variants change the visual order of children without changing the DOM order. This matters for accessibility and for screen readers, which follow the DOM order, not the visual order. Use order-* utilities when the visual order should differ from the DOM order for a specific child.

Direction is often responsive. A layout that stacks vertically on mobile and lays out horizontally on desktop uses flex-col md:flex-row:

<div class="flex flex-col md:flex-row gap-4">
  <div>Sidebar</div>
  <div>Content</div>
</div>

c. Flex wrap

The flex-wrap utilities control whether children flow onto multiple lines when they do not fit on one.

UtilityCSSBehavior
flex-nowrapflex-wrap: nowrapNo wrapping (default)
flex-wrapflex-wrap: wrapWrap onto multiple lines
flex-wrap-reverseflex-wrap: wrap-reverseWrap in reverse direction
<div class="flex flex-wrap gap-4">
  <div class="w-32">Item</div>
  <div class="w-32">Item</div>
  <div class="w-32">Item</div>
  <div class="w-32">Item</div>
</div>

With flex-nowrap, children shrink to fit rather than wrapping. With flex-wrap, children maintain their size and flow onto the next line. The choice depends on whether the items should compress or reflow.

Wrapping introduces the cross axis as a second dimension for distribution. When children wrap onto multiple lines, align-content controls how the lines are distributed, and gap adds space both between items and between lines.


d. Justify content

The justify-* utilities distribute children along the main axis. They set justify-content.

UtilityCSSDistribution
justify-startjustify-content: flex-startPacked at start
justify-endjustify-content: flex-endPacked at end
justify-centerjustify-content: centerCentered
justify-betweenjustify-content: space-betweenFirst at start, last at end, equal between
justify-aroundjustify-content: space-aroundEqual space on each side of each item
justify-evenlyjustify-content: space-evenlyEqual space around every item
justify-stretchjustify-content: stretchAuto-sized items fill the axis
<div class="flex justify-between">
  <div>Left</div>
  <div>Center</div>
  <div>Right</div>
</div>

The difference between justify-around and justify-evenly is subtle but visible. With around, each item has equal space on each side, so the space between two items is double the space at the edges. With evenly, the space between items equals the space at the edges, producing a more uniform rhythm.

Tailwind v4 adds justify-center-safe and justify-end-safe, which fall back to the start alignment when there is not enough space. This prevents items from being cut off when the container is too small.

<div class="flex justify-center-safe">
  <div>Centered when space allows</div>
</div>

e. Align items

The items-* utilities align children along the cross axis. They set align-items.

UtilityCSSAlignment
items-startalign-items: flex-startStart of cross axis
items-endalign-items: flex-endEnd of cross axis
items-centeralign-items: centerCentered
items-baselinealign-items: baselineAligned to text baseline
items-stretchalign-items: stretchFill the cross axis (default)
<div class="flex items-center gap-4">
  <img src="avatar.jpg" class="h-12 w-12 rounded-full" />
  <div>Name and details</div>
</div>

The most common use of items-center is aligning an icon or avatar with adjacent text. Without it, the icon sits at the baseline of the text and looks misaligned.

items-stretch is the default, which is why a flex row with no height set makes all children the same height. This is useful for card rows where all cards should match the tallest one.

<div class="flex items-stretch gap-4">
  <div class="bg-white p-4">Card 1</div>
  <div class="bg-white p-4">Card 2 with more content</div>
</div>

items-baseline aligns text along the baseline, which is useful when children have different font sizes but should sit on the same line of text.


f. Align self

The self-* utilities override align-items for a single child. They set align-self.

UtilityCSS
self-autoalign-self: auto
self-startalign-self: flex-start
self-endalign-self: flex-end
self-centeralign-self: center
self-stretchalign-self: stretch
<div class="flex items-center gap-4">
  <div>Centered</div>
  <div class="self-start">Top-aligned</div>
  <div class="self-end">Bottom-aligned</div>
</div>

self-* is used when most children should align one way but a specific child needs a different alignment. A common case is a card in a row of cards that should stretch while the others are centered, or an action button that should sit at the bottom of a column while the content is centered.


g. Gap

The gap-* utilities add space between children without margin. They set gap, row-gap, or column-gap.

UtilityCSS
gap-4gap: 1rem
gap-x-4column-gap: 1rem
gap-y-4row-gap: 1rem
<div class="flex gap-4">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>

<div class="flex flex-col gap-y-2">
  <div>Item 1</div>
  <div>Item 2</div>
</div>

Gap is preferred over margin for spacing flex children because it does not add space at the edges. With margins, the first and last children have outer margins that affect the container; with gap, the spacing is strictly between children.

In Tailwind v4, gap is also the recommended replacement for the space-x and space-y utilities, which relied on child selectors and did not work with wrapping .


h. Grow and shrink

The grow-* and shrink-* utilities control how children absorb or yield space. They set flex-grow and flex-shrink. In v4, the older flex-grow-* and flex-shrink-* names are aliased to the shorter forms .

UtilityCSSBehavior
growflex-grow: 1Grow to fill available space
grow-0flex-grow: 0Do not grow
shrinkflex-shrink: 1Shrink when space is tight
shrink-0flex-shrink: 0Do not shrink
<div class="flex gap-4">
  <div class="grow">Fills available space</div>
  <div class="grow-0">Fixed width</div>
</div>

The flex-* utilities combine grow, shrink, and basis:

UtilityCSSEffect
flex-1flex: 1 1 0%Grow and shrink equally
flex-autoflex: 1 1 autoGrow and shrink from content size
flex-initialflex: 0 1 autoShrink, do not grow
flex-noneflex: noneNo grow, no shrink
<div class="flex">
  <div class="flex-none">Fixed</div>
  <div class="flex-1">Equal share</div>
  <div class="flex-1">Equal share</div>
</div>

flex-1 is the most common: it makes children share the available space equally. flex-none prevents a child from growing or shrinking, which is used for fixed-size elements like icons or buttons.

A critical detail: a flex child that should shrink below its content width needs min-w-0. Without it, the child’s automatic minimum size is its content size, and truncation does not work.

<div class="flex gap-2">
  <div class="min-w-0 flex-1 truncate">Long text that should truncate</div>
  <button class="shrink-0">Action</button>
</div>

Complete Example Session

<!-- ============================================
PART 1: BASIC FLEX ROW
============================================ -->
<div class="flex gap-4">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>
<!-- ============================================
PART 2: RESPONSIVE DIRECTION
============================================ -->
<div class="flex flex-col md:flex-row gap-4">
  <aside class="md:w-64">Sidebar</aside>
  <main class="flex-1">Content</main>
</div>
<!-- ============================================
PART 3: JUSTIFY BETWEEN
============================================ -->
<nav class="flex justify-between items-center p-4">
  <div class="font-bold">Logo</div>
  <div class="flex gap-4">
    <a href="#">Home</a>
    <a href="#">About</a>
  </div>
</nav>
<!-- ============================================
PART 4: CENTERING CONTENT
============================================ -->
<div class="flex items-center justify-center min-h-screen">
  <div class="text-center">
    <h1>Centered Content</h1>
  </div>
</div>
<!-- ============================================
PART 5: CARD ROW WITH STRETCH
============================================ -->
<div class="flex items-stretch gap-4">
  <div class="flex-1 bg-white p-4 rounded shadow">Card 1</div>
  <div class="flex-1 bg-white p-4 rounded shadow">Card 2</div>
  <div class="flex-1 bg-white p-4 rounded shadow">Card 3</div>
</div>
<!-- ============================================
PART 6: SELF ALIGNMENT
============================================ -->
<div class="flex items-center gap-4 h-32 bg-gray-100">
  <div class="self-start">Top</div>
  <div>Centered</div>
  <div class="self-end">Bottom</div>
</div>
<!-- ============================================
PART 7: WRAPPING ITEMS
============================================ -->
<div class="flex flex-wrap gap-2">
  <span class="px-3 py-1 bg-blue-100 rounded-full">Tag 1</span>
  <span class="px-3 py-1 bg-blue-100 rounded-full">Tag 2</span>
  <span class="px-3 py-1 bg-blue-100 rounded-full">Tag 3</span>
  <span class="px-3 py-1 bg-blue-100 rounded-full">Tag 4</span>
</div>
<!-- ============================================
PART 8: GROW AND SHRINK
============================================ -->
<div class="flex gap-4">
  <div class="flex-none w-16">Icon</div>
  <div class="flex-1">Fills the rest</div>
  <button class="shrink-0">Action</button>
</div>
<!-- ============================================
PART 9: TRUNCATION IN FLEX
============================================ -->
<div class="flex gap-3 max-w-md">
  <div class="min-w-0 flex-1">
    <p class="truncate">A very long filename that must be truncated.txt</p>
  </div>
  <button class="shrink-0">Open</button>
</div>
<!-- ============================================
PART 10: COMPLETE NAVBAR
============================================ -->
<header class="flex items-center justify-between px-4 h-16 bg-white shadow-sm">
  <div class="flex items-center gap-2">
    <div class="size-8 bg-blue-500 rounded"></div>
    <span class="font-semibold">App</span>
  </div>
  <nav class="flex items-center gap-6">
    <a href="#" class="text-sm hover:text-blue-600">Dashboard</a>
    <a href="#" class="text-sm hover:text-blue-600">Settings</a>
  </nav>
  <div class="flex items-center gap-3">
    <button class="shrink-0">Sign out</button>
  </div>
</header>

These ten parts cover basic flex rows, responsive direction, justify between, centering, card rows with stretch, self alignment, wrapping, grow and shrink, truncation in flex, and a complete navbar.


Quick Reference

Container Utilities

UtilityCSSPurpose
flexdisplay: flexEstablish flex container
inline-flexdisplay: inline-flexInline flex container
flex-rowflex-direction: rowHorizontal
flex-colflex-direction: columnVertical
flex-row-reverseflex-direction: row-reverseHorizontal reversed
flex-wrapflex-wrap: wrapAllow wrapping
flex-nowrapflex-wrap: nowrapNo wrapping
justify-*justify-contentDistribute main axis
items-*align-itemsAlign cross axis
gap-*gapSpace between children

Justify Content

UtilityCSS
justify-startflex-start
justify-endflex-end
justify-centercenter
justify-betweenspace-between
justify-aroundspace-around
justify-evenlyspace-evenly
justify-stretchstretch

Align Items

UtilityCSS
items-startflex-start
items-endflex-end
items-centercenter
items-baselinebaseline
items-stretchstretch

Align Self

UtilityCSS
self-autoauto
self-startflex-start
self-endflex-end
self-centercenter
self-stretchstretch

Grow and Shrink (v4)

UtilityCSSNotes
growflex-grow: 1v4 alias for flex-grow
grow-0flex-grow: 0
shrinkflex-shrink: 1v4 alias for flex-shrink
shrink-0flex-shrink: 0
flex-1flex: 1 1 0%Grow and shrink equally
flex-noneflex: noneFixed size

Best Practices

✅ Do This:

<!-- Direction changes at breakpoint -->
<div class="flex flex-col md:flex-row gap-4">

<!-- Center content with items-center -->
<div class="flex items-center gap-4">

<!-- Use gap instead of margins -->
<div class="flex gap-4">

<!-- min-w-0 for truncation in flex -->
<div class="min-w-0 flex-1 truncate">

<!-- shrink-0 for fixed items -->
<button class="shrink-0">Action</button>

❌ Don’t Do This:

<!-- Using margins for spacing -->
<div class="flex"><div class="mr-4">Item</div></div>  <!-- ❌ use gap -->

<!-- Forgetting min-w-0 -->
<div class="flex-1 truncate">Won't truncate</div>  <!-- ❌ -->

<!-- Using flex-grow-* in v4 -->
<div class="flex-grow">  <!-- ❌ use grow -->

<!-- justify-content without flex container -->
<div class="justify-center">  <!-- ❌ no flex parent -->

Common Pitfalls

PitfallWhy It HappensFix
Justify has no effectParent is not flexAdd flex to parent
Truncation failsMissing min-w-0Add min-w-0 to flex child
Items overflowMissing flex-wrapAdd flex-wrap
Items shrink unexpectedlyDefault shrink: 1Add shrink-0 to fixed items
Gap not appliedParent is not flexAdd flex to parent
flex-grow-* not workingUsing v3 name in v4Use grow-*

Real-World Examples

1. Navbar

<nav class="flex items-center justify-between p-4">
  <div>Logo</div>
  <div class="flex gap-4">Links</div>
</nav>

2. Centered Hero

<div class="flex items-center justify-center min-h-screen">
  <h1>Hero</h1>
</div>

3. Card Row

<div class="flex items-stretch gap-4">
  <div class="flex-1">Card</div>
  <div class="flex-1">Card</div>
</div>

4. Sidebar Layout

<div class="flex">
  <aside class="w-64 shrink-0">Sidebar</aside>
  <main class="flex-1">Content</main>
</div>

5. Tag Cloud

<div class="flex flex-wrap gap-2">
  <span class="px-2 py-1 rounded">Tag</span>
</div>

6. Icon and Text

<div class="flex items-center gap-2">
  <svg class="size-4">...</svg>
  <span>Label</span>
</div>

7. Responsive Stack

<div class="flex flex-col sm:flex-row gap-4">
  <div>Item 1</div>
  <div>Item 2</div>
</div>

8. Truncated with Action

<div class="flex gap-2">
  <span class="min-w-0 flex-1 truncate">Long name</span>
  <button class="shrink-0">Edit</button>
</div>

9. Space Between

<div class="flex justify-between">
  <span>Label</span>
  <span>Value</span>
</div>

10. Self-Aligned Card

<div class="flex items-center gap-4">
  <div>Centered</div>
  <div class="self-start">Top</div>
</div>

Visual

Flex Container and Axes

┌──────────────────────────────────────────────────────────────┐
│  flex-row (default):                                         │
│  ┌────────────────────────────────────────────────────────┐  │
│  │  ┌────┐  ┌────┐  ┌────┐                                │  │
│  │  │ A  │  │ B  │  │ C  │                                │  │
│  │  └────┘  └────┘  └────┘                                │  │
│  └────────────────────────────────────────────────────────┘  │
│  Main axis: horizontal (justify-content)                     │
│  Cross axis: vertical (align-items)                          │
│                                                              │
│  flex-col:                                                   │
│  ┌────────────────────────────────────────────────────────┐  │
│  │  ┌────┐                                                │  │
│  │  │ A  │                                                │  │
│  │  └────┘                                                │  │
│  │  ┌────┐                                                │  │
│  │  │ B  │                                                │  │
│  │  └────┘                                                │  │
│  └────────────────────────────────────────────────────────┘  │
│  Main axis: vertical (justify-content)                       │
│  Cross axis: horizontal (align-items)                        │
└──────────────────────────────────────────────────────────────┘

Justify Content

┌──────────────────────────────────────────────────────────────┐
│  justify-start:                                              │
│  │[A][B][C]                    │                             │
│                                                              │
│  justify-center:                                             │
│  │        [A][B][C]            │                             │
│                                                              │
│  justify-end:                                                │
│  │                    [A][B][C]│                             │
│                                                              │
│  justify-between:                                            │
│  │[A]         [B]         [C]  │                             │
│                                                              │
│  justify-around:                                             │
│  │ [A]   [B]   [C]            │                             │
│                                                              │
│  justify-evenly:                                             │
│  │   [A]   [B]   [C]          │                             │
└──────────────────────────────────────────────────────────────┘

Align Items

┌──────────────────────────────────────────────────────────────┐
│  items-start:                items-center:                   │
│  ┌────┐┌────┐┌────┐          ┌────┐┌────┐┌────┐              │
│  │ A  ││ B  ││ C  │          │    ││ B  ││    │              │
│  │    ││    ││    │          │ A  ││    ││ C  │              │
│  │    ││    ││    │          │    ││    ││    │              │
│  └────┘└────┘└────┘          └────┘└────┘└────┘              │
│                                                              │
│  items-end:                  items-stretch:                  │
│  ┌────┐┌────┐┌────┐          ┌────┐┌────┐┌────┐              │
│  │    ││    ││    │          │ A  ││ B  ││ C  │              │
│  │    ││    ││    │          │    ││    ││    │              │
│  │ A  ││ B  ││ C  │          └────┘└────┘└────┘              │
│  └────┘└────┘└────┘          All same height                 │
└──────────────────────────────────────────────────────────────┘

Grow and Shrink

┌──────────────────────────────────────────────────────────────┐
│  flex-none          flex-1          flex-1                   │
│  ┌────────┐         ┌──────────┐   ┌──────────┐             │
│  │ Fixed  │         │  Equal   │   │  Equal   │             │
│  └────────┘         └──────────┘   └──────────┘             │
│                                                              │
│  flex-none: does not grow or shrink                          │
│  flex-1:    grows and shrinks to fill available space        │
│                                                              │
│  WITH min-w-0:                                               │
│  ┌──────────────────────────────┐  ┌────────┐               │
│  │  Long text truncated...      │  │ Button │               │
│  └──────────────────────────────┘  └────────┘               │
│  └── min-w-0 allows shrink below content width               │
└──────────────────────────────────────────────────────────────┘

Responsive Direction

┌──────────────────────────────────────────────────────────────┐
│  MOBILE (flex-col)              DESKTOP (md:flex-row)        │
│  ┌────────────────────┐         ┌──────────┬──────────────┐  │
│  │  ┌──────────────┐  │         │          │              │  │
│  │  │   Sidebar    │  │         │ Sidebar  │   Content    │  │
│  │  └──────────────┘  │         │          │              │  │
│  │  ┌──────────────┐  │         │          │              │  │
│  │  │   Content    │  │         │          │              │  │
│  │  └──────────────┘  │         │          │              │  │
│  └────────────────────┘         └──────────┴──────────────┘  │
│                                                              │
│  <div class="flex flex-col md:flex-row gap-4">               │
└──────────────────────────────────────────────────────────────┘

Summary

ItemValue
Containerflex, inline-flex
Directionflex-row, flex-col, flex-row-reverse, flex-col-reverse
Wrapflex-wrap, flex-nowrap, flex-wrap-reverse
Justifyjustify-start, justify-center, justify-between, justify-evenly
Align itemsitems-start, items-center, items-stretch, items-baseline
Align selfself-center, self-start, self-end, self-stretch
Gapgap-*, gap-x-*, gap-y-*
Growgrow, grow-0 (v4 alias for flex-grow-*)
Shrinkshrink, shrink-0 (v4 alias for flex-shrink-*)
Flex shorthandflex-1, flex-auto, flex-initial, flex-none
TruncationRequires min-w-0 on flex child
Responsiveflex-col md:flex-row

Key takeaways:

  • flex establishes the container; direction sets the main axis. flex-row lays children horizontally, flex-col vertically. The main axis determines what justify-* distributes along, and the cross axis determines what items-* aligns along.
  • justify-* distributes along the main axis. justify-between pushes the first and last children to the edges with equal space between. justify-center centers the group. justify-evenly produces the most uniform spacing.
  • items-* aligns along the cross axis. items-center is the most common, used for aligning icons with text. items-stretch is the default and makes all children the same height.
  • self-* overrides alignment for one child. When most children should align one way but one needs a different alignment, self-start, self-center, or self-end applies to that child alone.
  • gap-* spaces children without margin. Gap does not add space at the edges, unlike margins. It works with wrapping and is the recommended replacement for the space-x/space-y utilities .
  • grow-* and shrink-* control space distribution. grow makes a child fill available space; shrink-0 prevents it from shrinking. flex-1 combines grow and shrink for equal distribution.
  • min-w-0 is required for truncation in flex. A flex child’s automatic minimum size is its content width, which prevents shrinking. min-w-0 removes that floor so truncate can work.
  • In v4, flex-grow-* and flex-shrink-* are aliased to grow-* and shrink-*. The shorter names are the v4 convention; the longer names still work but are deprecated .

Remember: Flexbox is a one-dimensional layout system: it arranges children along a main axis and aligns them along a cross axis. The container properties — direction, wrap, justify, items, gap — control the layout of all children. The item properties — grow, shrink, self, order — control individual children. The most common patterns are flex items-center gap-4 for horizontal alignment, flex flex-col gap-2 for vertical stacks, flex justify-between items-center for navbars, and flex-1 min-w-0 for flexible content that can truncate. The v4 aliases shortened flex-grow and flex-shrink to grow and shrink, and gap replaced the older space utilities. Understanding the main axis and cross axis is the key to reasoning about any flex layout: justify distributes along the main axis, items aligns along the cross axis, and the axis swaps when the direction changes from row to column.



Stop using slow, ad-bloated tool sites! 🤮

🔎 Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)

⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free

🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!