Basic Alerts

Provide contextual feedback messages for typical user actions with the handful of available and flexible alert messages.

Alerts are available for any length of text, as well as an optional dismiss button. For proper styling, use one of the eight required contextual classes (e.g., .alert-success). For inline dismissal, use the alerts jQuery plugin.


<!-- info --> <div class="alert alert-info" role="alert"> <i class="alert-close" aria-hidden="true"></i> Notification </div> <!-- success --> <div class="alert alert-success" role="alert"> <i class="alert-close" aria-hidden="true"></i> Success Message </div> <!-- highlighted note --> <div class="alert alert-highlight" role="alert"> <i class="alert-close" aria-hidden="true"></i> A highlighted note </div> <!-- warning --> <div class="alert alert-warning" role="alert"> <i class="alert-close" aria-hidden="true"></i> Warning </div> <!-- danger --> <div class="alert alert-danger" role="alert"> <i class="alert-close" aria-hidden="true"></i> <span class="alert-message">Error</span> </div> <!-- multiline alert --> <div class="alert alert-danger" role="alert"> <i class="alert-close" aria-hidden="true"></i> <div class="alert-message-container"> Multiple Line Example<br /> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do tempor incidunt ut labore et do lo re sas </div> </div>
Toasts

Toasts are lightweight notifications designed to mimic the push notifications that have been popularized by mobile and desktop operating systems. They’re built with flexbox, so they’re easy to align and position. A toast is only shown for a couple of seconds when something happens (i.e. when the user clicks on a button, submits a form, etc.).

Toasts are intended to be small interruptions to the user; to help those with screen readers and similar assistive technologies, wrap toasts in an aria-live region. Changes to live regions (such as injecting/updating a toast component) are automatically announced by screen readers without needing to move the user’s focus or otherwise interrupt the user. Additionally, include aria-atomic="true" to ensure that the entire toast is always announced as a single unit, rather than announcing what was changed (which could lead to problems if you only update part of the toast’s content, or if displaying the same toast content at a later point in time). If the information needed is important for the process, e.g. for a list of errors in a form, then use the alert component instead of toast.

Note that the live region needs to be present in the markup before the toast is generated or updated. If you dynamically generate both at the same time and inject them into the page, they will generally not be announced by assistive technologies.

The role and aria-live level would need to be updated depending on the content. If it’s an important message like an error, use role="alert" aria-live="assertive", otherwise use role="status" aria-live="polite" attributes.

As the displayed content changes, be sure to update the delay timeout to ensure users have enough time to read the toast.

<div class="toast" role="alert" aria-live="polite" aria-atomic="true" data-delay="10000"> <div role="alert" aria-live="assertive" aria-atomic="true">...</div> </div>


<div aria-live="polite" aria-atomic="true" class="d-flex justify-content-center align-items-center"> <!-- Then put toasts within --> <div class="toast" role="alert" aria-live="assertive" aria-atomic="true"> <div class="toast-header toast-success"> <strong class="mr-auto">Success!</strong> <small>11 mins ago</small> <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="toast-body"> Hello, world! This is a toast message. </div> </div> </div> <script> $(document).ready(function(){ $('#myBtn').click(function(){ $('.toast').toast({delay: 2000}); $('.toast').toast('show'); }); }); </script>

Dismissable Alerts

Using the alert JavaScript plugin, it’s possible to dismiss any alert inline. Here’s how:

  • Be sure you’ve loaded the alert plugin, or the compiled Bootstrap JavaScript.
  • If you’re building our JavaScript from source, it requires util.js. The compiled version includes this.
  • Add a dismiss button and the .alert-dismissible class, which adds extra padding to the right of the alert and positions the .close button.
  • On the dismiss button, add the data-dismiss="alert" attribute, which triggers the JavaScript functionality. Be sure to use the button element with it for proper behavior across all devices.
  • To animate alerts when dismissing them, be sure to add the .fade and .show classes.
Note that closing the alert will remove it from the DOM.


<div class="alert alert-warning alert-dismissible fade show" role="alert"> <strong>Holy feline frenzy, Batman!</strong> You should get busy filling out the below form fields... <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div>