Alerts are available for any length of text, as well as an optional close button. For proper styling, use one of the eight required contextual classes (e.g., .alert-success). For inline dismissal, use the alerts JavaScript plugin.
Click the button below to show an alert (hidden with inline styles to start), then dismiss (and destroy) it with the built-in close button.
We use the following JavaScript to trigger our live alert demo:
Use the .alert-link utility class to quickly provide matching colored links within any alert.
Alerts can also contain additional HTML elements like headings, paragraphs and dividers.
Similarly, you can use flexbox utilities and Bootstrap Icons to create alerts with icons. Depending on your icons and content, you may want to add more utilities or custom styles.
Need more than one icon for your alerts? Consider using more Bootstrap Icons and making a local SVG sprite like so to easily reference the same icons repeatedly.
Using the alert JavaScript plugin, it’s possible to dismiss any alert inline. Here’s how:
You can see this in action with a live demo:
As part of Bootstrap’s evolving CSS variables approach, alerts now use local CSS variables on .alert for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.
Used in combination with $theme-colors to create contextual modifier classes for our alerts.
Loop that generates the modifier classes with the alert-variant() mixin.
Initialize elements as alerts
Dismissal can be achieved with the data-bs-dismiss attribute on a button within the alert as demonstrated below:
or on a button outside the alert using the additional data-bs-target as demonstrated below:
Note that closing an alert will remove it from the DOM.
You can create an alert instance with the alert constructor, for example:
This makes an alert listen for click events on descendant elements which have the data-bs-dismiss="alert" attribute. (Not necessary when using the data-api’s auto-initialization.)
Bootstrap’s alert plugin exposes a few events for hooking into alert functionality.