TExcelTAD
Home › Bootstrap › BS5 Badges

Bootstrap 5 Badges

A badge is a small label or counter: "New", "Beta", "3 unread". Badges are <span>s with the .badge class and a background colour. Because they're sized in em, a badge automatically scales with the text around it.

Badges in headings and text

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <h2>Release notes <span class="badge text-bg-secondary">v2.4</span></h2>
  <h5>Dark mode <span class="badge text-bg-success">New</span></h5>
  <p>Experimental API <span class="badge text-bg-warning">Beta</span> — may change.</p>
</body>
</html>

The badge is bigger next to the <h2> and smaller next to the paragraph, with no extra classes.

Colours and pill badges

Use .text-bg-{color} to set a background and a readable text colour together. .rounded-pill gives fully rounded ends.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <p>
    <span class="badge text-bg-primary">Primary</span>
    <span class="badge text-bg-secondary">Secondary</span>
    <span class="badge text-bg-success">Success</span>
    <span class="badge text-bg-danger">Danger</span>
    <span class="badge text-bg-warning">Warning</span>
    <span class="badge text-bg-info">Info</span>
    <span class="badge text-bg-light">Light</span>
    <span class="badge text-bg-dark">Dark</span>
  </p>
  <p>
    <span class="badge rounded-pill text-bg-primary">Pill</span>
    <span class="badge rounded-pill text-bg-danger">99+</span>
    <span class="badge bg-primary-subtle text-primary-emphasis border border-primary-subtle rounded-pill">Subtle</span>
  </p>
</body>
</html>

Counters inside buttons

A badge inside a button works as a notification counter. Add visually hidden text so screen-reader users hear "4 unread messages", not just "4".

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <button type="button" class="btn btn-primary" id="inbox">
    Inbox <span class="badge text-bg-light" id="count">4</span>
    <span class="visually-hidden">unread messages</span>
  </button>
  <button type="button" class="btn btn-outline-secondary" onclick="addOne()">+1 message</button>
  <script>
    function addOne() {
      const c = document.getElementById('count');
      c.textContent = Number(c.textContent) + 1;
      console.log('unread:', c.textContent);
    }
  </script>
</body>
</html>

Positioned notification badges

Use the position utilities to pin a badge to a corner of a button or avatar: make the parent position-relative, then place the badge with position-absolute top-0 start-100 translate-middle.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-4">
  <button type="button" class="btn btn-dark position-relative me-4">
    🔔 Alerts
    <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger">
      12 <span class="visually-hidden">new alerts</span>
    </span>
  </button>
  <span class="position-relative d-inline-block fs-1">
    🧑
    <span class="position-absolute bottom-0 end-0 p-2 bg-success border border-2 border-white rounded-circle">
      <span class="visually-hidden">Online</span>
    </span>
  </span>
</body>
</html>

The second badge has no text at all — it's just a small coloured dot for an "online" status.

Class reference

Class Effect
.badge Base badge (small, bold, padded, em-sized)
.text-bg-{color} Background + contrasting text colour
.bg-{color} Background only (set text colour yourself)
.rounded-pill Pill shape
.position-relative (parent) Positioning context for a corner badge
.position-absolute .top-0 .start-100 .translate-middle Top-right corner placement
.visually-hidden Hidden on screen, read by screen readers

Note: Bootstrap 4's .badge-primary and .badge-pill classes are gone. In Bootstrap 5 use background utilities (.text-bg-primary) and .rounded-pill.

Test yourself

Why does a badge look bigger inside an <h1> than in a paragraph?

  1. Bootstrap detects headings with JS
  2. Badges are sized in em, relative to the parent font size
  3. You must add .badge-lg
  4. Headings add padding to children

Which class gives a badge fully rounded ends in Bootstrap 5?

  1. .badge-pill
  2. .rounded-pill
  3. .pill
  4. .rounded-circle

What must the parent have for a badge positioned with position-absolute top-0 start-100?

  1. .position-relative
  2. .d-flex
  3. .overflow-hidden
  4. .badge-parent