Badge
updatedA small label with Button's variants and colours, five sizes, side-aware icon padding and a square form.
Open Props in the corner of the example to try variant, color, size and square. xs to xl are 16 to 32px tall, four pixels apart, and the radius grows with the size.
variant and color are Button's: solid, soft, subtle, outline, ghost and link in primary, neutral, destructive, success, warning and info. A badge has no hover or pressed state; link underlines on hover only because it is meant to be rendered as a link.
Badges read the same colour variables as buttons, so a colour declared for a button works on a badge too: <Badge color="brand">.
Installation
Usage
<script setup lang="ts">
import { Badge } from '@/components/ui/badge'
</script>
<template>
<Badge variant="soft" color="success">Active</Badge>
</template>
Examples
Icons
As in Button, an icon says which side it sits on with data-icon="inline-start" or data-icon="inline-end", and the badge tightens its padding on that side only. Icons without a size-* class take the size's icon size.
Square
square gives the badge equal padding on all sides and at least its height as width: a single digit or an icon is a square, and a longer count keeps even padding. Give an icon-only badge an aria-label.
As a link
as renders another element; as-child merges the badge onto the single element you pass.
Touch target
Smaller than a finger? Add touch-target: expand when it stands alone, wrapper when it sits close to others. How it works.
Only a badge you can tap needs it, one rendered as a link.
API Reference
Badge
class is also accepted and merged over the variant classes with tailwind-merge.
| Prop | Default | Type and description |
|---|---|---|
| variant | "solid" | NonNullable<"solid" | "soft" | "subtle" | "outline" | "ghost" | "link" | null | undefined>Appearance, independent of colour. |
| color | "primary" | BadgeColor | (string & {})Colour role the variant is drawn in. |
| size | "md" | BadgeSizeHeight, padding, text, icon size and radius, from 16px for |
| square | false | booleanRender the badge with equal padding on all sides. A single character or icon becomes a square. |
| touchTarget | "none" | NonNullable<"none" | "expand" | "wrapper" | null | undefined>Grows the pressable area to at least 48px without changing how the badge looks. |
| asChild | — | booleanRender the single child instead of a wrapper, merging the badge's attributes and classes onto it. |
| as | "span" | AsTag | ComponentThe element or component to render. |
| Slot | Scope and description |
|---|---|
| default | {}Label and icons. Mark an icon with |
class is also accepted and merged over the variant classes with tailwind-merge.
Props
- variant
"solid" NonNullable<"solid" | "soft" | "subtle" | "outline" | "ghost" | "link" | null | undefined>Appearance, independent of colour.linkunderlines on hover, for a badge rendered as a link.- color
"primary" BadgeColor | (string & {})Colour role the variant is drawn in.neutraluses the page's own foreground. Any other name picks up a colour you declare on[data-slot][data-color="<name>"].- size
"md" BadgeSizeHeight, padding, text, icon size and radius, from 16px forxsto 32px forxl.- square
false booleanRender the badge with equal padding on all sides. A single character or icon becomes a square.- touchTarget
"none" NonNullable<"none" | "expand" | "wrapper" | null | undefined>Grows the pressable area to at least 48px without changing how the badge looks.wrapperalso reserves that space in layout;expandmay overlap neighbours.- asChild
booleanRender the single child instead of a wrapper, merging the badge's attributes and classes onto it.- as
"span" AsTag | ComponentThe element or component to render.
Slots
- default
{}Label and icons. Mark an icon withdata-icon="inline-start"or"inline-end"to tighten padding on that side.
Data attributes
| data-slot | Attributes | Description |
|---|---|---|
| badge | data-variant, data-color, data-size | The root element, or your own element under as-child. |