Tailwind Grid: A Quick Overview

By: Erwin, 19 April 2023

Category: #quickoverview

< back to the Tailscan Blog

In this Quick Overview, we will explain Grid layouts in Tailwind CSS. We will dive into what grid layouts are, the difference between grid and flex and how to create grid layouts using Tailwind CSS. After that, we will talk about grid structure, item placement, order, alignment, and the usage of arbitrary values with Tailwind grid classes. Ready? Let's get started!

What is a Grid Layout?

The Grid layout is an easy way to organize elements in a container. It helps control the position, alignment, and size of elements, making responsive user interfaces look better. Grid layouts replace older, less flexible methods, making it simpler to create designs that work well on different devices and screen sizes.

Difference between grid and flex in Tailwind

Both grid and flex in Tailwind CSS are powerful layout systems that can help you with creating responsive and adaptive designs. However, there are some key differences between the two. The grid layout is a two-dimensional system, allowing for the control of both columns and rows, while flex is a one-dimensional system that primarily focuses on controlling the layout along a single axis (either horizontal or vertical). Besides that, grid layouts can handle overlapping elements more effectively, whereas flex layouts are better suited for distributing space along a single axis.

How to make grid layouts in Tailwind

Tailwind offers a variety of pre-built classes, enabling you to easily construct responsive and adaptable grid systems. To start, apply the grid class to a container element, followed by grid-cols-{value} and grid-rows-{value} classes to define the number of columns or rows.

Additionally, Tailwind allows for precise control over grid item placement, alignment, spacing, and sizing, as well as the ability to create auto-sized columns and rows, resulting in highly customizable grid layouts to suit your exact requirements. We'll go further into detail a bit later in this article.

Here is a basic example that shows where to place these classes:

<!-- Grid container -->
<div class="grid grid-cols-3">
  <!-- Items inside the grid container -->
  <div>Item</div>
  <div>Item</div>
</div>

Control the grid layout in Tailwind

Template Columns and Template Rows

In Tailwind CSS, you can define the structure of your grid layout by specifying the number of columns and rows using the grid-cols-{value} and grid-rows-{value} classes. For example, to create a grid with three columns and two rows, you can use the following classes:

<div class="grid grid-cols-3 grid-rows-2">
  <!-- Six grid items go here -->
</div>
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6

You don't always have to specify both the amount of columns and amount of rows, though. For example, if you load data that is dynamic, it's best to define just one of the two.

Column Start/End and Row Start/End

To control the placement of grid items within the grid layout, Tailwind CSS offers a variety of classes for defining the start, end, and span of columns and rows. These classes allow you to precisely position and size grid items:

Row and column span: Determines how many columns or rows a grid item should occupy.

  • col-span-{value}: Specifies the number of columns a grid item should span. Possible values are 1-12 and full.
  • row-span-{value}: Specifies the number of rows a grid item should span. Possible values are 1-6 and full.

Row and Column start: Specifies the starting point of a grid item within the grid.

Row and Column end: Indicates the ending point of a grid item within the grid.

  • col-end-{value}: Specifies the column where a grid item should end. Possible values are 1-13 and auto.
  • row-end-{value}: Specifies the row where a grid item should end. Possible values are 1-7 and auto.

Here is an example showing the use of span, start and end with rows and columns in a grid that has 6 columns and 3 rows:

<div class="grid grid-cols-6 grid-rows-3">
  <!-- Grid item with col-span-3 -->
  <div class="col-span-3">
    Item 1 (col-span-3)
  </div>

  <!-- Grid item with col-start-4 and row-span-2 -->
  <div class="col-start-4 row-span-2">
    Item 2 (col-start-4, row-span-2)
  </div>

  <!-- Grid item with col-end-6 and row-start-2 -->
  <div class="col-end-6 row-start-2">
    Item 3 (col-end-6, row-start-2)
  </div>

  <!-- Grid item with col-span-full and row-start-3 -->
  <div class="col-span-full row-start-3">
    Item 4 (col-span-full, row-start-3)
  </div>
</div>
Item 1 (col-span-3)
Item 2 (col-start-4, row-span-2)
Item 3 (col-end-6, row-start-2)
Item 4 (col-span-full, row-start-3)
Item 5
Item 6
Item 7
Item 8
Item 9
Item 10

