tailwind-css
  1. tailwind-css-grid-auto-columns

Tailwind CSS Grid Auto Columns

The grid-auto-columns utility in Tailwind CSS sets the size of the auto-generated grid columns in a grid container.

Syntax

To set the size of the auto-generated grid columns, use the following syntax:

<div class="grid grid-cols-4 grid-auto-rows-auto grid-auto-columns-auto">
  <!-- Content goes here -->
</div>

Here, grid-auto-columns-auto sets the size of the auto-generated grid columns to the default value of auto.

Example

Let's see an example of using grid-auto-columns to set the size of the auto-generated grid columns.

<div class="grid grid-cols-3 grid-auto-rows-auto grid-auto-columns-1fr gap-4">
  <div class="bg-red-500 text-white p-4">Box 1</div>
  <div class="bg-green-500 text-white p-4">Box 2</div>
  <div class="bg-blue-500 text-white p-4">Box 3</div>
  <div class="bg-purple-500 text-white p-4">Box 4</div>
  <div class="bg-yellow-500 text-white p-4">Box 5</div>
  <div class="bg-pink-500 text-white p-4">Box 6</div>
</div>

In this example, we have a grid container with 3 columns and auto-generated rows and columns. We have also set the size of the auto-generated columns to 1fr, which means that each auto-generated column will take up an equal amount of space.

Output

The output of the above example will look like this:

Tailwind CSS Grid Auto Columns Example Output

Explanation

In the above example, we have used the grid-auto-columns-1fr utility to set the size of the auto-generated grid columns to 1fr. This means that each auto-generated column will take up an equal amount of space, so the six boxes are evenly spaced out.

Use

The grid-auto-columns utility is useful when you want to control the size of the auto-generated grid columns. You can use it to ensure that the columns are evenly spaced out, or to set a specific size for the auto-generated columns.

Important Points

  • The grid-auto-columns utility sets the size of the auto-generated grid columns in a grid container.
  • The size of the auto-generated columns can be set using a variety of sizing units, including fr, px, em, and %.
  • By default, the size of the auto-generated columns is set to auto.

Summary

The grid-auto-columns utility in Tailwind CSS sets the size of the auto-generated grid columns in a grid container. It can be used to ensure that the columns are evenly spaced out, or to set a specific size for the auto-generated columns. By default, the size of the auto-generated columns is set to auto.

Published on: