×

Please try us on a desktop

Our app relies on some desktop browser features to render design files and interactable code. Unfortunately, we haven't been able to make these work on mobile yet.

Please try Kombai using a chromium based browser on desktop.

Tailwind

How to use Flex in Tailwind CSS and Best Practices

Published on: May 25, 2023

Flexbox has revolutionized the way developers approach layout design, making it easier than ever to create responsive and adaptable interfaces. Tailwind CSS embraces this innovation through its utility-first approach, providing a seamless and efficient way to leverage the power of Flexbox for diverse web projects.

In this article, we will explore the ins and outs of using flex in Tailwind CSS, offering insights and examples that will help you learn this layout technique. We will begin by discussing the fundamentals of flexbox, and how flex works within the Tailwind CSS framework, followed by an explanation of how flex items grow and shrink, setting flex-direction, and using flex-wrap properties in Tailwind. So without further ado, let's get started.

What is Flexbox?

Flexbox, short for Flexible Box Layout, is a CSS layout model that simplifies the process of designing responsive and fluid web layouts. With Flexbox, developers can efficiently distribute and align elements within a container, making it a powerful tool for building complex UI designs without the need for floats, tables, or other cumbersome methods.

The main components of Flexbox are the container and its child elements, known as flex items. The container is responsible for defining the layout rules, while flex items are automatically rearranged and resized according to these rules.

Here's a simple example of a Flexbox container with three items:

<div class="flex">
  <div class="bg-blue-500 text-white p-4">Item 1</div>
  <div class="bg-green-500 text-white p-4">Item 2</div>
  <div class="bg-red-500 text-white p-4">Item 3</div>
</div>

In this example, we have used the flex class on a container div, making it a Flexbox container. The child divs with different background colors are the flex items. By default, these items will be arranged in a row and resized to fit the container's width.

Note: Try Kombai For Email to convert all your Figma email designs to HTML code. Check out our HTML email templates library to access 400+ free email templates for your future marketing campaigns.

How does flex work in Tailwind CSS?

In Tailwind CSS, you can create a flex container by simply adding the flex class to an HTML element. This class sets the element's display property to flex, initiating Flexbox behavior for that element and its child elements, which become flex items.

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

By default, flex items are arranged in a row, and their widths are adjusted to fit the container. You can easily change this behavior and customize the layout using additional utility classes provided by Tailwind CSS.

How do flex items grow and shrink?

In a flex container, managing how flex items grow and shrink is crucial for creating responsive and adaptable layouts. Tailwind CSS offers a set of utility classes that focus on the flex-grow and flex-shrink properties to determine the distribution of space among flex items as the container size changes.

These classes enable you to define the proportional relationship between flex items and the available space, influencing how items expand or contract when the container is resized. Let’s discuss each of them in detail-

flex-auto

The flex-auto class allows a flex item to grow and shrink automatically based on the available space in the container. By applying this class to a flex item, you're telling it to take up any available free space when there is room in the container, and to shrink proportionally when there's not enough space. This is the default behavior for flex items.

<div class="flex">
  <div class="flex-auto bg-blue-500 text-white p-4">Item 1</div>
  <div class="flex-auto bg-green-500 text-white p-4">Item 2</div>
  <div class="flex-auto bg-red-500 text-white p-4">Item 3</div>
</div>

flex-1

The flex-1 class is similar to flex-auto but with a difference. It ensures that the flex item takes up an equal share of the available space. When you apply the flex-1 class to all flex items within a container, they will grow and shrink together, maintaining the same width relative to one another.

<div class="flex">
  <div class="flex-1 bg-blue-500 text-white p-4">Item 1</div>
  <div class="flex-1 bg-green-500 text-white p-4">Item 2</div>
  <div class="flex-1 bg-red-500 text-white p-4">Item 3</div>
</div>

flex-initial

The flex-initial class sets a flex item to its initial size, neither allowing it to grow nor shrink. The item will maintain its base width, regardless of the available space in the container.

<div class="flex">
  <div class="flex-initial bg-blue-500 text-white p-4">Item 1</div>
  <div class="flex-auto bg-green-500 text-white p-4">Item 2</div>
  <div class="flex-auto bg-red-500 text-white p-4">Item 3</div>
</div>

In this example, Item 1 will remain at its initial width, while Items 2 and 3 will grow or shrink to fill the remaining space.

flex-none

The flex-none class prevents a flex item from growing or shrinking. It will maintain its initial width, regardless of the available space in the container, similar to flex-initial. The difference is that flex-none sets the item's flex-shrink property to 0, ensuring it won't shrink even when the container is too small to accommodate all items.

