Alerts
Bootstrap 5 Alerts component
Responsive Alerts built with the latest Bootstrap 5. Alerts provide contextual feedback messages for typical user actions with a handful of responsible and flexible alert boxes.
Provide contextual feedback messages for typical user actions with the handful of available and flexible alert messages.
Note: Read the API tab to find all available options and advanced customization
*
*
UMD autoinits are enabled
by default. This means that you don't need to initialize
the component manually. However if you are using MDBootstrap ES format then you should pass
the required components to the initMDB
method.
Basic examples
Click the buttons to launch the alerts.
Static examples
Use .alert
class, followed by one of the contextual classes
alert-success
, .alert-info
, .alert-warning
,
.alert-danger
, .alert-primary
, .alert-secondary
,
.alert-light
or .alert-dark
to create an alert.
Link Color
Use .alert-link
class to create matching colored links inside the alert box.
Icons
Use Font Awesome Icons to create alerts with icons. Depending on your icons and content, you may want to add more utilities or custom styles.
Additional Content
Alerts can contain additional 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.
Dismissing
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.
- Add
data-mdb-alert-init
attribute. -
Add a close button and the
.alert-dismissible
class, which adds extra padding to the right of the alert and positions the close button. -
On the close button, add the
data-mdb-dismiss="alert"
attribute, which triggers the JavaScript functionality. Be sure to use thebutton
element with it for proper behavior across all devices. -
To animate alerts when dismissing them, be sure to add the
.fade
and.show classes
.
You can see this in action with a live demo:
Show
You can manually show alert using show()
method.
Hide
You can manually hide alert using hide()
method.
Placement
You can set the position of every alert using the
data-mdb-position
attribute.
Select horizontal / vertical alignment
Current position: top-right
Container
You can display an alert anywhere. Just put it in your target element and fill the
data-mdb-container
attribute with id or class of parent.
Offset
You can set offset of your alert using a data-mdb-offset
tag.
Stacking
You can turn on stacking your alerts using the
data-mdb-stacking
attribute.
Stacking - Container
You can also stack alerts inside the container
Alerts - API
Import
Importing components depends on how your application works. If you intend to use the MDBootstrap ES
format, you must
first import the component and then initialize it with the initMDB
method. If you are going to use the UMD
format,
just import the mdb-ui-kit
package.
Usage
Via data attributes
Using the Alert component doesn't require any additional JavaScript code - simply add
data-mdb-alert-init
attribute to
.alert
and use other data attributes to set all options.
For ES
format, you must first import and call the initMDB
method.
Dismiss
Dismissal can be achieved with data attributes on a button within the alert as demonstrated below:
or on a button outside the alert using the data-mdb-target
as
demonstrated below:
Via JavaScript
Via jQuery
Note: By default, MDB does not include jQuery and you have to add it to the project on your own.
Options
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to
data-mdb-
, as in data-mdb-offset=""
.
Name | Type | Default | Description |
---|---|---|---|
position
|
String | null | null |
Sets the position of the alert. It can include horizontal options: top or
bottom , and vertical options: left , center , or
right . It can also be a combination of any horizontal and vertical
values, for example, top-left
|
delay |
Number | 1000 |
Sets the length of animation delay |
autohide
|
Boolean | false |
Alerts will hide automatically or not |
hidden
|
Boolean | false |
Alerts will be hidden until show() method is invoked. |
width
|
String | null | null |
Sets width of alert |
container
|
String | null | null |
Defines id/class of the parent element |
offset |
Number | 10 |
Defines offset of the element |
stacking
|
Boolean | false |
Enables stacking alerts |
appendToBody
|
Boolean | false |
Appends element to the end of the body |
color
|
String | null | null |
Allows to set the color of an alert - primary | secondary | secondary | danger | warning | info | light | dark |
Methods
Name | Description | Example |
---|---|---|
show |
Manually shows an alert | myAlert.show() |
hide |
Manually hides an alert | myAlert.hide() |
close |
Manually hides an alert (deletes it from DOM) | myAlert.close() |
dispose
|
Removes an mdb.Alert instance |
myAlert.dispose()
|
getInstance
|
Static method which allows you to get the alert instance associated to a DOM element. |
Alert.getInstance(myAlert)
|
getOrCreateInstance
|
Static method which returns an alert instance associated to a DOM element or create a new one in case it wasn't initialized. |
Alert.getOrCreateInstance(myAlert)
|
update |
Updates options of an mdb.Alert instance |
myAlert.update({position: 'top-right'})
|
Events
Name | Description |
---|---|
close.mdb.alert
|
Fires immediately when the close instance method is called. |
closed.mdb.alert
|
Fired when the alert has been closed and CSS transitions have completed. |
show.mdb.alert |
This event is triggered when a new alert is about to be shown. By using e.preventDefault() you
can implement custom validation.
|
shown.mdb.alert |
This event is triggered after the alert has been shown. |
hide.mdb.alert |
This event is triggered when a new alert is about to be hidden. By using e.preventDefault() you
can implement custom validation.
|
hidden.mdb.alert |
This event is triggered after the alert has been hidden. |
CSS variables
As part of MDB’s evolving CSS variables approach, alerts now uses 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.