> ## 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.

# Layout – Code

> Define page layout structure and columns.

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="layout-playground" fullWidth screenshot>
      ```tsx lines expandable theme={null}
      import { Layout, Card } from "@servicetitan/anvil2";

      function App() {
        const cardProps = {
          background: "stronger" as const,
          style: { width: "100%" },
        };
        const createArray = (number: number) => [...new Array(number)];

        return (
          <div style={{ width: "calc(100vw - 4rem)" }}>
            <Layout variant="narrow">
              <Layout.Item span={12}>
                <Card {...cardProps}>span=12</Card>
              </Layout.Item>
              {createArray(2).map((_, i) => (
                <Layout.Item key={i} span={6}>
                  <Card {...cardProps}>span=6</Card>
                </Layout.Item>
              ))}
              {createArray(3).map((_, i) => (
                <Layout.Item key={i} span={4}>
                  <Card {...cardProps}>span=4</Card>
                </Layout.Item>
              ))}
              {createArray(4).map((_, i) => (
                <Layout.Item key={i} span={3}>
                  <Card {...cardProps}>span=3</Card>
                </Layout.Item>
              ))}
              {createArray(6).map((_, i) => (
                <Layout.Item key={i} span={2}>
                  <Card {...cardProps}>span=2</Card>
                </Layout.Item>
              ))}
              {createArray(12).map((_, i) => (
                <Layout.Item key={i} span={1}>
                  <Card justifyContent="center" {...cardProps}>
                    1
                  </Card>
                </Layout.Item>
              ))}
            </Layout>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ## Common Examples

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

    const ExampleComponent = () => (
      <Layout>
        <Layout.Item span={12}>{/* full-width content... */}</Layout.Item>
        <Layout.Item span={3}>{/* one-quarter-width content... */}</Layout.Item>
        <Layout.Item span={9}>{/* three-quarter-width content... */}</Layout.Item>
      </Layout>
    );
    ```

    ### Using layouts in pages

    In general, the `Layout` component is intended to be used as the child of a [Page.Content](/docs/web/components/page/code#page-components/design) component. Using these components together creates the foundation of page layouts in Anvil2.

    <LiveCode showCode example="layout-page-full-page" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Layout, Page, Card, Text } from "@servicetitan/anvil2";

      function App() {
        return (
          <div style={{ width: "100vw", height: "100vh" }}>
            <Page style={{ margin: "-1.5rem" }}>
              <Page.Sidebar>
                <Page.SidebarHeader>
                  <Text variant="headline" el="h2">
                    Sidebar
                  </Text>
                </Page.SidebarHeader>
              </Page.Sidebar>
              <Page.Content>
                <Layout variant="narrow">
                  <Layout.Item span={12}>
                    <Card
                      background="strong"
                      justifyContent="center"
                      alignItems="center"
                      style={{ width: "100%", height: "12rem" }}
                    >
                      <Text variant="headline" el="h2">
                        Layout
                      </Text>
                    </Card>
                  </Layout.Item>
                </Layout>
              </Page.Content>
            </Page>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Fixed and fluid layouts

    By default, layouts are fixed, meaning that they have a maximum width of `1280px` and are centered on the page. On smaller screens, they become fluid and fill the width of the page.

    ```tsx theme={null}
    // max-width: 1280px
    <Layout>
      <Layout.Item span={12}>{/* content... */}</Layout.Item>
    </Layout>
    ```

    To make a layout fluid on larger screens, set the `fluid` prop on the `Layout` component to `true`.

    ```tsx theme={null}
    // max-width: none
    <Layout fluid>
      <Layout.Item span={12}>{/* content... */}</Layout.Item>
    </Layout>
    ```

    ### Responsive layouts

    Layout items come with responsive styles baked-in. As the parent layout becomes more narrow, the layout items will adjust accordingly based on the [number of columns available in that breakpoint](/docs/web/components/layout/design#columns-per-breakpoint).

    For example, a `Layout.Item` with `span={4}` will take up a third of the width on breakpoints `lg`, `xl`, and `xxl`, two-thirds on the `md` breakpoint, and full-width on `sm` breakpoint and below.

    **Note –** these breakpoints correspond to the size of the layout rather than the screen, using container queries. Sidebars and panels may also affect the number of columns displayed.

    The `Layout.Item` also features responsive props, which can be used to override the `span` value at specific breakpoints. See the [Layout.Item Props](#layout-item-props) for details.

    ### Layout density variants

    Use the `variant` prop of the `Layout` component to control the gap between the layout items. Note that the gap will also change based on the width of the layout.

    #### Narrow

    <LiveCode showCode example="layout-variant-narrow" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Layout, Card } from "@servicetitan/anvil2";

      function App() {
        return (
          <div style={{ width: "calc(100vw - 4rem)" }}>
            <Layout variant="narrow">
              {[...new Array(4)].map((_, i) => (
                <Layout.Item key={i} span={2}>
                  <Card background="strong" style={{ width: "100%" }} />
                </Layout.Item>
              ))}
            </Layout>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    #### Default

    <LiveCode showCode example="layout-variant" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Layout, Card } from "@servicetitan/anvil2";

      function App() {
        return (
          <div style={{ width: "calc(100vw - 4rem)" }}>
            <Layout>
              {[...new Array(4)].map((_, i) => (
                <Layout.Item key={i} span={2}>
                  <Card background="strong" style={{ width: "100%" }} />
                </Layout.Item>
              ))}
            </Layout>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    #### Wide

    <LiveCode showCode example="layout-variant-wide" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Layout, Card } from "@servicetitan/anvil2";

      function App() {
        return (
          <div style={{ width: "calc(100vw - 4rem)" }}>
            <Layout variant="wide">
              {[...new Array(4)].map((_, i) => (
                <Layout.Item key={i} span={2}>
                  <Card background="strong" style={{ width: "100%" }} />
                </Layout.Item>
              ))}
            </Layout>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ## Best Practices

    * Use the `Layout` component as the first child of the [Page.Content](/docs/web/components/page/code#page-components/) component.
    * Use layouts to appropriately divide a page into columns, then use the [Grid](/docs/web/components/grid/code/) and [Flex](/docs/web/components/flex/code/) components to create smaller UI layouts within the content of the page.
  </Tab>

  <Tab title="Layout Props">
    ```tsx theme={null}
    <Layout fluid={false} variant="default">
      <Layout.Item span={12}>Content</Layout.Item>
    </Layout>
    ```

    ## `Layout` Props

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

    <ParamField path="fluid" type="boolean" default="false" />

    <ParamField path="variant" type={`"default" | "narrow" | "wide"`} default="default" />
  </Tab>

  <Tab title="Layout.Item Props">
    ```tsx theme={null}
    <Layout.Item span={12} xs={12} sm={6} md={4} lg={3} xl={2} xxl={1}>
      Content
    </Layout.Item>
    ```

    ## `Layout.Item` Props

    The `Layout.Item` component can accept any valid HTML `div` props, as well as the following:

    <ParamField path="span" type="0 - 12" default="12">
      Override span value below sm breakpoint
    </ParamField>

    <ParamField path="xs" type="0 - 12">
      Override `span` value below `sm` breakpoint
    </ParamField>

    <ParamField path="sm" type="0 - 12">
      Override `span` value for `sm` breakpoint
    </ParamField>

    <ParamField path="md" type="0 - 12">
      Override `span` value for `md` breakpoint
    </ParamField>

    <ParamField path="lg" type="0 - 12">
      Override `span` value for `lg` breakpoint
    </ParamField>

    <ParamField path="xl" type="0 - 12">
      Override `span` value for `xl` breakpoint
    </ParamField>

    <ParamField path="xxl" type="0 - 12">
      Override `span` value for `xxl` breakpoint
    </ParamField>
  </Tab>
</Tabs>
