View on GitHub

Badges

Documentation and examples for badges, our small count and labeling component.

Examples

Badges scale to match the size of the immediate parent element by using relative font sizing and em units. As of v5, badges no longer have focus or hover styles for links.

Headings

Badge - 图1

html

  1. <h1>Example heading <span class="badge bg-secondary">New</span></h1>
  2. <h2>Example heading <span class="badge bg-secondary">New</span></h2>
  3. <h3>Example heading <span class="badge bg-secondary">New</span></h3>
  4. <h4>Example heading <span class="badge bg-secondary">New</span></h4>
  5. <h5>Example heading <span class="badge bg-secondary">New</span></h5>
  6. <h6>Example heading <span class="badge bg-secondary">New</span></h6>

Buttons

Badges can be used as part of links or buttons to provide a counter.

Badge - 图2

html

  1. <button type="button" class="btn btn-primary">
  2. Notifications <span class="badge text-bg-secondary">4</span>
  3. </button>

Note that depending on how they are used, badges may be confusing for users of screen readers and similar assistive technologies. While the styling of badges provides a visual cue as to their purpose, these users will simply be presented with the content of the badge. Depending on the specific situation, these badges may seem like random additional words or numbers at the end of a sentence, link, or button.

Unless the context is clear (as with the “Notifications” example, where it is understood that the “4” is the number of notifications), consider including additional context with a visually hidden piece of additional text.

Positioned

Use utilities to modify a .badge and position it in the corner of a link or button.

Badge - 图3

html

  1. <button type="button" class="btn btn-primary position-relative">
  2. Inbox
  3. <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger">
  4. 99+
  5. <span class="visually-hidden">unread messages</span>
  6. </span>
  7. </button>

You can also replace the .badge class with a few more utilities without a count for a more generic indicator.

Badge - 图4

html

  1. <button type="button" class="btn btn-primary position-relative">
  2. Profile
  3. <span class="position-absolute top-0 start-100 translate-middle p-2 bg-danger border border-light rounded-circle">
  4. <span class="visually-hidden">New alerts</span>
  5. </span>
  6. </button>

Background colors

Added in v5.2.0

Set a background-color with contrasting foreground color with our .text-bg-{color} helpers. Previously it was required to manually pair your choice of .text-{color} and .bg-{color} utilities for styling, which you still may use if you prefer.

Badge - 图5

html

  1. <span class="badge text-bg-primary">Primary</span>
  2. <span class="badge text-bg-secondary">Secondary</span>
  3. <span class="badge text-bg-success">Success</span>
  4. <span class="badge text-bg-danger">Danger</span>
  5. <span class="badge text-bg-warning">Warning</span>
  6. <span class="badge text-bg-info">Info</span>
  7. <span class="badge text-bg-light">Light</span>
  8. <span class="badge text-bg-dark">Dark</span>
Conveying meaning to assistive technologies

Using color to add meaning only provides a visual indication, which will not be conveyed to users of assistive technologies – such as screen readers. Ensure that information denoted by the color is either obvious from the content itself (e.g. the visible text), or is included through alternative means, such as additional text hidden with the .visually-hidden class.

Pill badges

Use the .rounded-pill utility class to make badges more rounded with a larger border-radius.

Badge - 图6

html

  1. <span class="badge rounded-pill text-bg-primary">Primary</span>
  2. <span class="badge rounded-pill text-bg-secondary">Secondary</span>
  3. <span class="badge rounded-pill text-bg-success">Success</span>
  4. <span class="badge rounded-pill text-bg-danger">Danger</span>
  5. <span class="badge rounded-pill text-bg-warning">Warning</span>
  6. <span class="badge rounded-pill text-bg-info">Info</span>
  7. <span class="badge rounded-pill text-bg-light">Light</span>
  8. <span class="badge rounded-pill text-bg-dark">Dark</span>

CSS

Variables

Added in v5.2.0

As part of Bootstrap’s evolving CSS variables approach, badges now use local CSS variables on .badge for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.

  1. --#{$prefix}badge-padding-x: #{$badge-padding-x};
  2. --#{$prefix}badge-padding-y: #{$badge-padding-y};
  3. @include rfs($badge-font-size, --#{$prefix}badge-font-size);
  4. --#{$prefix}badge-font-weight: #{$badge-font-weight};
  5. --#{$prefix}badge-color: #{$badge-color};
  6. --#{$prefix}badge-border-radius: #{$badge-border-radius};

Sass variables

  1. $badge-font-size: .75em;
  2. $badge-font-weight: $font-weight-bold;
  3. $badge-color: $white;
  4. $badge-padding-y: .35em;
  5. $badge-padding-x: .65em;
  6. $badge-border-radius: $border-radius;