---
title: "Grid · UI SDK — 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/ui-sdk/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: UI SDK
- Also available for: [Ark UI](https://stylexblocks.com/components/ark-ui/grid.md), [Base UI](https://stylexblocks.com/components/base-ui/grid.md)
- Category: Layout
- Used in blocks: [AI Chat Landing](https://stylexblocks.com/block/ui-sdk/ai-chat-landing.md), [Analytics Dashboard](https://stylexblocks.com/block/ui-sdk/analytics-dashboard.md), [Card Grid](https://stylexblocks.com/block/ui-sdk/card-grid.md), [Checkout Form](https://stylexblocks.com/block/ui-sdk/checkout-form.md), [Checkout Wizard](https://stylexblocks.com/block/ui-sdk/checkout-wizard.md), [Classic Gallery](https://stylexblocks.com/block/ui-sdk/classic-gallery.md), [Collapsible Table](https://stylexblocks.com/block/ui-sdk/collapsible-table.md), [Contact Form](https://stylexblocks.com/block/ui-sdk/contact-form.md), [Course Detail](https://stylexblocks.com/block/ui-sdk/course-detail.md), [Data Dashboard](https://stylexblocks.com/block/ui-sdk/data-dashboard.md), [Documentation Catalog](https://stylexblocks.com/block/ui-sdk/documentation-catalog.md), [Documentation Design](https://stylexblocks.com/block/ui-sdk/documentation-design.md), [Documentation Technical](https://stylexblocks.com/block/ui-sdk/documentation-technical.md), [Executive Dashboard](https://stylexblocks.com/block/ui-sdk/executive-dashboard.md), [File Explorer](https://stylexblocks.com/block/ui-sdk/file-explorer.md), [Filterable Table](https://stylexblocks.com/block/ui-sdk/filterable-table.md), [Two-Column Form](https://stylexblocks.com/block/ui-sdk/form-two-column.md), [Form Wizard](https://stylexblocks.com/block/ui-sdk/form-wizard.md), [Dialog Wizard](https://stylexblocks.com/block/ui-sdk/form-wizard-dialog.md), [Inline Wizard](https://stylexblocks.com/block/ui-sdk/form-wizard-inline.md), [Vertical Wizard](https://stylexblocks.com/block/ui-sdk/form-wizard-vertical.md), [Funnel & Cohort Dashboard](https://stylexblocks.com/block/ui-sdk/funnel-cohort-dashboard.md), [Gallery Hero](https://stylexblocks.com/block/ui-sdk/gallery-hero.md), [Grouped Table](https://stylexblocks.com/block/ui-sdk/grouped-table.md), [Incident Console](https://stylexblocks.com/block/ui-sdk/incident-console.md), [Kanban Board](https://stylexblocks.com/block/ui-sdk/kanban-board.md), [Login Card](https://stylexblocks.com/block/ui-sdk/login-card.md), [Login Split](https://stylexblocks.com/block/ui-sdk/login-split.md), [Mixed Gallery](https://stylexblocks.com/block/ui-sdk/mixed-gallery.md), [Order Detail](https://stylexblocks.com/block/ui-sdk/order-detail.md), [Page Editor](https://stylexblocks.com/block/ui-sdk/page-editor.md), [Portfolio Dashboard](https://stylexblocks.com/block/ui-sdk/portfolio-dashboard.md), [Product Detail](https://stylexblocks.com/block/ui-sdk/product-detail.md), [Product Gallery](https://stylexblocks.com/block/ui-sdk/product-gallery.md), [Project Status Dashboard](https://stylexblocks.com/block/ui-sdk/project-status-dashboard.md), [Service Monitoring](https://stylexblocks.com/block/ui-sdk/service-monitoring.md), [Settings Dialog](https://stylexblocks.com/block/ui-sdk/settings-dialog.md), [Settings Form](https://stylexblocks.com/block/ui-sdk/settings-form.md), [Settings Panels](https://stylexblocks.com/block/ui-sdk/settings-panels.md), [Shell Nav](https://stylexblocks.com/block/ui-sdk/shell-nav.md), [Side Nav Shell](https://stylexblocks.com/block/ui-sdk/shell-side-nav.md), [Top Nav Shell](https://stylexblocks.com/block/ui-sdk/shell-top-nav.md), [Side Gallery](https://stylexblocks.com/block/ui-sdk/side-gallery.md), [Simple Table](https://stylexblocks.com/block/ui-sdk/simple-table.md), [Theme Showcase](https://stylexblocks.com/block/ui-sdk/theme-showcase.md), [Work Item Detail](https://stylexblocks.com/block/ui-sdk/work-item-detail.md)
- HTML page: https://stylexblocks.com/components/ui-sdk/grid/

## Install

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

```sh
npx stylexblocks init --style ui-sdk
```

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/ui-sdk/grid.json

## Usage

```tsx
import { Grid, GridItem } from "#components/ui/ui-sdk/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/ui-sdk/grid.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import { createComponent } from "#lib/ui-sdk/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 `render`.
 */
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>;
```
