Bootstrap ยท Chapter 11 of 43

Bootstrap Alerts

Alerts are colored message boxes used to give feedback to users, such as success confirmations, warnings, or error messages. Add the .alert class plus a contextual color class like .alert-success.

Alerts can also be dismissible, allowing users to close them with an animated fade by adding a close button and the required JavaScript classes.

Syntax
<div class="alert alert-success">Message</div>

Basic alerts

Add .alert and a color variant such as .alert-danger, .alert-warning, or .alert-info to a <div> to display a styled message box.

Dismissible alerts

Add .alert-dismissible and a button with class .btn-close and data-bs-dismiss="alert" to let users close the alert. Bootstrap's JS handles the fade-out animation.

Example 1 (html)
<div class="alert alert-success">
  Your changes have been saved successfully!
</div>
Output
A green box with a success message

alert-success gives the box a green theme suited to positive feedback.

Example 2 (html)
<div class="alert alert-warning alert-dismissible fade show" role="alert">
  This action cannot be undone.
  <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
Output
A yellow alert box with a close (x) button that fades out when clicked

alert-dismissible plus the btn-close button lets the user close the alert with a smooth fade animation.

Key points

  • Add .alert plus a color class like .alert-info to style a message box.
  • Available colors include success, danger, warning, and info.
  • .alert-dismissible with .btn-close makes alerts closable.
  • data-bs-dismiss="alert" triggers Bootstrap's JS to remove the alert.
๐Ÿ’ก Note: Dismissible alerts require Bootstrap's JavaScript bundle to be loaded on the page.

๐Ÿ“ Quick Quiz

1. Which class creates a green success alert?

2. Which attribute makes a button close its alert?

3. What is required for dismissible alerts to work?