---
title: Section for Astro
description: "Astro section parts for page headings, content, and actions."
seo:
    title: Section for Astro | Shadcn & Tailwind CSS
related:
    - /blocks
    - /components/layout
sidebar:
    label: "Section"
---

Build the heading with `SectionTitle` and put its explanation in `SectionDescription`. Set `as` on the title to fit the page heading order. Standard HTML attributes pass through both parts.

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

<Section id="services">
  <SectionContainer>
    <SectionTitle>Services</SectionTitle>
    <SectionDescription>How we can help.</SectionDescription>
  </SectionContainer>
</Section>
```

For full page examples, browse the [block examples](/blocks).

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

<Section>
  <SectionContainer class="gap-6">
    <div class="flex max-w-2xl flex-col gap-3">
      <p class="text-primary text-sm font-medium">Launch checklist</p>
      <h2 class="text-3xl font-semibold tracking-tight">
        Plan the next release
      </h2>
      <p class="text-muted-foreground leading-7">
        Keep copy, actions, and supporting details aligned in one predictable
        section.
      </p>
    </div>
    <div class="border-border grid gap-3 border-t pt-6 @min-2xl/section:grid-cols-3">
      <p class="text-sm">
        <span class="font-medium">01</span> Scope
      </p>
      <p class="text-sm">
        <span class="font-medium">02</span> Review
      </p>
      <p class="text-sm">
        <span class="font-medium">03</span> Ship
      </p>
    </div>
    <div class="flex flex-wrap gap-3">
      <Button>Review plan</Button>
      <Button variant="outline">Open docs</Button>
    </div>
  </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.

#### src/components/ui/section/section-variants.ts

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

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

export const sectionVariants = (...args: Parameters<typeof sectionStyles>) =>
    cn(sectionStyles(...args))

export type SectionVariantProps = VariantProps<typeof sectionVariants>
```

#### 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 SectionHeading } from './section-heading.astro'
export { default as SectionTitle } from './section-title.astro'
export { sectionVariants, type SectionVariantProps } from './section-variants'
```

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

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

import { cn } from "cn"

type Props = HTMLAttributes<"div">

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

<div
  data-slot="section-container"
  class={cn(
    "relative mx-auto flex w-full max-w-7xl flex-col gap-8 px-4 @container/section",
    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-balance", className)}
  {...props}
>
  <slot />
</p>
```

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

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

import { cn } from "cn"

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

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

<Tag
  data-slot="section-heading"
  class={cn("w-full @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 "./section-variants"

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

```ts
import { Section, SectionContainer } from "@/components/ui/section"
```

```astro
<Section>
  <SectionContainer>
    <h2 class="text-2xl font-semibold">Release notes</h2>
    <p>Use the container for the section content you want aligned.</p>
  </SectionContainer>
</Section>
```

## Examples

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

<Section variant="floating">
  <SectionContainer class="gap-6">
    <div class="flex max-w-2xl flex-col gap-3">
      <p class="text-primary text-sm font-medium">Customer story</p>
      <h2 class="text-3xl font-semibold tracking-tight">
        A quieter layout for long-form copy
      </h2>
      <p class="text-muted-foreground leading-7">
        Floating sections frame a focused slice of content without changing the
        page width contract.
      </p>
    </div>
    <blockquote class="border-primary/40 text-muted-foreground border-l-2 pl-4 text-sm leading-6">
      "The section feels separate from the surrounding page, while still using
      the same layout rhythm."
    </blockquote>
  </SectionContainer>
</Section>
```

## Notes

- `Section` controls spacing and surface treatment.
- `SectionContainer` keeps the inner width and horizontal padding consistent.
- Use `class` on `Section` when a specific page needs custom vertical spacing.

## API Reference

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

## When to use Section

Use Section to give one page topic a clear boundary. Compose its heading and content parts, then pass content through props or slots in a block. Keep route data loading in the template.

## Keyboard and behavior

Use a heading that describes the section and fits the page hierarchy. Add an ID when navigation links target it. A section does not need extra interactive behavior to be useful.

## Theme and layout

Default and floating variants control spacing and surface treatment. Check the gap between adjacent sections so two sets of padding do not create unwanted space. Theme changes should apply through shared tokens.
