Kombai Logo

Tailwind CSS Grid Template Rows

Grid Template Rows defines the heights of the rows in a grid container, enabling you to build layouts that are flexible, maintainable, and easy to extend. Tailwind provides a wide range of utilities to manage the row definitions of your grid, allowing you to control everything from basic row configurations to advanced nesting scenarios within the same codebase.

In this guide, we will explore how to use Grid Template Rows utilities in Tailwind CSS, covering everything from fundamental concepts to advanced customization techniques.

ClassPropertiesExample
grid-rows-1grid-template-rows: repeat(1, minmax(0, 1fr));<div className="grid-rows-1"></div>
grid-rows-2grid-template-rows: repeat(2, minmax(0, 1fr));<div className="grid-rows-2"></div>
grid-rows-3grid-template-rows: repeat(3, minmax(0, 1fr));<div className="grid-rows-3"></div>
grid-rows-4grid-template-rows: repeat(4, minmax(0, 1fr));<div className="grid-rows-4"></div>
grid-rows-5grid-template-rows: repeat(5, minmax(0, 1fr));<div className="grid-rows-5"></div>
grid-rows-6grid-template-rows: repeat(6, minmax(0, 1fr));<div className="grid-rows-6"></div>
grid-rows-7grid-template-rows: repeat(7, minmax(0, 1fr));<div className="grid-rows-7"></div>
grid-rows-8grid-template-rows: repeat(8, minmax(0, 1fr));<div className="grid-rows-8"></div>
grid-rows-9grid-template-rows: repeat(9, minmax(0, 1fr));<div className="grid-rows-9"></div>
grid-rows-10grid-template-rows: repeat(10, minmax(0, 1fr));<div className="grid-rows-10"></div>
grid-rows-11grid-template-rows: repeat(11, minmax(0, 1fr));<div className="grid-rows-11"></div>
grid-rows-12grid-template-rows: repeat(12, minmax(0, 1fr));<div className="grid-rows-12"></div>
grid-rows-nonegrid-template-rows: none;<div className="grid-rows-none"></div>
grid-rows-subgridgrid-template-rows: subgrid;<div className="grid-rows-subgrid"></div>

Overview of Grid Template Rows

Adding the Grid Template Rows

To define the number of rows in grid, use Tailwind's grid-rows-* utilities like- grid-rows-2, grid-rows-3, grid-rows-4, etc.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Subgrid Support

To define sub-grids within parent grid items, use grid-rows-subgrid.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

States and Responsiveness

Hover and Focus States

Just like other Tailwind properties, you can adapt Grid Template Row behavior to hover, focus, or other interactive states. This ensures your layout can dynamically alter row definitions when a user interacts with certain elements.

To define grid rows on states like hover and focus, use hover and focus modifiers, e.g., hover:grid-rows-3, focus:grid-rows-4, etc.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Breakpoint Modifiers

Building responsive layouts often means adjusting the number and sizes of rows for different screen widths. Tailwind provides breakpoint-specific prefixes for utility classes. You can do something similar with the grid-template-rows property, switching from one arrangement to another based on device size.

To define grid rows based on the device breakpoint, use the breakpoint modifiers(sm, md, etc.), e.g., md:grid-rows-6.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Custom Grid Template Rows

Extending the Theme

To configure custom row setups, you can extend the theme in your tailwind.config.js file. By doing so, you’ll have a new set of utility classes that directly reflect your unique row definitions. This approach is particularly convenient if you plan to reuse the same row patterns across multiple sections of your application.

Below is a conceptual snippet of a theme extension in tailwind.config.js:

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Using Arbitrary Values

Tailwind’s arbitrary value feature allows you to apply custom, one-off styles that are not found in the default or extended configuration. This method gives you immediate flexibility without requiring you to open or modify your Tailwind configuration.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Real World Examples

Product Showcase Grid

A responsive product grid layout for an e-commerce store featuring electronics items with image, title, price, and rating.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Team Directory

A team directory layout displaying team members with their roles and contact information.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

A dynamic photography gallery layout with featured images spanning multiple rows, creating a masonry-like effect.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Dynamic Content Feed

A compact social media-style feed with featured posts spanning multiple rows.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Project Board

A compact project management board with tasks spanning different row heights based on priority and content.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Customization Examples

A responsive photo gallery that uses custom grid rows for an asymmetric layout with smooth scroll snapping.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Dashboard Layout

A dashboard interface with a custom grid template for header, content, and footer sections.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Timeline View

A vertical timeline with custom row height for different events.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Best Practices

Maintain Design Consistency

Consistency in design plays a crucial role in delivering a seamless user experience. When applying Tailwind's grid-rows-* utilities, it is essential to maintain a structured approach that aligns with the rest of your project's grid layout. By defining clear row configurations, you can ensure a predictable content structure across multiple components. For example, keeping a consistent row height ratio across different sections, such as headers, content, and footers.

Tailwind’s predefined classes like grid-rows-2, grid-rows-3, etc., offer a consistent approach to defining row templates without introducing unpredictable gaps or spacing inconsistencies. When custom grid rows are needed, rely on defining the values in the config file rather than using arbitrary values here and there. This makes it easier to align the layout with the overall design system and apply changes consistently across different screen sizes.

Optimize for Reusability

Reusability is an important aspect of modern development, and leveraging Tailwind’s grid-rows-* utilities effectively can enhance component scalability. Designing components with flexible grid structures ensures they can be used across different sections without requiring significant modifications.

One way to optimize reusability is by creating utility-based grid templates that can accommodate different content types. For example, defining a grid-rows-4 structure in a reusable card component allows developers to maintain consistent row heights while dynamically adjusting content inside each row.

Accessibility Considerations

Improve Content Structure and Flow

Using Tailwind’s grid-rows-* utilities effectively ensures a structured and predictable layout, improving both usability and visual harmony. A well-defined row system prevents content from becoming disorganized or misaligned, creating a more seamless and engaging reading experience.

By specifying explicit row templates like grid-rows-3 or grid-rows-6, developers can maintain a consistent vertical structure, ensuring that elements align correctly across different screen sizes. Additionally, incorporating spacing utilities such as gap-* enhances content separation, reducing visual clutter and guiding users’ attention to key information.

This structured approach helps maintain clear distinctions between paragraphs, lists, and headings, promoting readability for all users, including those with cognitive impairments.

Focs on High Contrast

Maintaining adequate contrast is essential for accessibility, as it ensures that all users, including those with visual impairments, can clearly perceive content. The way rows are structured using grid-rows-* impacts how colors and text align within a section, influencing contrast and readability.

To create well-defined visual separation, use border-* and shadow-* utilities alongside grid row definitions. For example, applying border-b between rows enhances distinction, while using bg-gray-100 for alternating rows improves contrast in tabular layouts. Additionally, defining a background color with sufficient contrast against text ensures that content remains legible.