Alerts, Badges & Close Button

Bootstrap Framework 9 min min read Updated: Nov 16, 2026 Beginner
Alerts, Badges & Close Button
Beginner Topic 3 of 14

Alerts

Alerts show important messages to the user, such as success, errors or warnings. Use alert plus a color class.

html <div class="alert alert-success" role="alert">Your profile was saved!</div> <div class="alert alert-danger" role="alert">Something went wrong.</div> <div class="alert alert-warning" role="alert">Your password will expire soon.</div> <div class="alert alert-info" role="alert">A new update is available.</div>
ClassWhen to Use
alert-successTask finished successfully
alert-dangerError or failure
alert-warningNeeds attention
alert-infoNeutral information
alert-primary, alert-secondaryGeneral notes

Links and Headings in Alerts

html <div class="alert alert-primary"> <h4 class="alert-heading">Welcome!</h4> <p>You can read the <a href="#" class="alert-link">getting started guide</a>.</p> <hr> <p class="mb-0">Have a great day.</p> </div>

Dismissible Alerts

A dismissible alert has a close button. It needs the Bootstrap JavaScript bundle.

html <div class="alert alert-warning alert-dismissible fade show" role="alert"> <strong>Heads up!</strong> You can close this message. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> </div>
  • alert-dismissible adds space for the close button
  • fade show gives a smooth fade-out animation
  • data-bs-dismiss="alert" tells Bootstrap to close the alert on click
Result

A yellow message with an X on the right. Clicking the X fades the alert away.

Badges

Badges are small labels used for counts, status or tags. They scale with the text around them.

html <h2>Messages <span class="badge text-bg-secondary">4</span></h2> <span class="badge text-bg-success">New</span> <span class="badge text-bg-danger rounded-pill">99+</span>

Badge Inside a Button

html <button class="btn btn-primary"> Notifications <span class="badge text-bg-light">4</span> </button>

Positioned Badge

html <button class="btn btn-outline-dark position-relative"> Cart <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger"> 3 </span> </button>
Tip: Do not use only color to give meaning. Add words such as Success or Error so that color-blind users also understand the message.

Conclusion

Alerts and badges give clear feedback in a small space. Use alerts for messages and badges for tiny labels like counts and status.

Quick Summary: alert alert-color for messages, alert-dismissible for closable ones, badge text-bg-color for small labels.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators