Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 37 ยท Layout with Flexbox and Grid

CSS Grid With Tailwind CSS

Create two-dimensional layouts with Tailwind v4 grid utilities: grid-cols and grid-rows, col-span, auto-flow, responsive card grids, auto-fill and subgrid.

Where flexbox lays items out along one line, grid works in two dimensions: rows and columns at once. That makes it the natural tool for card galleries, dashboards, page templates and any layout where things need to line up both horizontally and vertically. Tailwind's grid classes are short and the number you write is the number of columns.

Columns and gap

Add grid to a container and choose the number of equal-width columns with grid-cols-{n}:

HTML
<div class="grid grid-cols-3 gap-4">
  <div>1</div><div>2</div><div>3</div>
  <div>4</div><div>5</div><div>6</div>
</div>

grid-cols-3 writes grid-template-columns: repeat(3, minmax(0, 1fr)): three equal columns. The items fill the cells left to right, then wrap into new rows automatically. In v4 any number works, such as grid-cols-13 (v3 stopped at 12). grid-cols-none removes the template, and gap-*, gap-x-*, gap-y-* space the cells, exactly as in Spacing.

A responsive card grid is just the number changing with the screen size:

HTML
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4"> ... </div>

One column on phones, two from 640px, four from 1024px. Chapter 5 explains the prefixes; for now read sm:grid-cols-2 as "from the small breakpoint up, use two columns".

Spanning cells

Items can cover several columns or rows:

Class Meaning
col-span-2 span 2 columns
col-span-full span every column
col-start-2, col-end-4 start or end on a grid line
row-span-2 span 2 rows
row-start-1 begin on row line 1

Grid lines are numbered from 1, with line 1 at the left edge, so col-start-2 col-span-2 covers columns 2 and 3.

A bento layout with spans

The first block covers a 2x2 area. Grid placed the small items in the remaining cells, because grid's default is to fill empty cells in order.

Rows, auto-placement and flow

grid-rows-{n} defines explicit rows. Usually you let rows be created automatically, and size them with auto-rows-*:

  • auto-rows-min, auto-rows-fr, auto-rows-auto for implicit rows.
  • auto-cols-* for implicit columns.
  • grid-flow-row (default), grid-flow-col (fill columns first), grid-flow-dense (backfill holes created by spans).

grid-flow-col with grid-rows-3 produces a list that flows down three rows, then continues in the next column: perfect for a short multi-column list.

Custom templates

For anything beyond equal columns, use the arbitrary syntax (see Arbitrary Values):

HTML
<div class="grid grid-cols-[240px_1fr] gap-6">     <!-- sidebar + content -->
<div class="grid grid-cols-[2fr_1fr_1fr]">          <!-- weighted columns -->
<div class="grid grid-cols-[auto_1fr_auto]">        <!-- icon, text, action -->

Auto-fill and auto-fit: grids with no breakpoints

The most useful grid trick is a template that adapts to the available width by itself. repeat(auto-fill, minmax(12rem, 1fr)) means: put as many columns of at least 12rem as will fit, and share the leftover space.

Responsive gallery with auto-fill

Use auto-fit instead of auto-fill if you want the columns to stretch when there are fewer items than columns. This needs no md: or lg: classes at all.

Aligning inside the grid

Grid shares alignment names with flexbox but with more options:

  • justify-items-* and items-* align each item inside its cell; place-items-center does both.
  • justify-self-*, self-* and place-self-* do the same for a single item.
  • justify-*, content-* and place-content-* on the container move the whole grid when the tracks are smaller than the container.

The famous one-liner for centering is grid place-items-center on a parent with a height.

Subgrid

When cards sit in a grid and you want their inner parts (title, body, button) to line up across cards, a card can adopt the parent's rows with grid-rows-subgrid. In v4, grid-cols-subgrid and grid-rows-subgrid are available together with row-span-*:

HTML
<div class="grid grid-cols-3 gap-4">
  <article class="row-span-3 grid grid-rows-subgrid gap-y-2"> <h3>...</h3> <p>...</p> <a>...</a> </article>
  ...
</div>

Browsers have supported subgrid in all engines since late 2023, so you can use it now.

Page layout with grid

A two-column page with a header and sidebar is a few classes:

HTML
<div class="grid min-h-dvh grid-cols-[16rem_1fr] grid-rows-[auto_1fr]">
  <header class="col-span-2">...</header>
  <aside>...</aside>
  <main>...</main>
</div>

You will build such layouts in Page Layout Patterns.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does grid-cols-3 create?

    grid-cols-3 is repeat(3, minmax(0, 1fr)), three equal columns.
  2. 2. Which class makes an item span the whole row regardless of the number of columns?

    col-span-full spans from the first to the last grid line (grid-column: 1 / -1).
  3. 3. What does grid-cols-[repeat(auto-fill,minmax(12rem,1fr))] do?

    auto-fill creates as many tracks as fit, each at least 12rem wide, sharing leftover space.
  4. 4. Which pair centers a single child inside a grid parent?

    place-items-center aligns the item in both axes within its cell.

Summary

  • grid plus grid-cols-{n} and gap-* make a grid; v4 accepts any column count.
  • col-span-*, row-span-*, col-start-* place and size items; col-span-full spans all columns.
  • Arbitrary templates (grid-cols-[240px_1fr], repeat(auto-fill, minmax(...))) give flexible layouts without breakpoints.
  • place-items-center, justify-self-* and self-* align; subgrid lines up nested content.

Next: Page Layout Patterns