Skip to main content

Mastering Flexbox Alignment with Tailwind CSS

· 5 min read
Kristofer Löfberg
Elmish Land creator

Flexbox is one of the most powerful tools in modern CSS, enabling developers to create flexible and responsive layouts with ease. One of its greatest strengths lies in its alignment capabilities, allowing precise control over how elements are positioned within a container.

In this guide, I’ll break down Flexbox alignment properties with Tailwind CSS, ensuring you understand not just how they work, but when to use them effectively. If you're using Elmish Land and need help setting it up for Tailwind, you can read this blog post first before continuing.

A great place to try and explore the tailwind utility classes is the Tailwind Play site.

Understanding the Flexbox Axes

Before diving into alignment classes, it’s essential to understand how Flexbox operates:

  • Main Axis → The direction items flow (flex-row = left to right, flex-col = top to bottom).
  • Cross Axis → The perpendicular direction (vertical if flex-row, horizontal if flex-col).

You can read more about flex-direction in the Tailwind documentation.

Now, let’s explore how to align items using Tailwind’s Flexbox utilities.

1. Aligning Items on the Main Axis: justify-*

The justify-* classes control horizontal alignment in a row (flex-row) and vertical alignment in a column (flex-col).

Common Justification Classes:

ClassEffect
justify-startAlign items to the start (left in flex-row, top in flex-col)
justify-endAlign items to the end (right in flex-row, bottom in flex-col)
justify-centerCenter items on any axis
justify-betweenSpread items evenly, first item at the start, last at the end
justify-aroundEqual space around items
justify-evenlyEqual space between and outside items

Use Case: Centering navigation links in a navbar. Preview at tailwind play

<nav class="flex justify-between px-4 py-2 bg-gray-800 text-white">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</nav>

You can read more about justify-content in the Tailwind documentation.

2. Aligning Items on the Cross Axis: items-*

The items-* classes control vertical alignment in a row (flex-row) and horizontal alignment in a column (flex-col).

Common Items Classes:

ClassEffect
items-startItems align at the top (left in flex-col)
items-endItems align at the bottom (right in flex-col)
items-centerItems align in the middle
items-stretchItems stretch to fill the container

Use Case: Centering content inside a hero section (no wrapping). Preview at tailwind play

<section class="flex items-center justify-center h-screen bg-gray-200">
<h1 class="text-4xl font-bold">Welcome</h1>
</section>

You can read more about align-items in the Tailwind documentation.

3. Aligning a Single Item Differently: self-*

If you want only one item to have a different alignment than the rest, use self-*.

Common Self Classes:

ClassEffect
self-startAligns one item at the start
self-endAligns one item at the end
self-centerCenters one item
self-stretchStretches one item

Use Case: A single item in a list behaving differently. Preview at tailwind play

<ul class="flex gap-2 h-50 bg-amber-100">
<li class="bg-blue-500 p-2">Item 1</li>
<li class="bg-red-500 p-2 self-end">Item 2</li>
</ul>

You can read more about align-self in the Tailwind documentation.

4. Controlling Multi-Row Alignment: content-*

If you have multiple rows or columns (when using flex-wrap), use content-* to align them.

Common Multi-Row Alignment Classes:

ClassEffect
content-startRows stack at the top
content-endRows stack at the bottom
content-centerRows are centered
content-betweenRows spread out evenly
content-aroundRows get equal space around them

Use Case: Aligning Wrapped Flex Items in the Center. Preview at tailwind play

<div class="flex flex-wrap content-center h-96 bg-gray-300">
<div class="w-32 h-32 bg-blue-500"></div>
<div class="w-32 h-32 bg-red-500"></div>
<div class="w-32 h-32 bg-green-500"></div>
</div>

You can read more about align-content in the Tailwind documentation.

Cheat Sheet

Tailwind ClassAxisEffect
justify-*Main AxisControls left-to-right (or top-to-bottom in column mode) alignment
items-*Cross AxisControls vertical alignment (or horizontal in column mode)
self-*Cross AxisControls alignment of a single item
content-*Cross Axis (with wrap)Controls multi-row or multi-column spacing

Final Thoughts

Many developers find Flexbox confusing at first, but once you break it down into alignment along two axes, it becomes much easier to understand. Tailwind CSS simplifies this process by providing intuitive utility classes that eliminate the need to memorize complex CSS rules. Instead of struggling with display: flex; and various alignment properties, you can quickly apply justify-*, items-*, and self-* to get the exact layout you need.

The key to mastering Flexbox is practice—experiment with different alignment classes, play around with flex-row vs. flex-col, and observe how items behave. Once it clicks, you’ll realize how powerful and flexible it really is. And with Tailwind, you can skip the frustration and focus on building responsive, clean layouts faster than ever.