Creating Alerts with Bootstrap: Displaying Messages and Notifications

Learn how to create and customize alerts in Bootstrap for displaying messages and notifications to users. This tutorial covers Bootstrap 3 and Bootstrap 4 alert styles, their contextual classes, and how to effectively use alerts to provide feedback and warnings in your web applications.



Creating Alerts with Bootstrap

Bootstrap alerts provide a simple and visually appealing way to display important messages to users. They're useful for providing feedback, warnings, or other notifications.

Bootstrap 3 Alerts

Bootstrap 3 offers several alert styles using contextual classes within a `

` element with the class `.alert`:

  • `.alert-success` (green): For successful actions.
  • `.alert-info` (blue): For informative messages.
  • `.alert-warning` (yellow): For warnings.
  • `.alert-danger` (red): For errors or critical issues.

Test Bootstrap 3 Alerts Now

Bootstrap 4 Alerts

Bootstrap 4 adds more alert styles:

  • `.alert-primary` (blue): For important actions.
  • `.alert-secondary` (grey): For less important actions.
  • `.alert-light` (light grey): A light grey alert.
  • `.alert-dark` (dark grey): A dark grey alert.

All Bootstrap 4 alert types are demonstrated below.

Test Bootstrap 4 Alerts Now

Alert Links

Style links within alerts using the `.alert-link` class to match the alert's color.

Test Alert Links Now

Closable Alerts

To make alerts closable, add the `.alert-dismissible` class to the alert container and include a close button (a link or button with classes `close` and `data-dismiss="alert"`).

Test Closable Alerts Now

Animated Alerts

Add a fade-in/fade-out animation using the `.fade` and `.show` classes. The `.show` class is required for the animation to work correctly.

Test Animated Alerts Now