Auto Columns, Auto Rows, and Auto Flow

Important to note is that for most use cases, these classes aren't necessary. Tailwind's default grid behavior is to automatically size grid tracks based on the content they hold. However, there are some cases where you may want to explicitly set the size of the grid tracks, and these classes can help.

The following are classes for creating auto-sized columns and rows within your grid layout. These classes help manage the size of the grid tracks, adapting them based on the content they hold.

For auto-sized columns, use the auto-cols-{value} class, where {value} can be one of the following: auto, min, max, or fr. These values determine the sizing behavior of the auto-generated columns:

  • auto-cols-auto: Column size adapts to the content size, without causing overflow or wrapping.
  • auto-cols-min: Column size is set to the minimum value that prevents overflow or wrapping.
  • auto-cols-max: Column size is set to the maximum available space, distributing it evenly among columns.
  • auto-cols-fr: Column size is set to a flexible fraction of the remaining space, using the CSS minmax(0, 1fr). Want to learn more about the fr unit? Check out this comprehensive guide by CSS-Tricks.

Similarly, for auto-sized rows, use the auto-rows-{value} class, where {value} can also be auto, min, max, or fr. These values determine the sizing behavior of the auto-generated rows:

  • auto-rows-auto: Row size adapts to the content size, without causing overflow or wrapping.
  • auto-rows-min: Row size is set to the minimum value that prevents overflow or wrapping.
  • auto-rows-max: Row size is set to the maximum available space, distributing it evenly among rows.
  • auto-rows-fr: Row size is set to a flexible fraction of the remaining space, using the CSS minmax(0, 1fr). Want to learn more about the fr unit? Check out this comprehensive guide by CSS-Tricks.

To control the flow direction of grid items, use the grid-flow-{value} class, where {value} can be one of the following: row, col, row-dense, col-dense, or dense. These values determine how grid items are placed and how auto-generated tracks are created:

  • grid-flow-row: Grid items are placed in rows, and auto-generated tracks are created horizontally.
  • grid-flow-col: Grid items are placed in columns, and auto-generated tracks are created vertically.
  • grid-flow-row-dense/grid-flow-col-dense: Grid items are placed in rows or columns, respectively, and auto-generated tracks are created to fill any available gaps in the grid.
  • grid-flow-dense: Grid items are placed in either rows or columns, depending on the available space, and auto-generated tracks are created to fill any gaps in the grid.

Order and Gap

You can control the order of grid items and add gaps between them using the order-{value}, gap-{value}, gap-x-{value}, and gap-y-{value} classes. For example, to create a grid layout with a gap of 1 rem between columns and rows, you can use gap-4:

<div class="grid grid-cols-3 grid-rows-2 gap-4">
  <div class="order-last">Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
  <div>Item 4</div>
</div>
Item 1
Item 2
Item 3
Item 4

The order classes range from order-1 to order-12, as well as order-last, order-first, order-none, and negative orders. You can also use arbitrary values for more flexibility. See our Quick Overview for Flexbox for more examples with gap and order.

Grid item placement in Tailwind

In this section, we'll explore the most important aspect of grids: item placement. Learn how to effectively position, align, and control individual grid items using Tailwind's utility classes for a polished grid layout.

Justify content, self and items

<div class="grid justify-{position}">
  <div>Item 1</div>
  <div>Item 2</div>
</div>

<div class="grid justify-items-{position}">
  <div>Item 1</div>
  <div>Item 2</div>
</div>

<div class="grid">
  <div class="justify-self-{position}">Item 1</div>
  <div>Item 2</div>
</div>

Examples for justify-{position}

Place these classes on the parent container that also contains the class grid.

justify-normal
Item 1
Item 2
Item 3
justify-start
Item 1
Item 2
Item 3
justify-center
Item 1
Item 2
Item 3
justify-end
Item 1
Item 2
Item 3
justify-between
Item 1
Item 2
Item 3
justify-around
Item 1
Item 2
Item 3
justify-stretch
Item 1
Item 2
Item 3
justify-evenly
Item 1
Item 2
Item 3

Examples for justify-items-{position}

Place these classes on the parent container that also contains the class grid.

justify-items-start
Item 1
Item 2
Item 3
justify-items-center
Item 1
Item 2
Item 3
justify-items-end
Item 1
Item 2
Item 3
justify-items-stretch
Item 1
Item 2
Item 3

