---
title: Banner for Astro
description: "Astro announcement containers with optional dismissal and stored visibility."
seo:
    title: Banner for Astro | Shadcn & Tailwind CSS
related:
    - /components/alert
    - /components/header
sidebar:
    label: "Banner"
---

## Usage

```ts
import { Banner, BannerContainer } from "@/components/ui/banner"
```

```astro
<Banner>
  <BannerContainer>
    <p class="text-sm font-medium">Banner Title</p>
    <p class="text-sm opacity-90">Banner Description</p>
  </BannerContainer>
</Banner>
```

`BannerContainer` renders a close button by default. Set `showClose={false}` to
hide it.

```astro
<Banner>
  <BannerContainer showClose={false}>
    <p class="text-sm font-medium">Banner Title</p>
    <p class="text-sm opacity-90">Banner Description</p>
  </BannerContainer>
</Banner>
```

## Examples

```astro
---
import { Banner, BannerContainer } from "@/components/ui/banner"
---

<Banner>
  <BannerContainer>
    <p class="text-sm font-medium">New docs are live</p>
    <p class="text-sm opacity-90">
      Browse the latest component examples and installation guides.
    </p>
  </BannerContainer>
</Banner>
```

## Installation

### Command

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

### Manual

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

No extra packages are required.

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

```astro
---
import type { HTMLAttributes } from "astro/types"
import XIcon from "@lucide/astro/icons/x"

import { cn } from "cn"

type Props = HTMLAttributes<"div"> & {
  showClose?: boolean
}

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

<div
  class={cn(
    "relative mx-auto flex w-full max-w-7xl gap-4 px-4",
    showClose && "pr-10",
    className,
  )}
  {...props}
>
  <slot />
  {showClose && (
    <button
      data-slot="banner-close"
      type="button"
      class="focus-visible:ring-ring/50 absolute top-1/2 right-4 z-20 inline-flex shrink-0 -translate-y-1/2 items-center justify-center rounded-sm text-current/65 transition-colors outline-none hover:text-current focus-visible:text-current focus-visible:ring-2 disabled:pointer-events-none disabled:opacity-50 in-data-[variant=floating]:right-5"
      aria-label="Close"
    >
      <XIcon class="size-4" />
      <span class="sr-only">Close</span>
    </button>
  )}
</div>
```

#### src/components/ui/banner/banner.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import { cva, type VariantProps } from "class-variance-authority"

import { cn } from "cn"

type Props = HTMLAttributes<"aside"> &
  VariantProps<typeof variants> & {
    storageKey?: string
  }

const variants = cva("@container relative flex items-center py-2", {
  variants: {
    variant: {
      default: "bg-foreground text-background w-full",
      floating: "bg-background mx-auto my-2 w-[calc(100%-2rem)] max-w-[calc(var(--container,1280px)-2rem)] overflow-hidden rounded-lg border shadow-sm",
    },
  },
  defaultVariants: {
    variant: "default",
  },
})

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

<aside
  class={cn(variants({ variant }), className)}
  data-slot="banner"
  data-banner-storage-key={storageKey}
  data-variant={variant}
  {...props}
>
  <slot />
</aside>

<script>
  const getStorageToken = (key: string) => `banner:${key}`

  const syncBanners = () => {
    document.querySelectorAll("[data-slot='banner']").forEach((banner) => {
      if (!(banner instanceof HTMLElement)) return
      const storageKey = banner.dataset.bannerStorageKey
      if (
        storageKey &&
        sessionStorage.getItem(getStorageToken(storageKey)) === "hidden"
      ) {
        banner.hidden = true
      }
    })
  }

  syncBanners()
  document.addEventListener("astro:page-load", syncBanners)

  document.addEventListener("click", (event) => {
    if (!(event.target instanceof Element)) return
    const closeButton = event.target.closest("[data-slot='banner-close']")
    const banner = closeButton?.closest("[data-slot='banner']")
    if (!(banner instanceof HTMLElement)) return
    banner.hidden = true
    const storageKey = banner.dataset.bannerStorageKey
    if (storageKey)
      sessionStorage.setItem(getStorageToken(storageKey), "hidden")
  })
</script>
```

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

```ts
export { default as Banner } from "./banner.astro"
export { default as BannerContainer } from "./banner-container.astro"
```

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.


```astro
---
import { Banner, BannerContainer } from "@/components/ui/banner"
---

<Banner variant="floating" storageKey="docs-floating-banner">
  <BannerContainer>
    <p class="text-sm font-medium">Launch</p>
    <p class="text-sm opacity-90">New component docs are ready.</p>
  </BannerContainer>
</Banner>
```

## Notes

- The default banner spans its available width.
- Set `variant="floating"` for a bar with an inset surface.

## API Reference

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

## When to use Banner

Place a Banner near the top of the page for a service notice or current announcement. Use the default or floating layout to suit the page shell, then add message and action parts through slots.

## Keyboard and behavior

A dismissal control needs a clear accessible name. If you use `storageKey`, the client behavior can retain the dismissed state; change the key when the notice itself changes. Important information must also remain available elsewhere after dismissal.

## Theme and layout

Check long text beside the action at a narrow width. Shared background and border tokens keep the banner consistent with the page, but a persistent banner must not cover a focused control or heading.
