---
title: "Grid · Ark UI — StyleX Blocks"
description: "CSS Grid layout primitives (Grid and GridItem) with fixed or auto-fit column tracks, spacing-token gaps, and span controls."
url: "https://stylexblocks.com/components/ark-ui/grid/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Grid

CSS Grid layout primitives (Grid and GridItem) with fixed or auto-fit column tracks, spacing-token gaps, and span controls.

- Behavior engine: Ark UI
- Also available for: [Base UI](https://stylexblocks.com/components/base-ui/grid.md), [UI SDK](https://stylexblocks.com/components/ui-sdk/grid.md)
- Category: Layout
- HTML page: https://stylexblocks.com/components/ark-ui/grid/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init
```

Then add the item as source you own:

```sh
npx stylexblocks add grid
```

Equivalent: `npx shadcn add @stylexblocks/grid`. Registry item JSON: https://stylexblocks.com/r/ark-ui/grid.json

## Usage

```tsx
import { Grid, GridItem } from "#components/ui/ark-ui/grid.tsx";

export function Example() {
  return (
    <Grid columns="3" gap="3">
      <GridItem colSpan="2">Featured</GridItem>
      <GridItem>Side</GridItem>
      <GridItem>One</GridItem>
      <GridItem>Two</GridItem>
      <GridItem>Three</GridItem>
    </Grid>
  );
}
```

## Source

`components/ui/ark-ui/grid.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import { createComponent } from "#lib/ark-ui/create-component.tsx";
import { defineRecipe } from "#lib/recipe-props.ts";
import { spacing } from "@stylexblocks/ui/tokens/spacing.stylex.ts";

const gridStyles = stylex.create({
  base: {
    display: "grid",
    minWidth: 0,
  },
});

const gridColumns = stylex.create({
  "1": { gridTemplateColumns: "repeat(1, minmax(0, 1fr))" },
  "2": { gridTemplateColumns: "repeat(2, minmax(0, 1fr))" },
  "3": { gridTemplateColumns: "repeat(3, minmax(0, 1fr))" },
  "4": { gridTemplateColumns: "repeat(4, minmax(0, 1fr))" },
  "5": { gridTemplateColumns: "repeat(5, minmax(0, 1fr))" },
  "6": { gridTemplateColumns: "repeat(6, minmax(0, 1fr))" },
  "12": { gridTemplateColumns: "repeat(12, minmax(0, 1fr))" },
  autoFit: { gridTemplateColumns: "repeat(auto-fit, minmax(14rem, 1fr))" },
});

const gridGaps = stylex.create({
  "0": { gap: 0 },
  "1": { gap: spacing._1 },
  "2": { gap: spacing._2 },
  "3": { gap: spacing._3 },
  "4": { gap: spacing._4 },
  "5": { gap: spacing._5 },
  "6": { gap: spacing._6 },
  "8": { gap: spacing._8 },
  "10": { gap: spacing._10 },
  "12": { gap: spacing._12 },
});

const gridAligns = stylex.create({
  start: { alignItems: "start" },
  center: { alignItems: "center" },
  end: { alignItems: "end" },
  stretch: { alignItems: "stretch" },
});

const gridJustifies = stylex.create({
  start: { justifyItems: "start" },
  center: { justifyItems: "center" },
  end: { justifyItems: "end" },
  stretch: { justifyItems: "stretch" },
});

const gridRecipe = defineRecipe({
  styles: gridStyles,
  axes: {
    columns: gridColumns,
    gap: gridGaps,
    align: gridAligns,
    justify: gridJustifies,
  },
  defaultVariants: {
    columns: "1",
    gap: "4",
    align: "stretch",
    justify: "stretch",
  },
});

/**
 * Grid — CSS Grid layout primitive with column presets (1–6, 12, or
 * responsive `autoFit`), spacing-token `gap`, and item alignment.
 * Composition via `asChild`.
 */
export const Grid = createComponent("div", gridRecipe, "Grid");
export type GridProps = React.ComponentProps<typeof Grid>;

const gridItemStyles = stylex.create({
  base: { minWidth: 0 },
});

const gridItemColSpans = stylex.create({
  auto: { gridColumn: "auto" },
  "1": { gridColumn: "span 1 / span 1" },
  "2": { gridColumn: "span 2 / span 2" },
  "3": { gridColumn: "span 3 / span 3" },
  "4": { gridColumn: "span 4 / span 4" },
  "5": { gridColumn: "span 5 / span 5" },
  "6": { gridColumn: "span 6 / span 6" },
  full: { gridColumn: "1 / -1" },
});

const gridItemRowSpans = stylex.create({
  auto: { gridRow: "auto" },
  "1": { gridRow: "span 1 / span 1" },
  "2": { gridRow: "span 2 / span 2" },
  "3": { gridRow: "span 3 / span 3" },
  "4": { gridRow: "span 4 / span 4" },
});

const gridItemRecipe = defineRecipe({
  styles: gridItemStyles,
  axes: {
    colSpan: gridItemColSpans,
    rowSpan: gridItemRowSpans,
  },
  defaultVariants: {
    colSpan: "auto",
    rowSpan: "auto",
  },
});

export const GridItem = createComponent("div", gridItemRecipe, "GridItem");
export type GridItemProps = React.ComponentProps<typeof GridItem>;
```