<div class="flex">
  <div class="flex-none bg-blue-500 text-white p-4">Item 1</div>
  <div class="flex-auto bg-green-500 text-white p-4">Item 2</div>
  <div class="flex-auto bg-red-500 text-white p-4">Item 3</div>
</div>

In this example, Item 1 will maintain its width and won't shrink, while Items 2 and 3 will adjust their sizes based on the available space.

Flex direction in Tailwind

The flex-direction CSS property determines the direction in which flex items are laid out within a container. Tailwind CSS also has a bunch of classes that make it simple to set and modify the direction of your flex containers. Let's explore these classes and their effects on the layout.

flex-row

The flex-row class sets the flex-direction property to row, which is the default value in Flexbox. When you apply this class to a flex container, its flex items will be arranged horizontally, from left to right.

<div class="flex flex-row">
  <div class="bg-blue-500 text-white p-4">Item 1</div>
  <div class="bg-green-500 text-white p-4">Item 2</div>
  <div class="bg-red-500 text-white p-4">Item 3</div>
</div>

flex-row-reverse

The flex-row-reverse class sets the flex-direction property to row-reverse. With this class applied to a flex container, the flex items will be arranged horizontally, but in reverse order, from right to left.

<div class="flex flex-row-reverse">
  <div class="bg-blue-500 text-white p-4">Item 1</div>
  <div class="bg-green-500 text-white p-4">Item 2</div>
  <div class="bg-red-500 text-white p-4">Item 3</div>
</div>

flex-col

The flex-col class changes the flex-direction property to a column. When you use this class on a flex container, its flex items will be arranged vertically, from top to bottom.

<div class="flex flex-col">
  <div class="bg-blue-500 text-white p-4">Item 1</div>
  <div class="bg-green-500 text-white p-4">Item 2</div>
  <div class="bg-red-500 text-white p-4">Item 3</div>
</div>

flex-col-reverse

The flex-col-reverse class sets the flex-direction property to column-reverse. With this class applied to a flex container, the flex items will be arranged vertically, but in reverse order, from bottom to top.

<div class="flex flex-col-reverse">
  <div class="bg-blue-500 text-white p-4">Item 1</div>
  <div class="bg-green-500 text-white p-4">Item 2</div>
  <div class="bg-red-500 text-white p-4">Item 3</div>
</div>

How does flex-wrap work in Tailwind?

Flex-wrap is a CSS property that controls how flex items wrap within a container when there isn't enough room to fit them in a single line. Tailwind CSS offers utility classes that make it easy to control the wrapping behavior of flex items in a container.

flex-wrap

The flex-wrap class sets the flex-wrap property to wrap. When applied to a flex container, this class allows the flex items to wrap onto multiple lines if there's not enough space for them to fit in a single line.

<div class="flex flex-wrap">
  <div class="bg-blue-500 text-white p-4">Item 1</div>
  <div class="bg-green-500 text-white p-4">Item 2</div>
  <div class="bg-red-500 text-white p-4">Item 3</div>
</div>

flex-nowrap

The flex-nowrap class sets the flex-wrap property to nowrap, which is the default behavior. When using this class on a flex container, the flex items will be forced to stay on a single line, even if there's not enough space to accommodate them. Items may overflow the container.

<div class="flex flex-nowrap">
  <div class="bg-blue-500 text-white p-4">Item 1</div>
  <div class="bg-green-500 text-white p-4">Item 2</div>
  <div class="bg-red-500 text-white p-4">Item 3</div>
</div>

flex-wrap-reverse

The flex-wrap-reverse class sets the flex-wrap property to wrap-reverse. With this class applied to a flex container, the flex items will wrap onto multiple lines, but in reverse order. Items will be placed on the last line first, and new lines will be added above existing ones.

<div class="flex flex-wrap-reverse">
  <div class="bg-blue-500 text-white p-4">Item 1</div>
  <div class="bg-green-500 text-white p-4">Item 2</div>
  <div class="bg-red-500 text-white p-4">Item 3</div>
</div>

Conclusion

To summarize, Tailwind CSS is a powerful and efficient tool for leveraging the full potential of Flexbox, allowing you to create responsive and adaptable web layouts with ease. By understanding the fundamentals of Flexbox and the right Tailwind classes, you can create complex UI designs without the need for cumbersome methods.

Remember that Tailwind CSS can speed up your development process, but it's also important to have a solid foundation in CSS. The better your understanding of CSS, the more effectively you can work with Tailwind, and the faster you can build your projects.

We hope that this article has given you the knowledge and confidence to work with Flexbox in Tailwind CSS and create responsive flex layouts on your own.

Happy Coding!