Bootstrap

Badge Styles in Bootstrap 5 | Explained

Badges on a website are used to deliver some important information to the user such as a warning, notification count, promotion of a certain program, etc. Bootstrap 5 provides a certain class that lets you create badges on your website. Besides their creation, other classes are also available to style the badges. This blog guides you on how to create and style badges using Bootstrap 5.

How to create a badge

For the purpose of creating a badge use the .badge class along with a background color class which are .bg-primary, .bg-success, .bg-secondary, .bg-info, .bg-warning, .bg-danger, .bg-light, .bg-dark.

HTML

<div class="container">
  <h1>Click Here <span class="badge bg-warning">Important</span></h1>
  <p>Click Here <span class="badge bg-warning">Important</span></p>
</div>

To create a badge the <span> element is used within a <h1> and <p> element. The element has been assigned the .badge class along with a background color class, .bg-warning to give the badge a yellow background. Note that a badge by default has a rectangular shape and scales up and down to match the size of the parent element, if there is any.

Output

The badges in the above output are used to grab a users’ attention.

How to give a background color to badges

Badges can be provided a background color using any of the background color classes. These classes play an important role in conveying the meaning of a badge.

HTML

<div class="container">
  <span class="badge bg-primary">Badge</span>
  <span class="badge bg-success">Badge</span>
  <span class="badge bg-danger">Badge</span>
  <span class="badge bg-dark">Badge</span>
</div>

Here we have created four badges and each of them has been assigned a different background color class.

Output

Each badge conveys a different meaning.

How to create rounded badges

As already mentioned, badges by default have a rectangular shape, however, if you desire to give them a rounded shape then use the .rounded-pill class.

HTML

<div class="container">
  <span class="badge rounded-pill bg-primary">Badge</span>
  <span class="badge rounded-pill bg-success">Badge</span>
  <span class="badge rounded-pill bg-danger">Badge</span>
  <span class="badge rounded-pill bg-dark">Badge</span>
</div>

The above code will generate four pill shaped badges each with a different background color.

Output

The badges were given a rounded shape successfully.

How to create counter badges

Counter badges can be used to display notification count of a certain element. For the purpose of making such badges you have to nest the element within that particular element.

HTML

<div class="container">
  <button type="button" class="btn btn-info">
    Missed Calls <span class="badge bg-danger">8</span>
  </button>
</div>

Here we have created a “missed calls” button having a certain style and then we have nested a element which also has a certain style and shows the number of missed calls, thereby, displaying the notification count for the missed calls.

Output

The output above shows that there are a total of 8 missed calls.

Conclusion

For the purpose of creating and styling badges using Bootstrap 5 use the .badge class along with one of the background color classes which are .bg-primary, .bg-success, .bg-secondary, .bg-info, .bg-warning, .bg-danger, .bg-light, .bg-dark to give them a background color. Moreover, a badge by default has a rectangular shape, however, these can be given a rounded shape using the .rounded-pill class. Furthermore, you make a notification counter badge by nesting the badge inside an element.

About the author

Naima Aftab

I am a software engineering professional with a profound interest in writing. I am pursuing technical writing as my full-time career and sharing my knowledge through my words.