Badge
A small, rounded label next to other content, in the same variants as the button.
Thymeleaf source
<sl:badge>New</sl:badge>Rendered HTML
<span class="badge">New</span>When to use
Section titled “When to use”- The state of something in a table row, such as Paid, Pending or Failed.
- A count, such as unread messages.
- A tag or a category on an item or a card.
Use something else for:
- A count next to a link in the sidebar: the sidebar’s menu badge.
- A key on the keyboard: a kbd.
- A message the user should read: an alert.
Variants
Section titled “Variants”The same variants as the button.
Thymeleaf source
<sl:badge>Default</sl:badge><sl:badge variant="secondary">Secondary</sl:badge><sl:badge variant="destructive">Destructive</sl:badge><sl:badge variant="outline">Outline</sl:badge><sl:badge variant="ghost">Ghost</sl:badge><sl:badge variant="link">Link</sl:badge>Rendered HTML
<span class="badge">Default</span><span class="badge" data-variant="secondary">Secondary</span><span class="badge" data-variant="destructive">Destructive</span><span class="badge" data-variant="outline">Outline</span><span class="badge" data-variant="ghost">Ghost</span><span class="badge" data-variant="link">Link</span>Status variants
Section titled “Status variants”success, info and warning are Shadleaf’s own, for a state such as Paid, In progress or Due soon. They stand next
to destructive and are tinted the same way, in the status colours: --success, --info and --warning for the
tint, --success-text, --info-text and --warning-text for the text (the same tokens as a
toast’s rich colours). The text keeps 4.5:1 or more on the tint, in light and dark mode.
Thymeleaf source
<sl:badge variant="success">Paid</sl:badge><sl:badge variant="info">In progress</sl:badge><sl:badge variant="warning">Due soon</sl:badge><sl:badge variant="destructive">Overdue</sl:badge><sl:badge as="a" href="/orders?status=paid" variant="success"> <sl:slot name="icon-start"><sl:icon name="circle-check"/></sl:slot> Paid</sl:badge>Rendered HTML
<span class="badge" data-variant="success">Paid</span><span class="badge" data-variant="info">In progress</span><span class="badge" data-variant="warning">Due soon</span><span class="badge" data-variant="destructive">Overdue</span><a class="badge" data-variant="success" href="/orders?status=paid"> <span class="badge-icon" data-icon="inline-start"> <svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false"> <circle cx="12" cy="12" r="10"></circle> <path d="m16 9-5.5 5.5L8 12"></path> </svg> </span> Paid</a>With an icon
Section titled “With an icon”Put an <sl:icon> in the icon-start or icon-end slot. It takes the badge’s colour and a 12 pixel size.
Thymeleaf source
<sl:badge variant="secondary"> <sl:slot name="icon-start"><sl:icon name="badge-check"/></sl:slot> Verified</sl:badge><sl:badge variant="outline"> Details <sl:slot name="icon-end"><sl:icon name="arrow-right"/></sl:slot></sl:badge>Rendered HTML
<span class="badge" data-variant="secondary"> <span class="badge-icon" data-icon="inline-start"> <svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false"> <path d="M3.85 8.62a4 4 0 0 1 4.78-4.77 4 4 0 0 1 6.74 0 4 4 0 0 1 4.78 4.78 4 4 0 0 1 0 6.74 4 4 0 0 1-4.77 4.78 4 4 0 0 1-6.75 0 4 4 0 0 1-4.78-4.77 4 4 0 0 1 0-6.76Z"></path> <path d="m16 9-5.5 5.5L8 12"></path> </svg> </span> Verified</span><span class="badge" data-variant="outline"> Details <span class="badge-icon" data-icon="inline-end"> <svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false"> <path d="M5 12h14"></path> <path d="m12 5 7 7-7 7"></path> </svg> </span></span>As a link
Section titled “As a link”as="a" renders an anchor, for a badge that filters a list or opens a detail page. Only a linked badge reacts to hover
and shows a focus ring; a plain badge is not interactive, so it does not pretend to be.
Thymeleaf source
<sl:badge as="a" href="/orders?status=open">Open</sl:badge><sl:badge as="a" href="/orders?status=late" variant="destructive">Late</sl:badge><sl:badge as="a" href="/orders" variant="outline"> All orders <sl:slot name="icon-end"><sl:icon name="arrow-up-right"/></sl:slot></sl:badge>Rendered HTML
<a class="badge" href="/orders?status=open">Open</a><a class="badge" data-variant="destructive" href="/orders?status=late">Late</a><a class="badge" data-variant="outline" href="/orders"> All orders <span class="badge-icon" data-icon="inline-end"> <svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false"> <path d="M7 7h10v10"></path> <path d="M7 17 17 7"></path> </svg> </span></a>A badge is text, so a screen reader reads it where it stands. When a bare number or a single word would not make sense out of context, write the context into the text, or next to the badge.
Props from expressions
Section titled “Props from expressions”Thymeleaf source
<sl:badge th:variant="${order.late ? 'destructive' : 'secondary'}" th:text="${order.status}">Open</sl:badge>Rendered HTML
<span class="badge" data-variant="destructive">Late</span>Everything else passes through
Section titled “Everything else passes through”Thymeleaf source
<sl:badge variant="outline" class="app-wide" id="order-status" data-testid="status">Shipped</sl:badge>Rendered HTML
<span class="badge app-wide" data-variant="outline" id="order-status" data-testid="status">Shipped</span>Attributes
Section titled “Attributes”| Attribute | Values | Default | Description |
|---|---|---|---|
variant | default secondary destructive success info warning outline ghost link | default | Visual style of the badge. success, info and warning are Shadleaf's own, in the status colours. |
as | span a | span | Element to render: a span, or an anchor (a) for a badge that links somewhere. Only a linked badge reacts to hover. |
Every prop can also be set from an expression: th:variant="${...}". Any other attribute passes through to the rendered element, and class is appended.
Styling hooks
Section titled “Styling hooks”The classes and attributes your own CSS can select to restyle the component, as explained in your own CSS rules.
Parts
| Class | Element |
|---|---|
.badge | <a> <span> |
.badge-icon | <span> |
Props
| Prop | Selector | Values |
|---|---|---|
variant | .badge[data-variant="…"].badge:not([data-variant]) | secondary destructive success info warning outline ghost linkdefault (default) |
States
| Selector | Values |
|---|---|
.badge[aria-invalid="…"] | true |
.badge-icon[data-icon="…"] | inline-end inline-start |