Skip to main content
Bootstrap
CHAPTER 12 Beginner

Bootstrap Alerts and Badges

Updated: May 18, 2026
5 min read

# CHAPTER 12

Bootstrap Alerts and Badges

1. Chapter Introduction

Every application needs to communicate with its users — success after form submission, errors when validation fails, warnings before deleting data, and informational tips. Bootstrap's alert, badge, and toast components handle all these notification patterns elegantly.

2. Learning Objectives

  • Create alerts in all 8 color variants.
  • Make alerts dismissible.
  • Use badge components for counts and status labels.
  • Implement toast notifications.

3. Alert Components

html
1234567891011121314151617
<!-- Contextual alerts -->
<div class="alert alert-primary" role="alert">
  A simple primary alert — check it out!
</div>
<div class="alert alert-secondary" role="alert">Secondary alert</div>
<div class="alert alert-success" role="alert">
  ✅ <strong>Success!</strong> Your changes have been saved.
</div>
<div class="alert alert-danger" role="alert">
  ❌ <strong>Error!</strong> Something went wrong. Please try again.
</div>
<div class="alert alert-warning" role="alert">
  ⚠️ <strong>Warning!</strong> Your session expires in 5 minutes.
</div>
<div class="alert alert-info" role="alert">
  ℹ️ <strong>Info:</strong> A new version is available.
</div>
html
12345678910111213141516
<div class="alert alert-success">
  <h4 class="alert-heading">Well done! 🎉</h4>
  <p>Your account has been created successfully. You can now access all features.</p>
  <hr />
  <p class="mb-0">
    Go to your <a href="#" class="alert-link">Dashboard</a> to get started.
  </p>
</div>

<div class="alert alert-danger">
  <strong>Payment Failed</strong>
  <ul class="mb-0 mt-2">
    <li>Insufficient funds</li>
    <li>Card expiry date is incorrect</li>
  </ul>
</div>

5. Dismissible Alerts

html
12345678910
<!-- Dismissible alert (adds × button) -->
<div class="alert alert-warning alert-dismissible fade show" role="alert">
  ⚠️ Your trial period ends in <strong>3 days</strong>. Please upgrade to continue.
  <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>

<div class="alert alert-info alert-dismissible fade show" role="alert">
  📢 New feature: Dark mode is now available in settings.
  <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>

6. Badge Component

html
123456789101112131415161718192021222324252627282930
<!-- Badges on headings -->
<h1>Notifications <span class="badge bg-danger">4</span></h1>
<h2>Messages <span class="badge bg-primary">12</span></h2>

<!-- Status badges -->
<span class="badge bg-success">Active</span>
<span class="badge bg-warning text-dark">Pending</span>
<span class="badge bg-danger">Cancelled</span>
<span class="badge bg-secondary">Draft</span>
<span class="badge bg-info text-dark">Processing</span>

<!-- Pill badges (rounded) -->
<span class="badge rounded-pill bg-primary">New</span>
<span class="badge rounded-pill bg-danger">99+</span>

<!-- Badge on button (notification count) -->
<button type="button" class="btn btn-primary position-relative">
  Inbox
  <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger">
    8 <span class="visually-hidden">unread messages</span>
  </span>
</button>

<!-- Badge on icon button -->
<button class="btn btn-outline-secondary position-relative">
  🛒 Cart
  <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-primary">
    3
  </span>
</button>

7. Toast Notifications

html
123456789101112131415161718192021222324252627282930313233343536373839
<!-- Toast container (fixed position) -->
<div class="toast-container position-fixed bottom-0 end-0 p-3">

  <!-- Success toast -->
  <div id="successToast" class="toast align-items-center text-white bg-success border-0" role="alert">
    <div class="d-flex">
      <div class="toast-body">
        ✅ Profile updated successfully!
      </div>
      <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
    </div>
  </div>

  <!-- Toast with icon -->
  <div id="infoToast" class="toast" role="alert">
    <div class="toast-header">
      <span class="rounded me-2 bg-primary" style="width:20px;height:20px;display:inline-block;border-radius:3px;"></span>
      <strong class="me-auto">Notification</strong>
      <small>Just now</small>
      <button type="button" class="btn-close" data-bs-dismiss="toast"></button>
    </div>
    <div class="toast-body">
      You have a new message from Alice.
    </div>
  </div>

</div>

<!-- Trigger toasts with JS -->
<button class="btn btn-success" onclick="showToast(&#039;successToast')">Show Success Toast</button>
<button class="btn btn-primary" onclick="showToast(&#039;infoToast')">Show Info Toast</button>

<script>
  function showToast(id) {
    const el = document.getElementById(id);
    const toast = new bootstrap.Toast(el, { autohide: true, delay: 3000 });
    toast.show();
  }
</script>

8. Common Mistakes

  • Forgetting fade show on dismissible alerts: Without fade show, the dismiss animation won't work correctly.
  • Badge without visually-hidden span: Always include <span class="visually-hidden">unread messages</span> inside counter badges for screen readers.

9. MCQs

Question 1

What class creates a dismissible alert?

Question 2

What data attribute dismisses an alert?

Question 3

Pill-shaped badge?

Question 4

Badge positioned on a button corner uses?

Question 5

Toast position (bottom-right) uses?

Question 6

Auto-hiding toast delay is configured?

Question 7

What class adds the × close button in alerts?

Question 8

Alert link class?

Question 9

role="alert" on alerts provides?

Question 10

Badge warning color with readable text?

10. Interview Questions

  • Q: How do Bootstrap toasts differ from alerts?
  • Q: How do you position a notification count badge on a button?

11. Summary

Bootstrap's notification system covers all feedback scenarios. Alerts for persistent in-page messages with optional dismiss. Badges for compact status labels and counters. Toasts for non-intrusive temporary notifications. Together, these three components handle 100% of user feedback communication patterns.

12. Next Chapter Recommendation

In Chapter 13: Bootstrap Modals and Popups, we build the most powerful interactive component — modal dialogs for confirmations, forms, lightboxes, and side drawer panels.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·