> ## Documentation Index
> Fetch the complete documentation index at: https://anvil.servicetitan.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Grid – Code

> Create CSS Grid containers.

export const LiveCode = ({children, customHeight, clickToLoad, example, fullWidth, fullHeight, hideCodeInLiveCode, screenshot, screenshotOnly, showCode: showCodeProp}) => {
  const SCREENSHOTS_BASE = "https://servicetitan.github.io/anvil2-docs-live-code/screenshots";
  const STACKBLITZ_BASE = "https://stackblitz.com/github/servicetitan/anvil2-docs-live-code/tree/main/examples";
  const [showCodeBlock, setShowCodeBlock] = useState(showCodeProp ?? false);
  const [isLocalOverride, setIsLocalOverride] = useState(false);
  useEffect(() => {
    const examplePath = `/images/live-code-screenshots-tmp/${example}.png`;
    fetch(examplePath, {
      method: "HEAD"
    }).then(r => {
      if (r.ok) setIsLocalOverride(true);
    }).catch(() => {});
  }, [example]);
  const screenshotBase = isLocalOverride ? "/images/live-code-screenshots-tmp" : SCREENSHOTS_BASE;
  if (screenshotOnly) {
    return <Frame className="flex flex-col">
        <div className="flex dark:hidden" style={{
      justifyContent: "center",
      alignItems: "center",
      width: fullWidth ? "100%" : "50%",
      minHeight: fullHeight ? "284px" : undefined,
      background: "#FFFFFF"
    }}>
          <img srcset={`${screenshotBase}/${example}.png, ${screenshotBase}/${example}-2x.png 2x`} src={`${screenshotBase}/${example}.png`} alt={example} noZoom />
        </div>
        <div className="hidden dark:flex" style={{
      justifyContent: "center",
      alignItems: "center",
      width: fullWidth ? "100%" : "50%",
      minHeight: fullHeight ? "284px" : undefined,
      background: "#141414"
    }}>
          <img srcset={`${screenshotBase}/${example}-dark.png, ${screenshotBase}/${example}-dark-2x.png 2x`} src={`${screenshotBase}/${example}-dark.png`} alt={example} noZoom />
        </div>
      </Frame>;
  }
  if (screenshot) {
    return <Frame className="flex flex-col -mb-2">
        <div className="flex dark:hidden bg-white dark:bg-codeblock border border-gray-950/10 dark:border-white/10 dark:twoslash-dark rounded-2xl overflow-hidden" style={{
      justifyContent: "center",
      alignItems: "center",
      width: fullWidth ? "100%" : "50%",
      minHeight: fullHeight ? "284px" : undefined
    }}>
          <img srcset={`${screenshotBase}/${example}.png, ${screenshotBase}/${example}-2x.png 2x`} src={`${screenshotBase}/${example}.png`} alt={example} noZoom />
        </div>

        <div className="hidden dark:flex bg-white dark:bg-codeblock border border-gray-950/10 dark:border-white/10 dark:twoslash-dark rounded-2xl overflow-hidden" style={{
      background: "#141414",
      justifyContent: "center",
      alignItems: "center",
      width: fullWidth ? "100%" : "50%",
      minHeight: fullHeight ? "284px" : undefined
    }}>
          <img srcset={`${screenshotBase}/${example}-dark.png, ${screenshotBase}/${example}-dark-2x.png 2x`} src={`${screenshotBase}/${example}-dark.png`} alt={example} noZoom />
        </div>

        <div className="flex justify-end items-center text-xs py-2 px-1 gap-4">
          {!showCodeProp ? <button className="inline-flex justify-end items-center text-gray-700 dark:text-gray-50 hover:text-blue-500 dark:hover:text-blue-300 transition-colors group self-end gap-1 cursor-pointer" onClick={() => setShowCodeBlock(!showCodeBlock)} style={{
      appearance: "none"
    }}>
              <Icon icon="code" size="12px" className="group-hover:bg-blue-500 dark:group-hover:bg-blue-300" />
              <span>{showCodeBlock ? "Hide code" : "Show code"}</span>
            </button> : null}

          <a className="inline-flex justify-end items-center hover:text-blue-500 dark:hover:text-blue-300 transition-colors group self-end gap-1" href={`${STACKBLITZ_BASE}/${example}?file=src/App.tsx`} target="_blank" rel="noreferrer">
            <Icon icon="bolt" size="12px" className="group-hover:bg-blue-500 dark:group-hover:bg-blue-300" />
            <span>StackBlitz demo</span>
          </a>
        </div>

        <div className="grid transition-[grid-template-rows] duration-300 ease-in-out overflow-auto overflow-y-hidden overflow-x-auto" style={showCodeBlock ? {
      gridTemplateRows: "1fr"
    } : {
      gridTemplateRows: "0fr"
    }}>
          <div style={{
      minHeight: 0,
      overflowX: "auto",
      overflowY: "hidden",
      marginBlockStart: "-1.25rem",
      marginBlockEnd: "-1.5rem"
    }}>
            {children}
          </div>
        </div>
      </Frame>;
  } else {
    return <div style={{
      display: "flex",
      width: fullWidth ? "100%" : "50%",
      minHeight: customHeight ? customHeight : "316px",
      resize: "vertical",
      overflow: "auto"
    }}>
        <iframe title={example} style={{
      flex: 1,
      width: fullWidth ? "100%" : "50%",
      minHeight: customHeight ? customHeight : "316px"
    }} src={`${STACKBLITZ_BASE}/${example}?embed=1&hideNavigation=1&hideExplorer=1&terminalHeight=0&file=src/App.tsx${clickToLoad ? "&ctl=1" : ""}${hideCodeInLiveCode ? "&view=preview" : ""}`} allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" />
      </div>;
  }
};

