---
title: Section for Astro
description: "Maintain consistent styles across your pages with the Section component."
seo:
    title: Section for Astro | Shadcn & Tailwind CSS
related:
    - /blocks
    - /components/layout
sidebar:
    label: "Section"
---

```astro
---
import {
  Section,
  SectionContainer,
  SectionDescription,
  SectionHeader,
  SectionTitle,
} from "@/components/ui/section"
---

<Section>
  <SectionContainer>
    <SectionHeader class="text-center">
      <p class="text-primary text-sm font-medium">Launch checklist</p>
      <SectionTitle>Plan the next release</SectionTitle>
      <SectionDescription>
        Set the scope, review the changes, and choose a release date.
      </SectionDescription>
    </SectionHeader>
    <ol class="flex list-inside list-decimal flex-wrap justify-center gap-x-8 gap-y-2 text-sm">
      <li>Set scope</li>
      <li>Review changes</li>
      <li>Ship release</li>
    </ol>
  </SectionContainer>
</Section>
```

## Installation

### Command

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

### Manual

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

No extra packages are required.

#### Check your global Tailwind stylesheet

Add these variables to your Tailwind theme for responsive section title sizing.

```css
@theme inline {
    --text-section-title: clamp(
        calc(var(--typeset-size, 1rem) * var(--heading-min, 1.875)),
        calc(var(--typeset-size, 1rem) + 4cqi),
        calc(var(--typeset-size, 1rem) * var(--heading-max, 3))
    );
    --text-section-title--line-height: 1.2;
}
```

#### src/components/ui/section/types.ts

```ts
import { cva, type VariantProps } from 'class-variance-authority'

export const sectionVariants = cva("relative flex scroll-mt-12 flex-col gap-14 py-16 @container", {
    variants: {
        variant: {
            default: 'bg-background w-full',
            floating: "bg-background mx-auto my-16 w-[calc(100%-2rem)] max-w-[calc(var(--container,var(--container-7xl))-2rem)] overflow-hidden rounded-xl border shadow-md",
        },
    },
    defaultVariants: {
        variant: 'default',
    },
})

export const sectionContainerVariants = cva(
    '@container/section relative mx-auto w-full max-w-(--container,var(--container-7xl)) px-4',
    {
        variants: {
            variant: {
                default: 'flex flex-col items-center gap-8',
                split: 'grid grid-cols-1 items-center gap-14 @2xl:grid-cols-[minmax(0,1fr)_minmax(0,1.15fr)]',
                responsive:
                    'flex flex-col items-baseline justify-between gap-6 @sm:flex-row',
            },
            order: {
                default: '',
                reverse: '*:first:order-last',
            },
        },
        defaultVariants: {
            variant: 'default',
            order: 'default',
        },
    },
)

export type SectionVariantProps = VariantProps<typeof sectionVariants>

type ContainerVariants = VariantProps<typeof sectionContainerVariants>

/** Layout options for the content inside a section. */
export interface SectionContainerProps {
    /** Stack the children, use two columns, or put them in a responsive row.
     * @default 'default'
     */
    variant?: ContainerVariants['variant']
    /** Move the first child to the last visual position with `reverse`.
     * @default 'default'
     */
    order?: ContainerVariants['order']
}
```

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

```ts
export { default as Section } from './section.astro'
export { default as SectionContainer } from './section-container.astro'
export { default as SectionDescription } from './section-description.astro'

export {
    default as /** @deprecated Use SectionHeader instead. */ SectionHeading,
} from './section-header.astro'

export { default as SectionHeader } from './section-header.astro'
export { default as SectionTitle } from './section-title.astro'

export type { SectionVariantProps, SectionContainerProps } from './types.ts'
export { sectionVariants, sectionContainerVariants } from './types.ts'
```

#### src/components/ui/section/section-container.astro

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

import { cn } from "cn"

import {
  sectionContainerVariants,
  type SectionContainerProps,
} from "./types.ts"

type Props = HTMLAttributes<"div"> & SectionContainerProps

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

