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


Basic examples

Click the buttons to launch the alerts.


Static examples



Icons


Additional Content


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 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 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.

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


If you want to support our friends from Tailwind Elements you can also check out the Tailwind Alerts documentation.

Alerts - API


Usage

Via data attributes


        <div
          class="alert alert-primary fade show"
          id="example"
          data-mdb-autohide="true"
          data-mdb-delay="2000"
          role="alert"
        >
          Example
        </div>
      

Via JavaScript


        mdb.Alert.getInstance(document.getElementById('show-example')).update({ position: ...,
        width: ..., });
      

Via jQuery

Note: By default, MDB does not include jQuery and you have to add it to the project on your own.

 $('.example-class').alert(options); 

Options

Name Type Default Description
position String / null null Sets a position for the alert - top-left | top-right | bottom-left | bottom-right
delay Number 1000 Sets the length of animation delay
autohide boolean false Alerts will hide automatically or not
width String 'unset' Sets width of alert
container String - 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)
update Updates options of an mdb.Alert instance myAlert.update({position: 'top-right'})

        mdb.Alert.getInstance(document.getElementById('show-example')).show();
      

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.

        const alert = document.getElementById('example')
        alert.addEventListener('close.mdb.alert', () => { // do something });
      

Import

MDB UI KIT also works with module bundlers. Use the following code to import this component:

 import { Alert } from 'mdb-ui-kit';