Examples for justify-self-{position}

Place these classes on the individual grid item(s). It's especially useful if you want to override the alignment of a single item.

justify-self-start
Item 1
Item 2
Item 3
justify-self-center
Item 1
Item 2
Item 3
justify-self-end
Item 1
Item 2
Item 3
justify-self-auto
Item 1
Item 2
Item 3
justify-self-stretch
Item 1
Item 2
Item 3

Align content, self and items

<div class="grid content-{position}">
  <div>Item 1</div>
  <div>Item 2</div>
</div>

<div class="grid items-{position}">
  <div>Item 1</div>
  <div>Item 2</div>
</div>

<div class="grid">
  <div class="self-{position}">Item 1</div>
  <div>Item 2</div>
</div>

Examples for content-{position}

Place these classes on the parent container that also contains the class grid.

content-start
Item 1
Item 2
content-center
Item 1
Item 2
content-end
Item 1
Item 2
content-between
Item 1
Item 2
content-around
Item 1
Item 2
content-evenly
Item 1
Item 2
content-normal
Item 1
Item 2
content-stretch
Item 1
Item 2
content-baseline
Item 1
Item 2

Examples for items-{position}

Place these classes on the parent container that also contains the class grid.

items-start
Item 1
Item 2
Item 3
items-center
Item 1
Item 2
Item 3
items-end
Item 1
Item 2
Item 3
items-stretch
Item 1
Item 2
Item 3
items-baseline
Item 1
Item 2
Item 3

Examples for self-{position}

Place these classes on the individual grid item(s). It's especially useful if you want to override the alignment of a single item.

self-start
Item 1
Item 2
Item 3
self-center
Item 1
Item 2
Item 3
self-end
Item 1
Item 2
Item 3
self-auto
Item 1
Item 2
Item 3
self-stretch
Item 1
Item 2
Item 3
self-baseline
Item 1
Item 2
Item 3

Place content, self and items

Placing is essentially a combination of justify and align.

<div class="grid place-content-{position}">
  <div>Item 1</div>
  <div>Item 2</div>
</div>

<div class="grid place-items-{position}">
  <div>Item 1</div>
  <div>Item 2</div>
</div>

<div class="grid">
  <div class="place-self-{position}">Item 1</div>
  <div>Item 2</div>
</div>

Examples for place-content-{position}

Place these classes on the parent container that also contains the class grid.

place-content-center
Item 1
Item 2
Item 3
Item 4
place-content-start
Item 1
Item 2
Item 3
Item 4
place-content-end
Item 1
Item 2
Item 3
Item 4
place-content-between
Item 1
Item 2
Item 3
Item 4
place-content-around
Item 1
Item 2
Item 3
Item 4
place-content-evenly
Item 1
Item 2
Item 3
Item 4
place-content-baseline
Item 1
Item 2
Item 3
Item 4
place-content-stretch
Item 1
Item 2
Item 3
Item 4

Examples for place-items-{position}

Place these classes on the parent container that also contains the class grid.

place-items-baseline
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
place-items-start
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
place-items-center
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
place-items-end
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
place-items-stretch
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6

Examples for place-self-{position}

Place these classes on the individual grid item(s). It's especially useful if you want to override the alignment of a single item.

place-self-auto
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
place-self-start
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
place-self-center
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
place-self-end
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
place-self-stretch
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6

Final Thoughts

Grid utility classes in Tailwind CSS provide a simple and effective way to create grid layouts and align content. In this blog post, we have covered what grid in Tailwind is and how it can be used for various layout requirements.

Check out all the Tailwind CSS grid (and flex) classes here: Tailwind CSS Grid Classes.

Want to get quick feedback when developing and see what all grid-related classes do instantly? Try out Tailscan below!

Tailscan will help you build grid layouts quicker by immediately giving visual feedback, making your development process more efficient and enjoyable.

Tailscan for Tailwind CSS

The absolute must-have tool for anyone using Tailwind CSS.

Build, design and debug your Tailwind website visually with Tailscan, right within the browser.

Get Tailscan now
video poster

Want to learn more? Join the Tailscan email list!

Get the latest on Tailwind CSS and Tailscan straight to your inbox with our monthly newsletter.

If you want to unsubscribe later, you can do so very easily, no hard feelings!