Skip to content

Tailwind CSS - Flex Direction

The Flex Direction utilities in Tailwind CSS are used to control the direction of flex items within a flex container. They determine whether the items are arranged in a horizontal row or a vertical column, and in which direction.

In Flexbox, items are laid out along a main axis and a cross axis. The flex-direction property establishes the main axis. This concept is fundamental to understanding how other flex properties like justify-content and align-items work.

  • flex-row or flex-row-reverse: The main axis is horizontal.
  • flex-col or flex-col-reverse: The main axis is vertical.

Here are the primary utility classes for controlling flex direction:

ClassCSS Properties
flex-rowflex-direction: row;
flex-row-reverseflex-direction: row-reverse;
flex-colflex-direction: column;
flex-col-reverseflex-direction: column-reverse;

Let’s explore how these utilities work in practice. For modern development, it’s recommended to set up Tailwind CSS using a package manager like npm and a build tool like Vite or Next.js. The examples below are HTML snippets but assume such a setup.

In this example, we showcase all four direction utilities. Notice how we use gap-4 on the flex container for spacing, which is the modern best practice over adding margins to individual flex items.

<!-- Assumes a modern Tailwind CSS setup -->
<div class="space-y-8 p-8 bg-slate-50">
<!-- 1. Flex Row (Default) -->
<div>
<h3 class="font-semibold mb-2">flex-row</h3>
<div class="flex flex-row gap-4 p-4 bg-white rounded-lg shadow">
<div class="bg-sky-500 text-white font-bold p-4 rounded-md">1</div>
<div class="bg-sky-500 text-white font-bold p-4 rounded-md">2</div>
<div class="bg-sky-500 text-white font-bold p-4 rounded-md">3</div>
</div>
</div>
<!-- 2. Flex Row Reverse -->
<div>
<h3 class="font-semibold mb-2">flex-row-reverse</h3>
<div class="flex flex-row-reverse gap-4 p-4 bg-white rounded-lg shadow">
<div class="bg-amber-500 text-white font-bold p-4 rounded-md">1</div>
<div class="bg-amber-500 text-white font-bold p-4 rounded-md">2</div>
<div class="bg-amber-500 text-white font-bold p-4 rounded-md">3</div>
</div>
</div>
<!-- 3. Flex Column -->
<div>
<h3 class="font-semibold mb-2">flex-col</h3>
<div class="flex flex-col gap-4 p-4 bg-white rounded-lg shadow">
<div class="bg-emerald-500 text-white font-bold p-4 rounded-md">1</div>
<div class="bg-emerald-500 text-white font-bold p-4 rounded-md">2</div>
<div class="bg-emerald-500 text-white font-bold p-4 rounded-md">3</div>
</div>
</div>
<!-- 4. Flex Column Reverse -->
<div>
<h3 class="font-semibold mb-2">flex-col-reverse</h3>
<div class="flex flex-col-reverse gap-4 p-4 bg-white rounded-lg shadow">
<div class="bg-rose-500 text-white font-bold p-4 rounded-md">1</div>
<div class="bg-rose-500 text-white font-bold p-4 rounded-md">2</div>
<div class="bg-rose-500 text-white font-bold p-4 rounded-md">3</div>
</div>
</div>
</div>

A key strength of Tailwind CSS is its mobile-first responsive design system. You can easily change the flex direction at different screen sizes by using responsive prefixes like sm:, md:, lg:, etc.

The following card layout stacks its content vertically on small screens (flex-col) and arranges it horizontally on medium screens and larger (md:flex-row).

<!-- Responsive Flex Direction -->
<div class="max-w-md mx-auto bg-white rounded-xl shadow-md overflow-hidden md:max-w-2xl m-8">
<div class="md:flex">
<!-- On medium screens and up, the parent is flex-row. We need flex-shrink-0 to prevent the image from being squashed. -->
<div class="md:flex-shrink-0">
<img class="h-48 w-full object-cover md:h-full md:w-48" src="https://picsum.photos/seed/tech/400/300" alt="Modern building architecture">
</div>
<div class="p-8">
<div class="uppercase tracking-wide text-sm text-indigo-500 font-semibold">Case Study</div>
<a href="#" class="block mt-1 text-lg leading-tight font-medium text-black hover:underline">Building a Responsive UI with Tailwind CSS</a>
<p class="mt-2 text-slate-500">Learn how responsive prefixes like `md:flex-row` allow you to create adaptive layouts that work on any device, from mobile phones to desktops.</p>
</div>
</div>
</div>
  • Use gap for spacing: Always prefer using gap-{size} on the flex container to create space between items. Avoid adding margins (m-, mx-, my-) to the flex children, as gap is more predictable and easier to manage.
  • Remember the Main Axis: When you switch from flex-row to flex-col, the main axis switches from horizontal to vertical. This means justify-* utilities will now control vertical alignment, and items-* will control horizontal alignment.
  • Combining with Reverse: When using flex-row-reverse or flex-col-reverse, you might also need the space-x-reverse or space-y-reverse utilities if you are using the space-* utilities for spacing. However, the modern gap-* utility handles this automatically and is therefore recommended.