<!--
Source: https://www.theui.dev/docs/badge
Part of the theui-svelte documentation. Index: https://www.theui.dev/llms.txt
-->

# Badge

The Badge component shows a small count or a short label next to something else. It suits unread counts, status labels and new item markers.

## Example

To use the Badge component in your project, import the required component first. You can create **dot-badges** just using the `Badge` component or add text inside the `Badge` element to create a text badge. Here are some examples:

```html
<script>
  import { Badge } from "theui-svelte";
</script>

<Badge ariaTitle="Demo badge" />
<Badge>New</Badge>
```

## Grow Badge

By default, the font size of the Badge component is fixed, 0.8em, making it slightly smaller than regular text. However, you can make the font size inherit from te parent element by adding the `grow` attribute to the `Badge` component.

```html
<!-- Indicator badge -->
<Badge ariaTitle="Demo badge" />
<!-- Text badge -->
<Badge grow>...</Badge>
```

## Fixed Top Badge

You can position badges at fixed locations relative to their parent elements using the `fixed` attribute.

```html
<p class="relative p-2 bg-secondary">
  Notifications <Badge ariaTitle="3 unread" fixed />
</p>
<p class="relative p-2 bg-secondary">
  Notifications <Badge fixed>New</Badge>
</p>
```

## Rounded Corner

The `rounded` prop in the `Badge` component controls its border radius. Values: `none`, `sm`, `md`, `lg`, `xl`, `2xl`, `full`. Default is `full`.

```html
<Badge ... rounded="xl">New</Badge>
<Badge ... rounded="lg">New</Badge>
<Badge ... rounded="md">New</Badge>
<Badge ... rounded="sm">New</Badge>
<Badge ... rounded="none">New</Badge>
```

## Customization

To change the background color, add your own classes to the Badge. They are merged with the defaults, so a background class of yours replaces the built in one. Here's an example:

```html
<Badge ariaTitle="Demo badge" class="bg-green-500"/>

<Badge class="bg-yellow-400 text-black">New</Badge>

<p class="relative font-semibold text-white p-2 bg-gradient-to-r from-red-400 to-yellow-500">
  Notifications <Badge class="border-white dark:border-black" fixed>New</Badge>
</p>
```

Anything else you pass reaches the element itself, so an `id`, a `title`, a `data-*` attribute or an event handler all work the way they would on a `<span>`.

## Accessibility

Every badge carries `role="status"`, so a screen reader announces it when it appears or its text changes, which is what a count or a "New" marker is for.

A badge with text is read as that text, so **leave `ariaTitle` off a badge that says something**. A label would replace the content, and `<Badge>New</Badge>` with an `ariaTitle` is read as the label, never as "New".

A dot badge shows no text, so it has nothing to be read as. Give that one an `ariaTitle` that says what it stands for, such as `ariaTitle="3 unread"`. The remaining thing to watch is contrast: a custom background needs a text color to match.

## Configuration

### Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `ariaTitle` | string \| undefined | `undefined` | Names a badge that shows no text, such as a dot badge. Leave it off a badge with text: the label would replace that text for a screen reader. |
| `rounded` | [ROUNDED](https://www.theui.dev/docs/types#rounded-type) | `full` | Defines the border radius of the component. |

### Dynamic props

| Name | Description |
| --- | --- |
| `grow` | Determines if the component should grow to match the parent element's font size. |
| `fixed` | Fixes the component to the top of its container. |
| `class` | Classes for the badge element, merged with the default classes. |

---

Category: Components
Keywords: badge, notification, status-label, component, svelte, svelte-5, theui-svelte, component-library, ui, tailwindcss
Full documentation set: https://www.theui.dev/llms-full.txt
