Responsive utilities for the column and row counts, the auto-placement, and the implicit tracks of a CSS Grid container.
Overview
Grid utilities set the number of columns and rows of a grid and the way its items are placed. They act on .grid, the container of the grid system, and on any element with display: grid (.d-grid). The classes that place a single item — .col-span-*, .col-start-*, .col-end-*, .row-span-*, .row-start-*, .row-end-* — belong to the grid system and are covered in Columns.
Column counts
.grid-cols-{n} sets grid-template-columns to n equal tracks, from 1 to 12. On a .grid it replaces the column count of --cx-grid-columns, so items of equal width need no span class.
<div class="grid grid-cols-3 text-center">
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
</div><div class="grid grid-cols-4 text-center">
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
</div>Span classes count against the tracks of the utility:
<div class="grid grid-cols-4 text-center">
<div>Item</div>
<div class="col-span-2">.col-span-2</div>
<div>Item</div>
<div class="col-span-full">.col-span-full</div>
</div>Each track is minmax(0, 1fr), the declaration of the Tailwind utility of the same name, so wide content does not widen a track:
.grid-cols-3 {
grid-template-columns: repeat(3, minmax(0, 1fr));
}.grid-cols-none removes the template, which leaves the columns to auto flow and the implicit tracks, and .grid-cols-subgrid takes the tracks of the parent grid — see Subgrid. Both have the variants of the counts, so a template can be dropped or adopted at a breakpoint (.md:grid-cols-subgrid).
.grid-cols-fill, which made an item span the whole grid, was removed in v0.7.0. Use .col-span-full.
Row counts
.grid-rows-{n} sets grid-template-rows to n equal rows, from 1 to 6. A .grid declares no rows of its own, so without the class each row is as tall as its content. The class applies to one grid, not to the grids nested in it. .grid-rows-none removes a template set at a narrower breakpoint, and .grid-rows-subgrid takes the rows of the parent grid, on a nested grid that spans several of them.
<div class="grid grid-cols-3 grid-rows-2 text-center">
<div class="row-span-2">.row-span-2</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
</div>Each row is minmax(0, 1fr), the declaration of the Tailwind utility of the same name. Items past the last explicit row create rows as tall as their content.
.grid-rows-2 {
grid-template-rows: repeat(2, minmax(0, 1fr));
}Auto flow
.grid-flow-{value} sets grid-auto-flow, the direction in which items without a start line fill the grid. The values are Tailwind's: row, col, dense, row-dense, and col-dense.
Until v0.7.0 these classes were .grid-auto-flow-row, .grid-auto-flow-column, and .grid-auto-flow-dense. They are now .grid-flow-row, .grid-flow-col, and .grid-flow-dense.
Row
.grid-flow-row, the browser default, fills each row in turn and adds rows as needed.
<div class="grid grid-cols-3 grid-flow-row text-center">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
<div>Item 5</div>
</div>Column
.grid-flow-col fills each column in turn and adds columns as needed. .grid-rows-{n} sets the number of rows a column holds.
<div class="grid grid-cols-3 grid-rows-2 grid-flow-col text-center">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
<div>Item 5</div>
</div>Dense
.grid-flow-dense lets a later item fill a hole that an earlier, wider item left behind. .grid-flow-row-dense and .grid-flow-col-dense name the direction with it.
<div class="grid grid-cols-3 grid-flow-dense text-center">
<div class="col-span-2">Item 1 (spans 2)</div>
<div class="col-span-2">Item 2 (spans 2)</div>
<div>Item 3</div>
<div>Item 4</div>
</div>Dense packing moves items out of their DOM order on screen, while keyboards and screen readers keep that order. Use it where the order carries no meaning, such as a gallery.
Implicit tracks
A grid creates tracks beyond its template when it has more items than cells: rows in a row flow, columns in a column flow. .auto-rows-{value} and .auto-cols-{value} set their size with grid-auto-rows and grid-auto-columns: auto (the default, the size of the content), min (min-content), max (max-content), and fr (minmax(0, 1fr), equal tracks).
<div class="grid grid-cols-3 auto-rows-fr text-center">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
<div>Item 5</div>
</div><div class="grid grid-cols-none grid-flow-col auto-cols-max text-center">
<div class="px-xs">Item</div>
<div class="px-xs">A longer item</div>
<div class="px-xs">Item</div>
</div>Responsive
Every grid utility has a variant per breakpoint.
<div class="grid grid-cols-2 md:grid-cols-4 text-center">
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
</div>Responsive columns
The column counts run from 1 to $grid-columns, 12 by default, with the none and subgrid values, at each breakpoint:
.grid-cols-1to.grid-cols-12,.grid-cols-none,.grid-cols-subgrid.sm:grid-cols-1to.sm:grid-cols-12,.sm:grid-cols-none,.sm:grid-cols-subgrid.md:grid-cols-1to.md:grid-cols-12,.md:grid-cols-none,.md:grid-cols-subgrid.lg:grid-cols-1to.lg:grid-cols-12,.lg:grid-cols-none,.lg:grid-cols-subgrid.xl:grid-cols-1to.xl:grid-cols-12,.xl:grid-cols-none,.xl:grid-cols-subgrid.2xl:grid-cols-1to.2xl:grid-cols-12,.2xl:grid-cols-none,.2xl:grid-cols-subgrid
Responsive rows
The row counts run from 1 to $grid-rows, 6 by default, with the same two values:
.grid-rows-1to.grid-rows-6,.grid-rows-none,.grid-rows-subgrid.sm:grid-rows-1to.sm:grid-rows-6,.sm:grid-rows-none,.sm:grid-rows-subgrid.md:grid-rows-1to.md:grid-rows-6,.md:grid-rows-none,.md:grid-rows-subgrid.lg:grid-rows-1to.lg:grid-rows-6,.lg:grid-rows-none,.lg:grid-rows-subgrid.xl:grid-rows-1to.xl:grid-rows-6,.xl:grid-rows-none,.xl:grid-rows-subgrid.2xl:grid-rows-1to.2xl:grid-rows-6,.2xl:grid-rows-none,.2xl:grid-rows-subgrid
Responsive flow
The five flow classes have a variant at each breakpoint:
.grid-flow-row.grid-flow-col.grid-flow-dense.grid-flow-row-dense.grid-flow-col-dense.sm:grid-flow-row.sm:grid-flow-col.sm:grid-flow-dense.sm:grid-flow-row-dense.sm:grid-flow-col-dense.md:grid-flow-row.md:grid-flow-col.md:grid-flow-dense.md:grid-flow-row-dense.md:grid-flow-col-dense.lg:grid-flow-row.lg:grid-flow-col.lg:grid-flow-dense.lg:grid-flow-row-dense.lg:grid-flow-col-dense.xl:grid-flow-row.xl:grid-flow-col.xl:grid-flow-dense.xl:grid-flow-row-dense.xl:grid-flow-col-dense.2xl:grid-flow-row.2xl:grid-flow-col.2xl:grid-flow-dense.2xl:grid-flow-row-dense.2xl:grid-flow-col-dense
Implicit track sizes
The implicit track utilities have a variant per breakpoint too: .md:auto-rows-fr, .lg:auto-cols-max.
Container variants
Every grid utility also has a variant per container width, under the @ prefix: .@md:grid-cols-3 applies when the nearest query container is at least as wide as the md breakpoint. See Container queries.
CSS
Grid utilities are generated via the utilities API.
Reference
The table lists all grid utilities and their CSS properties.
| Class | Properties |
|---|---|
.grid-cols-1 | grid-template-columns: repeat(1, minmax(0, 1fr)); |
.grid-cols-2 | grid-template-columns: repeat(2, minmax(0, 1fr)); |
.grid-cols-3 | grid-template-columns: repeat(3, minmax(0, 1fr)); |
.grid-cols-4 | grid-template-columns: repeat(4, minmax(0, 1fr)); |
.grid-cols-5 | grid-template-columns: repeat(5, minmax(0, 1fr)); |
.grid-cols-6 | grid-template-columns: repeat(6, minmax(0, 1fr)); |
.grid-cols-7 | grid-template-columns: repeat(7, minmax(0, 1fr)); |
.grid-cols-8 | grid-template-columns: repeat(8, minmax(0, 1fr)); |
.grid-cols-9 | grid-template-columns: repeat(9, minmax(0, 1fr)); |
.grid-cols-10 | grid-template-columns: repeat(10, minmax(0, 1fr)); |
.grid-cols-11 | grid-template-columns: repeat(11, minmax(0, 1fr)); |
.grid-cols-12 | grid-template-columns: repeat(12, minmax(0, 1fr)); |
.grid-cols-none | grid-template-columns: none; |
.grid-cols-subgrid | grid-template-columns: subgrid; |
.grid-rows-1 | grid-template-rows: repeat(1, minmax(0, 1fr)); |
.grid-rows-2 | grid-template-rows: repeat(2, minmax(0, 1fr)); |
.grid-rows-3 | grid-template-rows: repeat(3, minmax(0, 1fr)); |
.grid-rows-4 | grid-template-rows: repeat(4, minmax(0, 1fr)); |
.grid-rows-5 | grid-template-rows: repeat(5, minmax(0, 1fr)); |
.grid-rows-6 | grid-template-rows: repeat(6, minmax(0, 1fr)); |
.grid-rows-none | grid-template-rows: none; |
.grid-rows-subgrid | grid-template-rows: subgrid; |
.grid-flow-row | grid-auto-flow: row; |
.grid-flow-col | grid-auto-flow: column; |
.grid-flow-dense | grid-auto-flow: dense; |
.grid-flow-row-dense | grid-auto-flow: row dense; |
.grid-flow-col-dense | grid-auto-flow: column dense; |
.auto-cols-auto | grid-auto-columns: auto; |
.auto-cols-min | grid-auto-columns: min-content; |
.auto-cols-max | grid-auto-columns: max-content; |
.auto-cols-fr | grid-auto-columns: minmax(0, 1fr); |
.auto-rows-auto | grid-auto-rows: auto; |
.auto-rows-min | grid-auto-rows: min-content; |
.auto-rows-max | grid-auto-rows: max-content; |
.auto-rows-fr | grid-auto-rows: minmax(0, 1fr); |
Utilities API
Grid utilities are declared in scss/utilities/_grid.scss using the utilities API. The column counts are generated from 1 to $grid-columns, the row counts from 1 to $grid-rows.
"grid-column-counts": (
responsive: true,
container: true,
property: grid-template-columns,
class: grid-cols,
values: map.merge($-grid-column-counts, (none: none, subgrid: subgrid))
),
"grid-row-counts": (
responsive: true,
container: true,
property: grid-template-rows,
class: grid-rows,
values: map.merge($-grid-row-counts, (none: none, subgrid: subgrid))
),
"grid-flow": (
responsive: true,
container: true,
property: grid-auto-flow,
class: grid-flow,
values: (
row: row,
col: column,
dense: dense,
row-dense: row dense,
col-dense: column dense
)
),
"auto-cols": (
responsive: true,
container: true,
property: grid-auto-columns,
class: auto-cols,
values: $-grid-auto-tracks
),
"auto-rows": (
responsive: true,
container: true,
property: grid-auto-rows,
class: auto-rows,
values: $-grid-auto-tracks
),