Master Bootstrap From Scratch

Clear, interactive, and structured Bootstrap lessons designed for beginners and developers.

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.

Simple Example: A notification badge showing the number of unread messages.

Basic Badges

Use the .badge class to create a badge. Add a contextual background class to change its color.

Example:
Primary Secondary Success Danger Warning Info Dark
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>
Simple Explanation: The .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>
Simple Explanation: The badge appears next to the heading and highlights information such as New, Popular, or Updated.

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>
Simple Explanation: The badge displays the number of messages, notifications, or pending tasks inside the 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>
Real-World Example: An application can display 12 to indicate that the user has 12 notifications.

Pill Badges

Add the .rounded-pill class to make a badge appear rounded.

Example:
Primary Active Error Pending
<span class="badge rounded-pill bg-primary">
    Primary
</span>

<span class="badge rounded-pill bg-success">
    Active
</span>
Simple Explanation: The .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:
Primary Success Danger
<span class="badge bg-white text-primary border border-primary">
    Primary
</span>

<span class="badge bg-white text-success border border-success">
    Success
</span>
Simple Explanation: A white background, colored text, and colored border create an outline-style badge.

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>
Note: Replace 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

Active
Python Full Stack

Development Course

New
Data Analytics

Analytics Course

Upcoming
Simple Explanation: Badges make it easy for students to identify whether a course is Active, New, or Upcoming.

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 .badge class.
  • Forgetting to add a background color class.
  • Using a warning background without text-dark when 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.

  • .badge creates a badge.
  • Background classes change the badge color.
  • .rounded-pill creates a rounded badge.
  • Badges can be used with headings, buttons, and links.
  • Application logic is required for automatically updating notification counts.