Skip to content

Grid Template Columns

View raw .md

Utilities for specifying the columns in a grid layout.

On this page
Preview layout/grid_cols The live demo is not available for this example. grid_cols.dart View source
dart
// Basic grid with 3 columns
WDiv(
  className: 'grid grid-cols-3 gap-4',
  children: [
    WDiv(className: 'bg-blue-500 h-12'),
    WDiv(className: 'bg-blue-500 h-12'),
    WDiv(className: 'bg-blue-500 h-12'),
  ],
)

Basic Usage#

Use grid-cols-{n} to create a grid with n equally sized columns.

This utility must be used alongside the grid class to activate the grid layout model.

dart
WDiv(
  className: 'grid grid-cols-4 gap-4',
  children: [
    // ... 4 items per row
  ],
)

Quick Reference#

Class Result
grid-cols-1 1 equal column
grid-cols-2 2 equal columns
grid-cols-3 3 equal columns
grid-cols-4 4 equal columns
grid-cols-5 5 equal columns
grid-cols-6 6 equal columns
grid-cols-12 12 equal columns

Cells wrap onto a new row after every n items. By default the grid is a Wrap with computed column widths, not a Flutter GridView; with items-stretch it is a column of equal-height rows, and with overflow-x-auto or overflow-x-scroll it is a single Row whose cells keep their intrinsic width.

Equal-Height Rows#

By default a Wind grid sizes each cell to its own content (it renders as a Wrap), so a row where one card is taller leaves the others short. Add items-stretch to make every cell in a row match the tallest, mirroring CSS Grid's default align-items: stretch. With items-stretch the grid renders as a column of equal-height rows instead of a Wrap.

dart
// KPI stat cards that all share the tallest card's height per row
WDiv(
  className: 'grid grid-cols-3 gap-4 items-stretch',
  children: [
    WDiv(
      className: 'p-4 rounded-lg bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700',
      children: [
        WText('Revenue', className: 'text-sm text-gray-500 dark:text-gray-400'),
        WText('\$12,480', className: 'text-2xl font-bold text-gray-900 dark:text-white'),
        WText('+8% vs last week', className: 'text-xs text-green-600 dark:text-green-400'),
      ],
    ),
    // ...sibling cards stretch to match the tallest
  ],
)

The equal-height rows are laid out for real (each cell is measured with a loose height, then re-laid to at least the row's tallest via a min height, never a tight squeeze), NOT via IntrinsicHeight, so cells whose content is itself a flex flex-col, or that use h-full / basis-* (neither of which carries a LayoutBuilder any more), stretch correctly instead of asserting LayoutBuilder does not support returning intrinsic dimensions. Because a cell is never forced below its own content height, a stretched cell also produces no residual RenderFlex overflowed warning (#141).

Column Span#

Add col-span-{n} to a grid child to make it n columns wide, or col-span-full to make it take the whole row. A span is n columns plus the n - 1 gaps between them: in a grid-cols-4 gap-4 grid that is 400 wide, a column is 88 and col-span-2 is 192.

Preview layout/grid_cols The live demo is not available for this example. grid_cols.dart View source
dart
WDiv(
  className: 'grid grid-cols-4 gap-4',
  children: [
    WDiv(className: 'col-span-2 bg-blue-500 dark:bg-blue-600 h-12'),
    WDiv(className: 'bg-blue-500 dark:bg-blue-600 h-12'),
    WDiv(className: 'bg-blue-500 dark:bg-blue-600 h-12'),
    WDiv(className: 'col-span-full bg-blue-500 dark:bg-blue-600 h-12'),
  ],
)
  • A span larger than the column count caps at the column count, and col-span-0 counts as one column.
  • A child's own style: WindStyle(gridColSpan: 2) spans two columns, and a literal col-span-* class overrides it, as it does every other style field.
  • A theme alias that expands to a span works too: with aliases: {'wide-tile': 'col-span-2'}, className: 'wide-tile' spans two columns.
  • Breakpoint and dark: prefixes work: md:col-span-2 is one column below the md breakpoint and two above it. A state prefix is read from the child's own states and from the anchor the grid sits in, so disabled:col-span-2 spans two columns under a disabled WAnchor. A child's own hover: or focus: state is not seen by the grid, so a span behind hover: or focus: is not supported.
  • Rows pack in order and sparsely: a child that does not fit the columns left in the row starts the next row, and later children never backfill the gap.
  • Both the default Wrap grid and the items-stretch grid honour spans; with items-stretch each row still shares one height.
  • The grid reads the span from its direct child, so only a direct child that carries a className can span: a Wind widget such as WDiv, WText or WButton. A plain Flutter widget such as Text or SizedBox, and a WAnchor (which takes no className), keep one column even when a descendant carries col-span-*; wrap such a child in a WDiv to span it.
  • A grid narrower than its gaps has no column width, so on both paths every cell collapses to zero width, except a col-span-full (or a span of every column) cell, which still fills the row.
  • With overflow-x-auto or overflow-x-scroll the grid is a single Row and spans are ignored.
  • row-span-* is not supported.

Responsive#

Prefix grid utilities with breakpoint variants like md: or lg: to change the column count at different screen sizes.

Preview layout/grid_responsive The live demo is not available for this example. grid_responsive.dart View source
dart
WDiv(
  className: 'grid grid-cols-1 md:grid-cols-3 lg:grid-cols-6 gap-4',
  children: [
    // 1 col on mobile, 3 on tablet, 6 on desktop
  ],
)

Dark Mode#

While column counts rarely change based on theme, you can use dark: if needed.

dart
WDiv(
  className: 'grid grid-cols-2 dark:grid-cols-4',
  children: [...],
)

Arbitrary Values#

Wind supports any integer for grid columns directly in the utility class. You do not need square brackets [] for this property.

dart
// Create a grid with exactly 16 columns
WDiv(
  className: 'grid grid-cols-16 gap-1',
  children: [...],
)
esc

Search index did not load.

No match for "".

Try a class like rounded-lg or a widget like WButton.