---
title: Layout for Astro
description: "Astro document parts for the page shell, head, body, and main content."
seo:
    title: Layout for Astro | Shadcn & Tailwind CSS
related:
    - /components/section
    - /components/header
sidebar:
    label: "Layout"
---

Use the `Layout` parts in a document shell or a template layout. The framed examples show how those parts arrange a page with its own document structure.

```astro
---
import MenuIcon from "@lucide/astro/icons/menu"

import logo from "@/assets/gnostic-ai-logo.svg"
import { Button } from "@/components/ui/button"
import { HeaderContainer, Header, HeaderGroup } from "@/components/ui/header"
import {
  Layout,
  LayoutBody,
  LayoutHead,
  LayoutMain,
} from "@/components/ui/layout"
import { Logo, LogoImage, LogoText } from "@/components/ui/logo"
---

<Layout>
  <LayoutHead
    name="Gnostic UI"
    title="Free Astro components and blocks"
    noindex
  >
    <slot />
  </LayoutHead>
  <LayoutBody>
    <Header variant="floating" class="bg-card">
      <HeaderContainer class="justify-between">
        <HeaderGroup>
          <Logo>
            <LogoImage alt="Gnostic UI" src={logo} />
            <LogoText>
              gnostic <span class="text-muted-foreground">ui</span>
            </LogoText>
          </Logo>
        </HeaderGroup>
        <HeaderGroup>
          <nav class="flex items-center gap-4 @max-sm/header:hidden">
            <a href="" class="text-muted-foreground text-sm font-medium">
              Docs
            </a>
            <a href="" class="text-muted-foreground text-sm font-medium">
              Components
            </a>
            <a href="" class="text-muted-foreground text-sm font-medium">
              Blocks
            </a>
          </nav>
          <Button
            variant="ghost"
            size="icon"
            class="hidden @max-sm/header:inline-flex"
          >
            <MenuIcon />
          </Button>
        </HeaderGroup>
      </HeaderContainer>
    </Header>
    <LayoutMain class="mx-auto max-w-(--container) p-6">
      <h1 class="text-2xl font-semibold @2xl/main:text-4xl @5xl/main:text-5xl">
        Layout Document Example
      </h1>
      <p class="text-muted-foreground mt-3">
        The main content of an Astro page.
      </p>
    </LayoutMain>
  </LayoutBody>
</Layout>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/layout
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

No extra packages are required.

#### src/components/ui/layout/index.ts

```ts
export { default as Layout } from './layout.astro'
export { default as LayoutBody } from './layout-body.astro'
export { default as LayoutHead } from './layout-head.astro'
export { default as LayoutMain } from './layout-main.astro'
export { default as Seo } from './seo.astro'

export type { LayoutProps, SeoProps } from './types.ts'
```

#### src/components/ui/layout/layout-body.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"body">

const { class: className, ...props } = Astro.props
---

<body
  class={cn(
    "bg-background text-foreground font-sans overscroll-none antialiased",
    className,
  )}
  data-slot="layout-body"
  {...props}
>
  <slot />
</body>
```

#### src/components/ui/layout/layout-head.astro

```astro
---
import Seo from "./seo.astro"
import type { SeoProps } from "./types.ts"

type Props = SeoProps
---

<head data-slot="layout-head">
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="generator" content={Astro.generator} />
  <Seo {...Astro.props} />
  <slot />
</head>
```

#### src/components/ui/layout/layout-main.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"main">

const { class: className, ...props } = Astro.props
---

<main
  data-slot="layout-main"
  class={cn(
    "@container/main flex flex-col in-data-[slot=sidebar-inset]:rounded-[inherit] not-in-data-[slot=sidebar-inset]:w-full",
    className,
  )}
  {...props}
>
  <slot />
</main>
```

#### src/components/ui/layout/layout.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"

import type { LayoutProps } from "./types.ts"

type Props = HTMLAttributes<"html"> & LayoutProps

const { class: className, lang, style, containerSize, ...props } = Astro.props

let rootStyle = style
if (containerSize) {
  rootStyle =
    typeof style === "string"
      ? `${style}; --container: ${containerSize}`
      : { ...style, "--container": containerSize }
}
---

<html
  class={cn(
    "has-data-[slot=sidebar]:data-[variant=inset]:bg-sidebar overscroll-none scroll-smooth [--container:var(--container-7xl)] motion-reduce:scroll-auto",
    className,
  )}
  style={rootStyle}
  data-slot="layout"
  lang={Astro.currentLocale || "en"}
  {...props}
>
  <slot />
</html>

<style is:global>
  :root {
    color-scheme: light;
  }

  .dark {
    color-scheme: dark;
  }

  @view-transition {
    navigation: auto;
  }
</style>
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```astro PageLayout.astro
---
import { Layout, LayoutBody, LayoutHead, LayoutMain } from "@/components/ui/layout"
---

<Layout lang="en">
    <LayoutHead name="gnostic ui" title="Page title" description="Page description" />
    <LayoutBody>
        <LayoutMain>
            <slot />
        </LayoutMain>
    </LayoutBody>
</Layout>
```

## Responsive layout

Set `containerSize` on `Layout` to change the maximum section content width. For example, `<Layout containerSize="64rem">` sets `--container` to `64rem` on the document root. The default is Tailwind's `var(--container-7xl)`.

`LayoutMain` uses `@container/main` to create a responsive layout.

```astro
---
import { LayoutMain } from "@/components/ui/layout"
---

<LayoutMain>
  <h1 class="text-[clamp(var(--text-3xl),1.5rem+3cqi,var(--text-6xl))] font-semibold">
    Responsive layout
  </h1>
  <p class="text-[clamp(var(--text-base),0.5rem+2cqi,var(--text-xl))] text-muted-foreground mt-3">
    Adjust the container width to see the text size change.
  </p>
</LayoutMain>
```

## Notes

- `Layout` owns the root `<html>` element and global visual baseline.
- `LayoutHead` centralizes metadata, canonical URLs, and social tags.
- `Seo` supplies the same metadata inside an existing `<head>`. Import it from `@/components/ui/layout/seo.astro`.
- `LayoutBody` and `LayoutMain` provide the default body and page-content wrappers.
- `LayoutMain` is a neutral structural wrapper for page content.

## API Reference

`LayoutHead` and `Seo` share `SeoProps` from `layout/types.ts`. Both require a site name (`name`) and a page title (`title`). Each value must contain text.

### LayoutProps

<AutoTypeTable path="../../packages/ui/src/components/ui/layout/types.ts" name="LayoutProps" />

### SeoProps

<AutoTypeTable path="../../packages/ui/src/components/ui/layout/types.ts" name="SeoProps" />

Read the [installed source](/ui/r/layout.json) for the complete prop types.

## When to use Layout

Use Layout parts in the site template that owns the route. Set the document language and provide page metadata in the head. Keep reusable blocks inside the main content rather than making them own the document.

## Keyboard and behavior

A page needs one main landmark and a logical heading order. Add a skip link when repeated navigation comes before the main content. These shell parts do not infer titles or canonical URLs from arbitrary content.

## Theme and layout

Import the shared stylesheet once in your site layout. `LayoutBody` applies the preset background, text color, sans font, and font smoothing to the body. Theme and page background choices belong at this level. Avoid a second html or body element inside a reusable block.