<div
  data-slot="section-container"
  class={cn(sectionContainerVariants({ variant, order }), className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/section/section-description.astro

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

import { cn } from "cn"

type Props = HTMLAttributes<"p">

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

<p
  data-slot="section-description"
  class={cn("text-muted-foreground text-lg leading-relaxed text-pretty", className)}
  {...props}
>
  <slot />
</p>
```

#### src/components/ui/section/section-header.astro

```astro
---
import type { HTMLTag, Polymorphic } from "astro/types"

import { cn } from "cn"

type Props<Tag extends HTMLTag = "header"> = Polymorphic<{ as: Tag }>

const { as: Tag = "header", class: className, ...props } = Astro.props
---

<Tag
  data-slot="section-heading"
  class={cn("w-full not-in-data-[variant=floating]:max-w-6xl not-in-data-[variant=floating]:mx-auto flex flex-col gap-3 @container/section-heading", className)}
  {...props}
>
  <slot />
</Tag>
```

#### src/components/ui/section/section-title.astro

```astro
---
import type { HTMLTag, Polymorphic } from "astro/types"

import { cn } from "cn"

type Props<Tag extends HTMLTag = "h2"> = Polymorphic<{ as: Tag }>

const { as: Tag = "h2", class: className, ...props } = Astro.props
---

<Tag
  data-slot="section-title"
  class={cn("font-semibold tracking-tight text-balance text-section-title font-heading", className)}
  {...props}
>
  <slot />
</Tag>
```

#### src/components/ui/section/section.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import { sectionVariants, type SectionVariantProps } from "./types.ts"

import { cn } from "cn"

type Props = HTMLAttributes<"section"> & SectionVariantProps

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

<section
  class={cn(sectionVariants({ variant }), className)}
  data-slot="section"
  data-variant={variant}
  {...props}
>
  <slot />
</section>
```

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

`Section` sets the background and vertical spacing. The default `SectionContainer` centers its children and stacks them in a column. Use one container when the header and body share a layout. Add a second container when a group needs its own width or alignment.

```astro
---
import {
  Section,
  SectionContainer,
  SectionDescription,
  SectionHeader,
  SectionTitle,
} from "@/components/ui/section"
---

<Section>
  <SectionContainer class="max-w-2xl text-center">
    <SectionHeader>
      <SectionTitle>Projects</SectionTitle>
      <SectionDescription>Recent work from the studio.</SectionDescription>
    </SectionHeader>
  </SectionContainer>
  <SectionContainer variant="split">
    <slot />
  </SectionContainer>
</Section>
```

`SectionHeader` renders a `<header>`. Put an optional link or button in its default slot. `SectionHeading` is a deprecated alias for `SectionHeader`.

## Examples

### Floating section

Use `variant="floating"` on `Section` to inset the whole section and give it a border and rounded corners.

```astro
---
import {
  Section,
  SectionContainer,
  SectionDescription,
  SectionHeader,
  SectionTitle,
} from "@/components/ui/section"
---

<Section variant="floating" class="bg-muted">
  <SectionContainer>
    <SectionHeader>
      <SectionTitle>Customer story</SectionTitle>
      <SectionDescription>
        How a small team keeps track of each release.
      </SectionDescription>
    </SectionHeader>
    <blockquote class="w-full border-l-2 border-primary pl-4">
      “Now we can see who reviews each change and when it ships.”
    </blockquote>
  </SectionContainer>
</Section>
```

### Split container

Use `variant="split"` on `SectionContainer` to place an image group beside the section header. The two groups stack until the section reaches the `@2xl` container width. Set `order="reverse"` to move the first group to the end of the visual layout.

```astro
---
import { Image } from "astro:assets"
import orion from "@/assets/images/winter-landscape-orion.png"
import theta from "@/assets/images/winter-landscape-theta.png"
import storm from "@/assets/images/futuristic-storm.png"
import {
  Section,
  SectionContainer,
  SectionDescription,
  SectionHeader,
  SectionTitle,
} from "@/components/ui/section"
---

<Section>
  <SectionContainer variant="split">
    <Image
      src={storm}
      alt="A storm gathers around a tower on the plain."
      width={720}
      class="aspect-video w-full rounded-lg object-cover"
    />
    <SectionHeader>
      <SectionTitle>Distant worlds</SectionTitle>
      <SectionDescription>
        Three landscape studies of winter light and passing storms.
      </SectionDescription>
    </SectionHeader>
  </SectionContainer>
</Section>
```

### Responsive container

Use `variant="responsive"` on `SectionContainer` to put a header and an action on the same row. They stack when the section is narrower than the `@sm` container width. Resize the preview to see the change.

```astro
---
import { Button } from "@/components/ui/button"
import {
  Section,
  SectionContainer,
  SectionDescription,
  SectionHeader,
  SectionTitle,
} from "@/components/ui/section"
---

<Section>
  <SectionContainer variant="responsive">
    <SectionHeader>
      <SectionTitle>Studio visits</SectionTitle>
      <SectionDescription>
        Book a tour of the print workshop.
      </SectionDescription>
    </SectionHeader>
    <Button as="a" href="mailto:visits@example.com">
      Request a visit
    </Button>
  </SectionContainer>
</Section>
```

## API Reference

`Section` accepts `variant="default"` or `variant="floating"`. Omit the prop for the default surface.

<AutoTypeTable
    path="../../packages/ui/src/components/ui/section/types.ts"
    name="SectionContainerProps"
/>

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

## Keyboard and behavior

`SectionTitle` renders an `<h2>`. Set its `as` prop to match the page heading order. Add an ID to the section when a navigation link points to it.

## Theme and layout

- `SectionTitle` uses the Tailwind utility `text-section-title`. With the default `--typeset-size` of `1rem`, the title scales from `1.875rem` to `3rem` with the container width. Its line height is `1.2`.
- Set `--heading-min` and `--heading-max` on a title to change its size limits. For example, `<SectionTitle class="[--heading-min:2] [--heading-max:4]">` sets the limits to two and four times the base size. The **Manual** tab under [Installation](#installation) includes the global CSS for this utility.
- Set `--container` to change the maximum container width. Its default is Tailwind's `--container-7xl`.
- Set `class` on `Section` to adjust its vertical spacing.
- Use `<Section class="inverted">` to swap the surface and text colors. Children inherit the swapped color pairs.
