Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Section for Astro

Astro section parts for page headings, content, and actions.

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.

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

Launch checklist

Plan the next release

Keep copy, actions, and supporting details aligned in one predictable section.

01 Scope

02 Review

03 Ship

Installation

bunx --bun shadcn@latest add @gnostic/section
npx shadcn@latest add @gnostic/section
pnpm dlx shadcn@latest add @gnostic/section

Component classes: Default Gnostic.

Complete the Installation guide first. Then copy the component files below.

No extra packages are required.

Copy the files

src/components/ui/section/section-variants.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
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
---
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
---
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
---
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
---
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
---
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>

Usage

import { Section, SectionContainer } from "@/components/ui/section"
<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

Customer story

A quieter layout for long-form copy

Floating sections frame a focused slice of content without changing the page width contract.

"The section feels separate from the surrounding page, while still using the same layout rhythm."

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