Skip to main content

Grid & GridItem

Grid is a responsive layout container built on CSS grid. It fits columns to the available width automatically, or takes a fixed column count / template that collapses gracefully as space shrinks. GridItem wraps a cell to span multiple columns or rows.

When to use

  • Card galleries, dashboards, tile layouts that should reflow with the viewport.
  • Any responsive grid where you'd otherwise hand-write grid-template-columns media queries.

Import

import { Grid, GridItem } from '@spartanfx/react';

Auto-fit columns (default)

By default Grid auto-fits columns based on breakThreshold (minimum px per column) and gap.

<Grid gap={16} minCols={1} maxCols={4}>
{cards.map((c) => (
<div key={c.id} className="card">{c.title}</div>
))}
</Grid>

Fixed or templated columns

{/* Fixed 3 columns that collapse when space is tight */}
<Grid cols={3} gap={12}></Grid>

{/* Proportional template */}
<Grid cols="1fr 2fr 1fr" gap={12}></Grid>

Spanning cells

Wrap a child in GridItem to span columns or rows.

<Grid cols={4} gap={12}>
<GridItem colSpan={2}><FeaturePanel /></GridItem>
<GridItem><Stat /></GridItem>
<GridItem><Stat /></GridItem>
<GridItem rowSpan={2}><SidePanel /></GridItem>
</Grid>

Key props

Grid

PropTypeDefaultNotes
cols'auto' | number | string'auto'Auto-fit, fixed count, or an fr template.
gap / columnGapnumber | string12Spacing (px number or CSS value).
minCols / maxColsnumber1 / —Bounds when cols="auto".
breakThresholdnumber150Min px per column for auto-fit.
alignItems / justifyItemsalign values'stretch'Item alignment.
askeyof JSX.IntrinsicElements'div'Container element.

GridItem

PropTypeDefaultNotes
colSpan / rowSpannumber1Cells to span.
className / styleCell overrides.

See the full generated reference: Grid ↗ · GridItem ↗.

See also