Bootstrap Badges
Learn how to use Bootstrap badges to display counts, status information, labels, and notifications.
What are Badges?
Badges are small components used to display additional information such as numbers, status, labels, or notifications.
They are commonly used with buttons, headings, navigation links, and notification icons.
Basic Badges
Use the .badge class to create a badge.
Add a contextual background class to change its color.
Example:
Code:
<span class="badge bg-primary">
Primary
</span>
<span class="badge bg-success">
Success
</span>
<span class="badge bg-danger">
Danger
</span>
<span class="badge bg-warning text-dark">
Warning
</span>
.badge class creates the badge.
Classes such as bg-primary and
bg-success set its background color.
Badges with Headings
You can place badges inside headings to highlight additional information.
Example:
Course New
Bootstrap Popular
Java Updated
<h2>
Course
<span class="badge bg-primary">
New
</span>
</h2>
<h3>
Bootstrap
<span class="badge bg-success">
Popular
</span>
</h3>
Badges with Buttons
Badges can be placed inside buttons to display counts or additional information.
Example:
<button type="button" class="btn btn-primary">
Messages
<span class="badge bg-light text-dark">
5
</span>
</button>
Notification Badges
Notification badges display the number of new or unread notifications.
Example:
<button type="button"
class="btn btn-outline-primary">
Notifications
<span class="badge bg-danger">
12
</span>
</button>
Pill Badges
Add the .rounded-pill class to make
a badge appear rounded.
Example:
<span class="badge rounded-pill bg-primary">
Primary
</span>
<span class="badge rounded-pill bg-success">
Active
</span>
.rounded-pill class gives the badge
a pill-shaped appearance.
Outline Style Badges
You can create outline-style badges using Bootstrap's border, text, and background utility classes.
Example:
<span class="badge bg-white text-primary border border-primary">
Primary
</span>
<span class="badge bg-white text-success border border-success">
Success
</span>
Badges with Links
Badges can also be used as links to navigate to another page or section.
Example:
<a href="#"
class="badge bg-primary text-decoration-none">
View Courses
</a>
href="#" with the actual URL
when you want the badge link to navigate to a real page.
Real-World Example
A training institute website can use badges to show course status and student information.
Java Full Stack
Development Course
Python Full Stack
Development Course
Data Analytics
Analytics Course
Important Bootstrap Badge Classes
| Class | Purpose |
|---|---|
.badge |
Creates a badge. |
.bg-primary |
Applies a primary background color. |
.bg-success |
Applies a success background color. |
.bg-danger |
Applies a danger background color. |
.bg-warning |
Applies a warning background color. |
.rounded-pill |
Makes the badge pill-shaped. |
.border |
Adds a border to the badge. |
Common Mistakes
-
Forgetting the
.badgeclass. - Forgetting to add a background color class.
-
Using a warning background without
text-darkwhen needed for readability. - Using badges when a large heading or message would be more appropriate.
- Assuming a badge automatically updates its count. Dynamic counts require application logic.
Summary
Bootstrap Badges are small components used to display labels, counts, statuses, and notifications.
-
.badgecreates a badge. - Background classes change the badge color.
-
.rounded-pillcreates a rounded badge. - Badges can be used with headings, buttons, and links.
- Application logic is required for automatically updating notification counts.