html

How to Set Flex Basis in Tailwind?

In Tailwind CSS, the flex-basis is a property that specifies how much space a flex item occupies in the main axis of the flex container. It is used for creating responsive layouts with Flexbox. Tailwind offers various sizing options for the flex-basis utility such as relative sizes (3, 28, 1/2, 3/5) and fixed sizes (rem, px, em). Moreover, it also has utilities like flex-auto, flex-initial, and flex-none to set standard values for the flex-basis.

This article will explain the method to set the flex-basis in Tailwind CSS.

How to Set Flex Basis in Tailwind?

To set the flex-basis in Tailwind CSS, make an HTML file. Then, use the “basis-<size>” utility class in the HTML program and specify the flex item size. This utility sets the initial size of the flex items. To ensure the changes, view the web page.

Look at the given-provided steps for a practical demonstration:

Step 1: Set Flex Basis in HTML Program
Make an HTML program and utilize the “basis-<size>” utility class to set the size of the flex item. For instance, we have used “basis-1/4”, “basis-1/3”, and “basis-1/2” utilities to set three flex items:

<body>

    <div class="flex h-20">
        <div class="basis-1/4 bg-red-400 m-1">1</div>
        <div class="basis-1/3 bg-teal-400 m-1">2</div>
        <div class="basis-1/2 bg-orange-400 m-1">3</div>
    </div>

</body>

Here:

  • flex” class is used to create a flexible layout and adjust the child element’s sizes based on available space.
  • h-20” class sets the height of the <div> to 20 units.
  • basis-1/4” class sets the inner <div> element’s width to 25% of its parent element.
  • basis-1/3” class sets the width of the inner <div> to 33.33% of its parent container.
  • basis-1/2” class sets the <div>’s width to 50% of its parent container.
  • bg-red-400” class applies a red background color to the <div>.
  • bg-teal-400” class sets the teal color to the <div>’s background.
  • bg-orange-400” class applies the orange background color to the <div>.
  • m-1” class adds a margin of 1 unit around each <div> element.

Step 2: Verify Output
To ensure that the flex-basis has been set and working properly, view the HTML web page:

In the above output, the flex-basis can be seen according to which they were styled.

Conclusion

Flex basis enables users to create flexible layouts that adjust to various screen sizes and resolutions. To set the flex-basis in Tailwind CSS, the “basis-<size>” utility class is used in the HTML program. Users need to specify the flex item size and ensure the changes by viewing the web page. This article has explained the method to set the flex-basis in Tailwind CSS.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.