---
title: Badge for Astro
description: "Small Astro labels for status, category, and counts."
seo:
    title: Badge for Astro | Shadcn & Tailwind CSS
related:
    - /components/alert
    - /components/button
sidebar:
    label: "Badge"
---

```astro
---
import BadgeCheck from "@lucide/astro/icons/badge-check"

import {
  Badge,
  type BadgeVariantProps,
  badgeVariants,
} from "@/components/ui/badge"

const badgeSizes = [
  "sm",
  "default",
  "lg",
] as const satisfies BadgeVariantProps["size"][]
const variants = [
  "default",
  "secondary",
  "success",
  "warning",
  "info",
  "destructive",
  "error",
  "outline",
  "ghost",
  "link",
] as const satisfies BadgeVariantProps["variant"][]

const badges = variants.flatMap((variant) =>
  badgeSizes.map((size) => ({ size, variant })),
)
---

<div class="flex flex-col items-center gap-4">
  <div class="inline-grid grid-cols-3 gap-3">
    {badges.map(({ size, variant }) => (
      <div class="flex items-center">
        <Badge
          size={size}
          variant={variant}
          as={variant === "link" ? "a" : undefined}
          href={variant === "link" ? "#" : undefined}
        >
          {variant}
        </Badge>
      </div>
    ))}
  </div>
</div>
```

## Installation

### Command

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

### Manual

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

No extra packages are required.

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

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

const badgeStyles = cva("font-sans focus-visible:ring-ring focus-visible:ring-offset-background relative inline-flex shrink-0 items-center justify-center gap-1 rounded-sm border border-transparent font-medium whitespace-nowrap transition-shadow outline-none focus-visible:ring-2 focus-visible:ring-offset-1 disabled:pointer-events-none disabled:opacity-64 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='opacity-'])]:opacity-80 [&_svg:not([class*='size-'])]:size-3.5 sm:[&_svg:not([class*='size-'])]:size-3 [button&,a&]:cursor-pointer [button&,a&]:pointer-coarse:after:absolute [button&,a&]:pointer-coarse:after:size-full [button&,a&]:pointer-coarse:after:min-h-11 [button&,a&]:pointer-coarse:after:min-w-11", {
    defaultVariants: {
        size: 'default',
        variant: 'default',
    },
    variants: {
        size: {
            default: "h-5.5 min-w-5.5 px-[calc(--spacing(1)-1px)] text-sm sm:h-4.5 sm:min-w-4.5 sm:text-xs",
            lg: "h-6.5 min-w-6.5 px-[calc(--spacing(1.5)-1px)] text-base sm:h-5.5 sm:min-w-5.5 sm:text-sm",
            sm: "h-5 min-w-5 rounded-[.25rem] px-[calc(--spacing(1)-1px)] text-xs sm:h-4 sm:min-w-4 sm:text-[.625rem]",
        },
        variant: {
            error: 'bg-destructive/8 text-destructive-foreground dark:bg-destructive/16',
            info: 'bg-info/8 text-info-foreground dark:bg-info/16',
            success: 'bg-success/8 text-success-foreground dark:bg-success/16',
            warning: 'bg-warning/8 text-warning-foreground dark:bg-warning/16',
            ghost: "text-foreground hover:bg-accent border-transparent",
            link: "text-primary border-transparent underline-offset-4 hover:underline",
            default: "bg-primary text-primary-foreground [button&,a&]:hover:bg-primary/90",
            destructive: "bg-destructive [button&,a&]:hover:bg-destructive/90 text-white",
            outline: "border-input bg-background text-foreground dark:bg-input/32 [button&,a&]:hover:bg-accent/50 dark:[button&,a&]:hover:bg-input/48",
            secondary: "bg-secondary text-secondary-foreground [button&,a&]:hover:bg-secondary/90",
        },
    },
})

export const badgeVariants = (...args: Parameters<typeof badgeStyles>) =>
    cn(badgeStyles(...args))

export type BadgeVariantProps = VariantProps<typeof badgeVariants>
```

#### src/components/ui/badge/badge.astro

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

import { cn } from "cn"

import { badgeVariants, type BadgeVariantProps } from "./badge-variants"

type Props<Tag extends HTMLTag> = Polymorphic<{ as: Tag }> & BadgeVariantProps

const {
  as: Tag = "span",
  class: className,
  variant = "default",
  size = "default",
  ...props
} = Astro.props
---

<Tag
  data-slot="badge"
  data-variant={variant}
  data-size={size}
  class={cn(badgeVariants({ variant, size }), className)}
  {...props}
>
  <slot />
</Tag>
```

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

```ts
export { badgeVariants, type BadgeVariantProps } from "./badge-variants"

export { default as Badge } from "./badge.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.


## Usage

```ts
import { Badge } from "@/components/ui/badge"
```

```astro
<Badge variant="default">Badge</Badge>
```

### Link

Use the polymorphic `as` prop to render the badge as a link element.

```astro
---
import { Badge } from "@/components/ui/badge"
---

<Badge as="a" href="/">Badge</Badge>
```

### Polymorphic

`Badge` is strict polymorphic. Pass `as` to change the rendered element and
use attributes for that element.

```astro
<Badge as="button" type="button">Action</Badge>
<Badge as="a" href="/docs">Docs</Badge>
```

## Notes

- Use variants first before adding custom classes.
- `Badge` works well for status labels, counts, and small inline metadata.

## With an icon

Put a check icon in the badge. The badge sets the icon size.

```astro
---
import Check from "@lucide/astro/icons/check"

import { Badge } from "@/components/ui/badge"
---

<div class="flex flex-wrap items-center gap-3">
  <Badge>
    <Check />
    Verified
  </Badge>
  <Badge variant="secondary">
    <Check />
    Verified
  </Badge>
  <Badge variant="outline">
    <Check />
    Verified
  </Badge>
</div>
```

## With a count

Add `rounded-full` when the badge shows a notification count.

```astro
---
import { Badge } from "@/components/ui/badge"
---

<div class="flex flex-wrap items-center gap-3">
  <Badge class="rounded-full tabular-nums">8</Badge>
  <Badge class="rounded-full tabular-nums" variant="destructive">
    3
  </Badge>
  <Badge class="rounded-full tabular-nums" variant="secondary">
    12
  </Badge>
</div>
```

## API Reference

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

## When to use Badge

Use Badge for a short fact attached to another object. Keep the text brief and choose a semantic variant only when the meaning is clear. The size prop changes density without requiring a different component.

## Keyboard and behavior

A badge is a label, not an action by default. Do not depend on its color alone to communicate status. If you make it a link through its supported element props, use a clear destination label and keep the focus style.

## Theme and layout

The variants share semantic color tokens with alerts and buttons. Test the small size with the actual label text. Long sentences belong in a description or alert instead of a badge.
