Alerts
Provide contextual feedback messages for typical user actions using flexible alert messages.
Alerts may contain text or other elements and are styled according to their required contextual class,.alert-{color}
, where color is any color from the color palette. An optional dismiss button allows the user to dismiss the alert.
<div class="alert alert-primary" role="alert">
I'm a primary alert, check me out!
</div>
<div class="alert alert-secondary" role="alert">
I'm a secondary alert, check me out!
</div>
<div class="alert alert-success" role="alert">
I'm a success alert, check me out!
</div>
<div class="alert alert-danger" role="alert">
I'm a danger alert, check me out!
</div>
<div class="alert alert-warning" role="alert">
I'm a warning alert, check me out!
</div>
<div class="alert alert-info" role="alert">
I'm a info alert, check me out!
</div>
<div class="alert alert-light" role="alert">
I'm a light alert, check me out!
</div>
<div class="alert alert-dark" role="alert">
I'm a dark alert, check me out!
</div>
Customizing alerts
Link styling
Style links to fit in with the alert using .alert-link
.
<div class="alert alert-warning" role="alert">
I'm a warning alert. <a href="" class="alert-link">Click here to learn more.</a>
</div>
Additional content
Alerts can also contain additional HTML elements like headings, paragraphs and dividers.
Well done!
Aww yeah, you successfully read this important alert message. This example text is going to run a bit longer so that you can see how spacing within an alert works with this kind of content.
Whenever you need to, be sure to use margin utilities to keep things nice and tidy.
<div class="alert alert-success" role="alert">
<h4 class="alert-heading">Well done!</h4>
<p>Aww yeah, you successfully read this important alert message. This example text is going to run a bit longer so that you can see how spacing within an alert works with this kind of content.</p>
<hr>
<p class="mb-0">Whenever you need to, be sure to use margin utilities to keep things nice and tidy.</p>
</div>
Dismissing
Using the alert plugin from the Bootstrap JavaScript, it’s possible to dismiss any alert inline. Here’s how:
- Add the
.alert-dismissible
class to your alert. - Add a dismiss button with the
.close
class and thedata-dismiss="alert"
attribute. - To animate alerts when dismissing them, add
.fade
and.show
classes to the alert.
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>Holy guacamole!</strong> You should check in on some of those fields below.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
Javascript behavior
Triggers
Enable dismissal of an alert via JavaScript
$('.alert').alert();
Methods
Method | Description |
---|---|
$().alert() |
Makes an alert listen for click events on descendant elements which have the data-dismiss="alert" attribute. (Not necessary when using the data-api’s auto-initialization.) |
$().alert('close') |
Closes an alert by removing it from the DOM. If the .fade and .show classes are present on the element, the alert will fade out before it is removed. |
$().alert('dispose') |
Destroys an element’s alert. |
Events
Bootstrap’s alert plugin exposes a few events for hooking into alert functionality.
Event | Description |
---|---|
close.bs.alert |
This event fires immediately when the close instance method is called. |
closed.bs.alert |
This event is fired when the alert has been closed (will wait for CSS transitions to complete). |
$('#myAlert').on('closed.bs.alert', function () {
// do something…
})