Skip to content

Tailwind CSS - Grid Auto Rows

Modern CSS Grid: Controlling Implicit Row Sizes with Tailwind CSS

Section titled “Modern CSS Grid: Controlling Implicit Row Sizes with Tailwind CSS”

In CSS Grid, when you add more items than your grid template defines, the browser creates implicit rows to hold them. The grid-auto-rows utilities in Tailwind CSS give you precise control over the height of these automatically generated rows, ensuring your layouts remain consistent and predictable.

While you can use a CDN for quick demos, for any real-world project, it’s highly recommended to install Tailwind CSS as a PostCSS plugin using a package manager. This unlocks Tailwind’s full potential, including customization, plugins, and tree-shaking for optimized production builds.

Here’s a typical setup with Vite:

Terminal window
# 1. Create a new project with Vite (or your favorite tool)
npm create vite@latest my-tailwind-project -- --template vanilla
cd my-tailwind-project
# 2. Install Tailwind CSS and its peer dependencies
npm install -D tailwindcss postcss autoprefixer
# 3. Initialize Tailwind CSS
npx tailwindcss init -p

This creates tailwind.config.js and postcss.config.js files, which you’ll configure to process your CSS. All examples below will work with either a CDN or a proper setup.

Here are the core utilities for managing the size of implicitly created grid rows. These classes are applied to the grid container.

ClassCSS PropertiesCommon Use Case
auto-rows-autogrid-auto-rows: auto;Rows adapt to the height of their tallest content. Ideal for grids with diverse content sizes.
auto-rows-mingrid-auto-rows: min-content;Rows shrink to the minimum height required by their content. Useful for compact, dense layouts.
auto-rows-maxgrid-auto-rows: max-content;Rows expand to the maximum intrinsic height of their content. Ensures content never wraps or gets cut off.
auto-rows-frgrid-auto-rows: minmax(0, 1fr);Implicit rows share the remaining vertical space equally. Perfect for creating uniform, full-height rows.

Example 1: Content-Based Row Height with auto-rows-auto

Section titled “Example 1: Content-Based Row Height with auto-rows-auto”

The auto-rows-auto utility is the default behavior. Each implicit row’s height is determined by the tallest item within it. This is useful when content varies significantly and you want to avoid empty space.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.tailwindcss.com"></script>
<title>Auto Rows: Auto</title>
</head>
<body class="bg-slate-100 p-8">
<div class="container mx-auto">
<h2 class="text-2xl font-bold mb-4 text-slate-800">Product Gallery</h2>
<div class="grid grid-cols-2 md:grid-cols-4 gap-4 auto-rows-auto">
<div class="bg-cyan-500 text-white p-4 rounded-lg shadow-lg">Item 1</div>
<div class="bg-cyan-600 text-white p-4 h-32 rounded-lg shadow-lg">Item 2 (Taller)</div>
<div class="bg-cyan-500 text-white p-4 rounded-lg shadow-lg">Item 3</div>
<div class="bg-cyan-500 text-white p-4 rounded-lg shadow-lg">Item 4</div>
<!-- This item starts an implicit row -->
<div class="bg-teal-500 text-white p-4 rounded-lg shadow-lg">Item 5 (Implicit Row)</div>
<div class="bg-teal-600 text-white p-4 h-40 rounded-lg shadow-lg">Item 6 (Implicit Row, Taller)</div>
</div>
</div>
</body>
</html>

Example 2: Uniform Flexible Rows with auto-rows-fr

Section titled “Example 2: Uniform Flexible Rows with auto-rows-fr”

The auto-rows-fr utility is excellent for creating clean, uniform grids where each implicit row takes up an equal fraction of the available space in the grid container. For this to have a visible effect, the grid container must have a defined height.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.tailwindcss.com"></script>
<title>Auto Rows: FR</title>
</head>
<body class="bg-slate-100 p-8">
<div class="container mx-auto">
<h2 class="text-2xl font-bold mb-4 text-slate-800">Feature List</h2>
<!-- The grid container needs a height for 'fr' units to work -->
<div class="grid grid-cols-3 auto-rows-fr gap-4 h-96 bg-white p-4 rounded-lg shadow-md">
<div class="bg-indigo-400 text-white flex items-center justify-center p-4 rounded-lg">Feature 1</div>
<div class="bg-indigo-400 text-white flex items-center justify-center p-4 rounded-lg">Feature 2</div>
<div class="bg-indigo-400 text-white flex items-center justify-center p-4 rounded-lg">Feature 3</div>
<!-- This implicit row will also take up its share of the 96-unit height -->
<div class="bg-violet-500 text-white flex items-center justify-center p-4 rounded-lg">Implicit Row Item</div>
</div>
</div>
</body>
</html>
  • grid-template-rows vs. grid-auto-rows: A common point of confusion. Use rows-{n} (grid-template-rows) to define the size of your explicit grid rows (the ones you plan for). Use auto-rows-* (grid-auto-rows) to define a sizing pattern for any implicit rows that get created automatically.
  • auto-rows-fr Not Working?: The fr unit distributes available space. If your grid container’s height is determined only by its content, there is no “available space” to distribute. Give the grid container a specific height (e.g., h-screen, h-[500px]) to see auto-rows-fr in action.
  • Debugging Tip: Use your browser’s developer tools to inspect the grid. Most browsers have a grid inspector that visually overlays the grid lines, clearly showing the difference between explicit and implicit tracks. This is the fastest way to diagnose layout issues.