Skip to content

Tailwind Grid

CSS Grid handles two-dimensional layouts, rows and columns together, better than flexbox ever could. This lesson covers Tailwind's grid utilities for building responsive card grids, dashboards, and page layouts.

What Is Tailwind Grid?

Adding the grid class turns an element into a grid container. From there, grid-cols-* defines how many equal-width columns it has, and gap-* controls spacing between both rows and columns simultaneously.

Individual grid items can span multiple columns or rows using col-span-* and row-span-*, giving you precise control that would require much more complex markup with flexbox alone.

<div class="grid grid-cols-3 gap-4">
  <div class="rounded-lg bg-slate-100 p-4">1</div>
  <div class="rounded-lg bg-slate-100 p-4">2</div>
  <div class="rounded-lg bg-slate-100 p-4">3</div>
  <div class="col-span-2 rounded-lg bg-slate-100 p-4">4 (spans 2 columns)</div>
  <div class="rounded-lg bg-slate-100 p-4">5</div>
</div>

Item 4 spans two of the three columns, while the rest occupy a single column each.

Grid Utility Syntax

class="grid grid-cols-{n} gap-{n}"
class="col-span-{n} row-span-{n}"
class="col-start-{n} col-end-{n}"
  • grid-cols-{n} creates n equal-width columns, from 1 to 12 by default.
  • gap-{n} sets spacing between both rows and columns; use gap-x-*/gap-y-* for one axis only.
  • col-span-{n} makes an item span multiple columns.
  • grid-cols-none and arbitrary values like grid-cols-[200px_1fr] support fully custom track sizes.

Tailwind Grid Cheatsheet

The essential grid utilities for building layouts.

Class CSS Equivalent Purpose
grid display: grid Creates a grid container
grid-cols-4 grid-template-columns: repeat(4, 1fr) Four equal-width columns
grid-rows-3 grid-template-rows: repeat(3, 1fr) Three equal-height rows
col-span-2 grid-column: span 2 / span 2 Item spans two columns
row-span-2 grid-row: span 2 / span 2 Item spans two rows
gap-4 gap: 1rem Space between rows and columns
gap-x-4 column-gap: 1rem Horizontal gap only
place-items-center place-items: center Centers items on both axes
grid-cols-[200px_1fr] grid-template-columns: 200px 1fr Custom, arbitrary column tracks
auto-cols-max grid-auto-columns: max-content Sizes implicit columns to their content

Responsive Column Counts

Just like other Tailwind utilities, grid-cols-* accepts responsive prefixes, letting you show fewer columns on mobile and more as the screen grows, the most common pattern for card and product grids.

<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6">
  <div class="rounded-lg bg-white p-4 shadow">Card 1</div>
  <div class="rounded-lg bg-white p-4 shadow">Card 2</div>
  <div class="rounded-lg bg-white p-4 shadow">Card 3</div>
  <div class="rounded-lg bg-white p-4 shadow">Card 4</div>
</div>

Dashboard Layouts With col-span and row-span

Combining grid-cols-* with col-span-* and row-span-* lets you build asymmetric dashboard layouts, like a large featured widget beside several smaller ones, all within one grid container.

<div class="grid grid-cols-4 grid-rows-2 gap-4">
  <div class="col-span-2 row-span-2 rounded-lg bg-blue-50 p-4">Featured Widget</div>
  <div class="rounded-lg bg-slate-100 p-4">Widget A</div>
  <div class="rounded-lg bg-slate-100 p-4">Widget B</div>
  <div class="rounded-lg bg-slate-100 p-4">Widget C</div>
  <div class="rounded-lg bg-slate-100 p-4">Widget D</div>
</div>

Auto-Sizing Grids With minmax()

Arbitrary value syntax lets you use raw CSS Grid functions like repeat() and minmax() directly in a class name, creating grids that automatically fit as many columns as space allows, without any breakpoint prefixes at all.

<div class="grid grid-cols-[repeat(auto-fit,minmax(200px,1fr))] gap-4">
  <div class="rounded-lg bg-slate-100 p-4">Auto A</div>
  <div class="rounded-lg bg-slate-100 p-4">Auto B</div>
  <div class="rounded-lg bg-slate-100 p-4">Auto C</div>
</div>

This single class automatically adjusts the number of columns based on available width, no breakpoints needed.

Common Mistakes

  • Reaching for Grid when a simple flex row would solve a one-dimensional layout more simply.
  • Forgetting gap-*, then manually adding margin to grid children, which breaks alignment at the container edges.
  • Using col-span-* values larger than the container's grid-cols-* count, which has undefined or unexpected results.
  • Not testing responsive column counts across every breakpoint, only checking mobile and the widest desktop size.
  • Overcomplicating a layout with named grid areas when simple numbered columns and spans would be clearer.

Key Takeaways

  • grid plus grid-cols-* creates a two-dimensional layout with equal-width columns by default.
  • col-span-* and row-span-* let individual items span multiple tracks.
  • Responsive prefixes on grid-cols-* are the standard way to build adaptive card grids.
  • Arbitrary values unlock raw CSS Grid functions like minmax() and repeat() for auto-sizing layouts.
  • Choose Grid for two-dimensional layouts and Flexbox for simpler one-dimensional rows or columns.

Pro Tip

For card grids that need to "just work" at any width without picking specific breakpoints, try grid-cols-[repeat(auto-fit,minmax(220px,1fr))] instead of manually setting column counts per breakpoint.