Lesson 7 / 1258%

Lesson 7

CSS Grid with Tailwind

25 min

Build responsive two-dimensional grid layouts.

Definition

CSS Grid is a layout module that divides a container into rows and columns , forming a grid in which you then place the elements. With Tailwind, you enable it with thegrid class and define the number of columns withgrid-cols-*, which replaces the display: gridand grid-template-columns properties of classic CSS.

CSS Grid with Tailwind

Where Flexbox excels along a single axis (a row or a column), CSS Grid handles two-dimensional layout: rows and columns at the same time. It is the ideal tool for image galleries, dashboards or grids of cards like the one you see on the "Courses" page of this site.

If you have already followed the Flexbox lesson, you will find the same simplicity: a few short classes are enough to describe a complete grid, without writing a single line of CSS by hand.

The Grid vocabulary

  • Grid container : the parent that carries the grid class.
  • Grid items : the direct children of the container.
  • Track : an entire row or column of the grid.
  • Cell : the intersection of a row and a column.

Creating a simple grid

You enable Grid with the grid class, then define the number of columns with grid-cols-*:

<div class="grid grid-cols-3 gap-4">
  <div class="bg-slate-100 p-4">1</div>
  <div class="bg-slate-100 p-4">2</div>
  <div class="bg-slate-100 p-4">3</div>
  <div class="bg-slate-100 p-4">4</div>
  <div class="bg-slate-100 p-4">5</div>
  <div class="bg-slate-100 p-4">6</div>
</div>

Result

Preview of six boxes laid out in a grid of three columns

Each column here is automatically 1/3 of the available width, and the items naturally wrap onto the next row once the 3 columns are filled. The gap-4 adds a uniform space between all the cells, both horizontally and vertically.

You can choose any number of columns withgrid-cols-1, grid-cols-2, up togrid-cols-12. To set the spacing separately, usegap-x-* for the columns and gap-y-* for the rows.

Defining rows with grid-rows-*

In the same way, grid-rows-* sets the number of explicit rows, for example grid-rows-2. In practice, it is rarely specified: rows are created automatically based on the number of items. To give all the automatic rows the same height, add auto-rows-fr to the container.

Changing the number of columns depending on the screen

This is where Grid and Tailwind's responsive design shine together. You combine several grid-cols-* classes prefixed with a screen size:

1 column on mobile, 2 on tablet, 3 on desktop
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
  <div class="bg-white rounded-xl shadow p-4">Card A</div>
  <div class="bg-white rounded-xl shadow p-4">Card B</div>
  <div class="bg-white rounded-xl shadow p-4">Card C</div>
</div>

This technique is exactly the one used to display the course cards on this site: a single column on mobile to stay readable, then two or three columns as the screen gets larger.

Merging cells with col-span and row-span

An item can occupy several columns or several rows thanks tocol-span-* and row-span-*:

A header that takes up the full width
<div class="grid grid-cols-3 gap-4">
  <div class="col-span-3 bg-indigo-600 text-white p-6 rounded-xl">
    Header spanning the full width
  </div>
  <div class="bg-slate-100 p-4 rounded-xl">Card 1</div>
  <div class="bg-slate-100 p-4 rounded-xl">Card 2</div>
  <div class="bg-slate-100 p-4 rounded-xl">Card 3</div>
</div>

Result

Preview of a full-width banner above three cards

If you do not want to count columns, col-span-full stretches an item across the full width, however many columns there are. For even more precise placement, col-start-2 and col-end-4indicate between which grid lines the item should sit.

Aligning the content of cells

As with Flexbox, classes control the alignment of items within their cells. The most useful are items-center for vertical centering, justify-items-center for horizontal centering, and place-items-center which combines both:

<div class="grid grid-cols-2 gap-4 place-items-center h-48">
  <div class="bg-slate-100 p-4">Centered in its cell</div>
  <div class="bg-slate-100 p-4">Also centered</div>
</div>

grid-cols-* vs auto-fit: when to use which?

  • Use grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 when you know exactly the number of columns you want at each screen size — this is the most common case and the easiest to maintain.
  • Use an arbitrary value such asgrid-cols-[repeat(auto-fit,minmax(200px,1fr))] when you want the number of columns to adapt automatically, without explicit breakpoints — handy for galleries where you do not know the number of items in advance.

Example: a dashboard layout

Grid lets you build an entire page with very few classes. Here is a content area with a sidebar and a main content:

<div class="grid grid-cols-1 lg:grid-cols-4 gap-6">
  <aside class="lg:col-span-1 bg-slate-100 p-4 rounded-xl">
    Menu
  </aside>
  <main class="lg:col-span-3 bg-white p-6 rounded-xl shadow">
    Main content
  </main>
</div>

On mobile, the two blocks stack. From the large screen size (lg:), the grid switches to 4 columns: the menu takes up one and the main content takes the other three.

A realistic card grid

Here is a component you will write often: a grid of product cards, each with a title and a text. The grid takes care of the distribution, and each card remains a simple box:

A grid of product cards
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
  <article class="rounded-xl border border-slate-200 bg-white p-5">
    <h3 class="text-lg font-bold text-slate-900">Product A</h3>
    <p class="mt-2 text-sm text-slate-600">A short description.</p>
  </article>
  <article class="rounded-xl border border-slate-200 bg-white p-5">
    <h3 class="text-lg font-bold text-slate-900">Product B</h3>
    <p class="mt-2 text-sm text-slate-600">A short description.</p>
  </article>
</div>

To make all the cards in the same row the same height, there is nothing to add: by default, grid items stretch to fill their cell.

A few best practices

  • Think mobile first : write the one-column version, then add the extra columns for large screens.
  • Keep a consistent gap : choose a value (for examplegap-4 or gap-6) and reuse it across your whole interface.
  • Keep it simple : if a layout requires a lot ofcol-start-* and col-end-*, check whether a simpler structure would do the job.

Flexbox or Grid? How to choose

In short: use Flexbox to align items along a single dimension (a toolbar, a list of tags, an inline form). Use Grid whenever you think in rows AND columns at the same time (a product grid, a dashboard, a magazine-style layout).

The two tools combine very well: an entire page can use Grid for its overall structure, and each card can use Flexbox internally to align its content.

Common beginner mistakes

  • Forgetting the grid class : grid-cols-3alone has no effect, you also need to enable Grid on the container.
  • Applying col-span to the parent : this class goes on the child that should expand, not on the container.
  • Forgetting the mobile starting point : always start withgrid-cols-1, then add columns with sm:,lg:, etc.

To visualize your grid while you build it, open your browser's developer tools (F12): on a gridelement, a badge lets you display the grid lines and numbers. It is a valuable aid for understanding why an element sits in a given place, and for debugging a col-span that does not have the expected effect.

Practical exercise

Build a grid of 6 cards withgrid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4, then make the very first card take up 2 columns on desktop usinglg:col-span-2.