Alerts
Examples
Conveying meaning to assistive technologies
Using color to add meaning only provides a visual indication, which will not be conveyed to users of assistive technologies – such as screen readers. Ensure that information denoted by the color is either obvious from the content itself (e.g. the visible text), or is included through alternative means, such as additional text hidden with the .visually-hidden class.
Link color
Additional content
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.
Dismissing
Add a close button with .alert-dismissible class and data-bs-dismiss="alert" attribute.
JavaScript behavior
Triggers
Via api:
Via data attributes:
Methods
Constructor:
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.)
| Method | Description |
|---|---|
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.
|
dispose
|
Destroys an element's alert. (Removes stored data on the DOM element) |
getInstance
|
Static method which allows you to get the alert instance associated to a DOM element, you can use it like this: bootstrap.Alert.getInstance(alert)
|
Events
| Event | Description |
|---|---|
close.bs.alert |
Fires immediately when the close instance method is called.
|
closed.bs.alert |
Fired when the alert has been closed and CSS transitions have completed. |