Skip to content

Tailwind CSS - Flex

Tailwind’s flex utilities control how flex items grow and shrink to fill the available space within their container. These are essential for creating flexible and responsive layouts where components can adapt their size.

These classes correspond to the CSS flex shorthand property, which controls flex-grow, flex-shrink, and flex-basis.

ClassCSS PropertiesDescription
flex-1flex: 1 1 0%;Allows an item to grow and shrink, ignoring its initial size. It will take up any available space.
flex-autoflex: 1 1 auto;Allows an item to grow and shrink, but its initial size (based on its content) is respected.
flex-initialflex: 0 1 auto;Allows an item to shrink if needed, but it will not grow beyond its initial size.
flex-noneflex: none; (flex: 0 0 auto;)Prevents an item from growing or shrinking at all. It will retain its natural size.

Let’s see how to use these utilities to build a common three-column layout. The central column will be flexible and take up the remaining space, while the side columns will have fixed or content-based widths.

Example: Flexible Content with Fixed Sidebars

Section titled “Example: Flexible Content with Fixed Sidebars”
<!-- Assumes a modern Tailwind CSS setup -->
<div class="p-8 bg-slate-100">
<h2 class="text-2xl font-bold mb-4 text-center">Website Layout</h2>
<div class="flex gap-4 min-h-[200px] text-white text-center">
<!-- Left Sidebar: Fixed width, does not grow or shrink -->
<div class="flex-none w-32 bg-sky-600 rounded-lg flex items-center justify-center">
<p class="font-bold">flex-none</p>
</div>
<!-- Main Content: Takes up all available space -->
<div class="flex-1 bg-sky-500 rounded-lg flex items-center justify-center">
<p class="font-bold text-lg">flex-1</p>
<p class="text-sm ml-2">(Grows and shrinks)</p>
</div>
<!-- Right Sidebar: Shrinks if needed, but doesn't grow -->
<div class="flex-initial w-48 bg-sky-600 rounded-lg flex items-center justify-center">
<p class="font-bold">flex-initial</p>
<p class="text-sm ml-2">(Shrinks, doesn't grow)</p>
</div>
</div>
</div>

The difference between flex-1 and flex-auto can be subtle. It comes down to the flex-basis value.

  • flex-1 (flex-basis: 0%): The item’s size calculation starts from zero. All items with flex-1 will share the available space equally, regardless of their content’s width.
  • flex-auto (flex-basis: auto): The item’s size calculation starts from its auto size (i.e., the width of its content). The remaining space is then distributed. This means an item with more content will be larger than an item with less content, even if both have flex-auto.
<div class="p-8 bg-slate-100 space-y-6">
<div>
<h3 class="font-semibold mb-2">Using `flex-1` (equal distribution)</h3>
<div class="flex gap-4 text-center text-white">
<div class="flex-1 bg-emerald-500 p-4 rounded-md">Short</div>
<div class="flex-1 bg-emerald-500 p-4 rounded-md">This is some longer content</div>
</div>
</div>
<div>
<h3 class="font-semibold mb-2">Using `flex-auto` (content-based distribution)</h3>
<div class="flex gap-4 text-center text-white">
<div class="flex-auto bg-violet-500 p-4 rounded-md">Short</div>
<div class="flex-auto bg-violet-500 p-4 rounded-md">This is some longer content</div>
</div>
</div>
</div>
  • flex-1 and width: When you apply flex-1 to an element, any w-* (width) utility on that same element is usually ignored because flex-basis: 0% overrides it.
  • flex-none for Sizing: Use flex-none for elements that must maintain a fixed size, like icons, avatars, or logos, to prevent them from being distorted by the flex container.
  • Combining with flex-basis: For more granular control, you can use arbitrary values to set the flex-basis directly, e.g., basis-1/2 or basis-[50%].
  • Container is Key: Remember that these flex-* utilities only have an effect on direct children of an element with the flex class.