<Tabs>
  <Tab title="Implementation">
    <LiveCode showCode example="grid-playground" fullWidth screenshot>
      ```tsx lines expandable theme={null}
      import { Grid, Card } from "@servicetitan/anvil2";

      function App() {
        return (
          <Grid
            templateColumns="repeat(5, 1fr)"
            autoRows="minmax(3rem, auto)"
            rowGap="2"
            columnGap="4"
          >
            <Card background="stronger" gridColumn="1 / 6">
              1
            </Card>
            <Card background="stronger" gridColumn={1} gridRow="2 / 5">
              2
            </Card>
            <Card background="strong" gridColumn="2 / 5" gridRowStart={2}>
              3
            </Card>
            <Card background="stronger" gridColumn="5 / 6" gridRowStart={2}>
              4
            </Card>
            <Card background="strong" gridColumn="2 / 6" gridRow="3 / 5">
              5
            </Card>
          </Grid>
        );
      }

      export default App;
      ```
    </LiveCode>

    ## Common Examples

    ```tsx theme={null}
    import { Grid } from "@servicetitan/anvil2";

    const ExampleComponent = () => (
      <Grid templateColumns="repeat(3, 1fr)">{/* grid items... */}</Grid>
    );
    ```

    ### Creating CSS Grid layouts

    The Anvil2 `Grid` component enables building complex layouts using [CSS Grid](https://css-tricks.com/snippets/css/complete-guide-grid/) properties. A grid is ultimately an HTML `div` element with `display: grid` applied. Use the props listed below to apply other CSS Grid properties to the container.

    Note that a `Grid` is always a container, but can also be used as a grid item by wrapping it in another `Grid`, or even as a flex item if it is a child of a [Flex](/docs/web/components/flex/code/) or other element with `display: flex`.

    ### Using Responsive prop

    Grid, like many of our components, have `sm`, `md`, `lg`, `xl`, and `xxl` that can be used to control component's layout properties.

    Resize window to mobile size to see it in action.

    <LiveCode showCode example="grid-responsive" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Grid } from "@servicetitan/anvil2";

      function App() {
        return (
          <div style={{ width: "calc(100vw - 4rem)" }}>
            <Grid
              templateColumns="repeat(1, 1fr)"
              md={{ templateColumns: "repeat(2, 1fr)" }}
              gap="2"
            >
              <div style={{ backgroundColor: "red", height: 20 }} />
              <div style={{ backgroundColor: "green", height: 20 }} />
              <div style={{ backgroundColor: "blue", height: 20 }} />
              <div style={{ backgroundColor: "black", height: 20 }} />
            </Grid>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ## Best Practices

    * The [Layout](/docs/web/components/layout/code/) component should be used to create the overall column structure of a page's content.
    * Use the `Grid` component to create [CSS Grid](https://css-tricks.com/snippets/css/complete-guide-grid/) containers within the page content.
  </Tab>

  <Tab title="Grid Container Props">
    ```tsx theme={null}
    <Grid
      templateColumns="repeat(3, 1fr)"
      templateRows="auto"
      templateAreas="header header header"
      gap="4"
      columnGap="2"
      rowGap="2"
      alignContent="stretch"
      alignItems="stretch"
      justifyContent="start"
      justifyItems="start"
      placeContent="center"
      placeItems="center"
      autoColumns="auto"
      autoRows="auto"
      inline={false}
    />
    ```

    ## `Grid` Container Props

    In addition to the props listed in the tables below, the `Grid` component can accept any valid HTML `div` props.

    Other than the gap and responsive props, each of the props listed corresponds to a CSS property.

    <ParamField path="autoColumns" type="CSSProperties[gridAutoColumns]" />

    <ParamField path="alignContent" type="CSSProperties[alignContent]" />

    <ParamField path="alignItems" type="CSSProperties[alignItems]" />

    <ParamField path="autoRows" type="CSSProperties[gridAutoRows]" />

    <ParamField path="columnGap" type={`"0" | "half" | "1" | "2" | "3" | "4" | "5" | "6" | "8" | "12" | "13" | "14"`}>
      Applies to `column-gap` CSS property. See
      [Spacing](/docs/web/foundations/spacing)
    </ParamField>

    <ParamField path="gap" type={`"0" | "half" | "1" | "2" | "3" | "4" | "5" | "6" | "8" | "12" | "13" | "14"`}>
      Applies to `gap` CSS property. See [Spacing](/docs/web/foundations/spacing)
    </ParamField>

    <ParamField path="inline" type="boolean">
      Applies `display: inline-grid` CSS property.
    </ParamField>

    <ParamField path="justifyContent" type="CSSProperties[justifyContent]" />

    <ParamField path="justifyItems" type="CSSProperties[justifyItems]" />

    <ParamField path="placeContent" type="CSSProperties[placeContent]" />

    <ParamField path="placeItems" type="CSSProperties[placeItems]" />

    <ParamField path="rowGap" type={`"0" | "half" | "1" | "2" | "3" | "4" | "5" | "6" | "8" | "12" | "13" | "14"`}>
      Applies to `row-gap` CSS property. See
      [Spacing](/docs/web/foundations/spacing)
    </ParamField>

    <ParamField path="templateAreas" type="CSSProperties[gridTemplateAreas]" />

    <ParamField path="templateColumns" type="CSSProperties[gridTemplateColumns]" />

    <ParamField path="templateRows" type="CSSProperties[gridTemplateRows]" />
  </Tab>

  <Tab title="Grid Item Props">
    ```tsx theme={null}
    <Grid
      gridArea="header"
      gridColumn="1 / 3"
      gridColumnEnd="3"
      gridColumnStart="1"
      gridRow="1 / 2"
      gridRowEnd="2"
      gridRowStart="1"
      alignSelf="start"
      justifySelf="start"
      placeSelf="center"
      order={0}
    />
    ```

    ## `Grid` Item Props

    These props are used to configure the `Grid` component as a grid item (with a parent grid container).

    <ParamField path="alignSelf" type="CSSProperties[alignSelf]">
      Can also be used for grid item `Grid`.
    </ParamField>

    <ParamField path="gridArea" type="CSSProperties[gridArea]" />

    <ParamField path="gridColumn" type="CSSProperties[gridColumn]" />

    <ParamField path="gridColumnEnd" type="CSSProperties[gridColumnEnd]" />

    <ParamField path="gridColumnStart" type="CSSProperties[gridColumnStart]" />

    <ParamField path="gridRow" type="CSSProperties[gridRow]" />

    <ParamField path="gridRowEnd" type="CSSProperties[gridRowEnd]" />

    <ParamField path="gridRowStart" type="CSSProperties[gridRowStart]" />

    <ParamField path="justifySelf" type="CSSProperties[justifySelf]" />

    <ParamField path="order" type="CSSProperties[order]">
      Can also be used for grid item `Grid`.
    </ParamField>

    <ParamField path="placeSelf" type="CSSProperties[placeSelf]">
      Can also be used for grid item `Grid`.
    </ParamField>
  </Tab>

  <Tab title="Flex Item Props">
    ```tsx theme={null}
    <Grid flexBasis="auto" flex="0 1 auto" flexGrow={1} flexShrink={1} />
    ```

    ## `Grid` Flex Item Props

    These props are used to configure the `Grid` component as a flex item (with a parent [Flex](/docs/web/components/flex/code) container).

    <ParamField path="flexBasis" type="CSSProperties[flexBasis]" />

    <ParamField path="flex" type="CSSProperties[flex]" />

    <ParamField path="flexGrow" type="CSSProperties[flexGrow]" />

    <ParamField path="flexShrink" type="CSSProperties[flexShrink]" />
  </Tab>

  <Tab title="Responsive Grid Props">
    ```tsx theme={null}
    <Grid
      sm={{ templateColumns: "1fr" }}
      md={{ templateColumns: "repeat(2, 1fr)" }}
      lg={{ templateColumns: "repeat(3, 1fr)" }}
      xl={{ gap: "4" }}
      xxl={{ alignItems: "center" }}
    />
    ```

    ## Responsive `Grid` Props

    The following props accept an object that allows any of the props listed in the table above, and applies them to the specified breakpoint (and above).

    <ParamField path="sm" type="GridProps">
      `>=640px`
    </ParamField>

    <ParamField path="md" type="GridProps">
      `>=768px`
    </ParamField>

    <ParamField path="lg" type="GridProps">
      `>=1024px`
    </ParamField>

    <ParamField path="xl" type="GridProps">
      `>=1280px`
    </ParamField>

    <ParamField path="xxl" type="GridProps">
      `>=1536px`
    </ParamField>
  </Tab>
</Tabs>
