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.

        
            
            <!-- Trigger buttons -->
            <button type="button" id="primary" class="btn btn-primary m-1">Primary</button>
            <button type="button" id="secondary" class="btn btn-secondary m-1">Secondary</button>
            <button type="button" id="success" class="btn btn-success m-1">Success</button>
            <button type="button" id="danger" class="btn btn-danger m-1">Danger</button>
            <button type="button" id="warning" class="btn btn-warning m-1">Warning</button>
            <button type="button" id="info" class="btn btn-info m-1">Info</button>
            <button type="button" id="light" class="btn btn-light m-1">Light</button>
            <button type="button" id="dark" class="btn btn-dark m-1">Dark</button>

            <!-- Alerts -->
            <div
              class="alert fade"
              id="alert-primary"
              role="alert"
              data-mdb-color="primary"
              data-mdb-position="top-right"
              data-mdb-stacking="true"
              data-mdb-width="535px"
              data-mdb-append-to-body="true"
              data-mdb-hidden="true"
              data-mdb-autohide="true"
              data-mdb-delay="2000"
            >
              A simple primary alert with
              <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
            </div>
            <div
              class="alert fade"
              id="alert-secondary"
              role="alert"
              data-mdb-color="secondary"
              data-mdb-position="top-right"
              data-mdb-stacking="true"
              data-mdb-width="535px"
              data-mdb-append-to-body="true"
              data-mdb-hidden="true"
              data-mdb-autohide="true"
              data-mdb-delay="2000"
            >
              A simple secondary alert with
              <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
            </div>
            <div
              class="alert fade"
              id="alert-success"
              role="alert"
              data-mdb-color="success"
              data-mdb-position="top-right"
              data-mdb-stacking="true"
              data-mdb-width="535px"
              data-mdb-width="535px"
              data-mdb-append-to-body="true"
              data-mdb-hidden="true"
              data-mdb-autohide="true"
              data-mdb-delay="2000"
            >
              A simple success alert with
              <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
            </div>
            <div
              class="alert fade"
              id="alert-danger"
              role="alert"
              data-mdb-color="danger"
              data-mdb-position="top-right"
              data-mdb-stacking="true"
              data-mdb-width="535px"
              data-mdb-width="535px"
              data-mdb-append-to-body="true"
              data-mdb-hidden="true"
              data-mdb-autohide="true"
              data-mdb-delay="2000"
            >
              A simple danger alert with
              <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
            </div>
            <div
              class="alert fade"
              id="alert-warning"
              role="alert"
              data-mdb-color="warning"
              data-mdb-position="top-right"
              data-mdb-stacking="true"
              data-mdb-width="535px"
              data-mdb-width="535px"
              data-mdb-append-to-body="true"
              data-mdb-hidden="true"
              data-mdb-autohide="true"
              data-mdb-delay="2000"
            >
              A simple warning alert with
              <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
            </div>
            <div
              class="alert fade"
              id="alert-info"
              role="alert"
              data-mdb-color="info"
              data-mdb-position="top-right"
              data-mdb-stacking="true"
              data-mdb-width="535px"
              data-mdb-width="535px"
              data-mdb-append-to-body="true"
              data-mdb-hidden="true"
              data-mdb-autohide="true"
              data-mdb-delay="2000"
            >
              A simple info alert with
              <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
            </div>
            <div
              class="alert fade"
              id="alert-light"
              role="alert"
              data-mdb-color="light"
              data-mdb-position="top-right"
              data-mdb-stacking="true"
              data-mdb-width="535px"
              data-mdb-width="535px"
              data-mdb-append-to-body="true"
              data-mdb-hidden="true"
              data-mdb-autohide="true"
              data-mdb-delay="2000"
            >
              A simple light alert with
              <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
            </div>
            <div
              class="alert fade"
              id="alert-dark"
              role="alert"
              data-mdb-color="dark"
              data-mdb-position="top-right"
              data-mdb-stacking="true"
              data-mdb-width="535px"
              data-mdb-width="535px"
              data-mdb-append-to-body="true"
              data-mdb-hidden="true"
              data-mdb-autohide="true"
              data-mdb-delay="2000"
            >
              A simple dark alert with
              <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
            </div>
          
        
    
        
            
            const triggers = [
              'primary',
              'secondary',
              'success',
              'danger',
              'warning',
              'info',
              'light',
              'dark',
            ];
            const basicInstances = [
              'alert-primary',
              'alert-secondary',
              'alert-success',
              'alert-danger',
              'alert-warning',
              'alert-info',
              'alert-light',
              'alert-dark',
            ];

            triggers.forEach((trigger, index) => {
              let basicInstance = mdb.Alert.getInstance(document.getElementById(basicInstances[index]));
              document.getElementById(trigger).addEventListener('click', () => {
                basicInstance.show();
              });
            });
          
        
    

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.

        
            
            <div class="alert" role="alert" data-mdb-color="primary">
              A simple primary alert—check it out!
            </div>
            <div class="alert" role="alert" data-mdb-color="secondary">
              A simple secondary alert—check it out!
            </div>
            <div class="alert" role="alert" data-mdb-color="success">
              A simple success alert—check it out!
            </div>
            <div class="alert" role="alert" data-mdb-color="danger">
              A simple danger alert—check it out!
            </div>
            <div class="alert" role="alert" data-mdb-color="warning">
              A simple warning alert—check it out!
            </div>
            <div class="alert" role="alert" data-mdb-color="info">
              A simple info alert—check it out!
            </div>
            <div class="alert" role="alert" data-mdb-color="light">
              A simple light alert—check it out!
            </div>
            <div class="alert" role="alert" data-mdb-color="dark">
              A simple dark alert—check it out!
            </div>
          
        
    


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.

        
            
            <div class="alert" role="alert" data-mdb-color="primary">
              <i class="fas fa-info-circle me-3"></i>A simple primary alert—check it out!
            </div>
            <div class="alert" role="alert" data-mdb-color="secondary">
              <i class="fas fa-circle me-3"></i>A simple secondary alert—check it out!
            </div>
            <div class="alert" role="alert" data-mdb-color="success">
              <i class="fas fa-check-circle me-3"></i>A simple success alert—check it out!
            </div>
            <div class="alert" role="alert" data-mdb-color="danger">
              <i class="fas fa-times-circle me-3"></i>A simple danger alert—check it out!
            </div>
            <div class="alert" role="alert" data-mdb-color="warning">
              <i class="fas fa-exclamation-triangle me-3"></i>A simple warning alert—check it out!
            </div>
            <div class="alert" role="alert" data-mdb-color="info">
              <i class="fas fa-chevron-circle-right me-3"></i>A simple info alert—check it out!
            </div>
            <div class="alert" role="alert" data-mdb-color="light">
              <i class="fab fa-gratipay me-3"></i>A simple light alert—check it out!
            </div>
            <div class="alert" role="alert" data-mdb-color="dark">
              <i class="fas fa-gem me-3"></i>A simple dark alert—check it out!
            </div>
          
        
    

Additional Content

Alerts can contain additional elements like headings, paragraphs and dividers.

        
            
            <div class="alert" role="alert" data-mdb-color="success">
              <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 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:

        
            
            <div class="alert alert-dismissible fade show" role="alert" data-mdb-color="warning">
              <strong>Holy guacamole!</strong> You should check in on some of those fields below.
              <button type="button" class="btn-close" data-mdb-dismiss="alert" aria-label="Close"></button>
            </div>
          
        
    

Show

You can manually show alert using show() method.

        
            
            <div class="alert fade" id="show-example" data-mdb-hidden="true" role="alert" data-mdb-color="primary">
              Hidden alert!
            </div>
          
        
    
        
            
            mdb.Alert.getInstance(document.getElementById('show-example')).show();
          
        
    

Hide

You can manually hide alert using hide() method.

        
            
            <div class="alert fade show mt-3" id="hide-example" role="alert" data-mdb-color="primary">
              Hide me!
            </div>
          
        
    
        
            
            mdb.Alert.getInstance(document.getElementById('hide-example')).hide();
          
        
    

Placement

You can set the position of every alert using the data-mdb-position attribute.

Select horizontal / vertical alignment

Current position: top-right
        
            
            <div
              class="alert fade mt-3"
              id="placement-example"
              data-mdb-position="top-right"
              data-mdb-hidden="true"
              data-mdb-width="25%"
              role="alert"
              data-mdb-color="primary"
            >
              Show me wherever you want!
            </div>
          
        
    
        
            
            mdb.Alert.getInstance(document.getElementById('placement-example')).show();
          
        
    

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.

        
            
            <div
              class="alert fade"
              id="container-example"
              role="alert"
              data-mdb-container="#parent-container-example"
              data-mdb-color="primary"
            >
              Hello from parent element!
            </div>
          
        
    
        
            
            mdb.Alert.getInstance(document.getElementById('container-example')).show();
          
        
    

Offset

You can set offset of your alert using a data-mdb-offset tag.

        
            
            <div
              class="alert fade"
              id="offset-example"
              data-mdb-hidden="true"
              data-mdb-offset="100"
              role="alert"
              data-mdb-color="primary"
            >
              Offset: 100px
            </div>
          
        
    
        
            
            mdb.Alert.getInstance(document.getElementById('offset-example')).show();
          
        
    

Stacking

You can turn on stacking your alerts using the data-mdb-stacking attribute.

        
            
            <div class="container text-center">
              <button type="button" class="btn btn-primary" id="stacking-trigger-example">
                Show notification
              </button>
            </div>
          
        
    
        
            
            let alertCount = 0;

            function* colorGenerator(i) {
              const colors = ['primary', 'warning', 'info', 'success', 'secondary', 'danger', 'light'];

              while (true) {
                yield colors[i];

                i++;

                if (i > colors.length - 1) {
                  i = 0;
                }
              }
            }

            const colorIterator = colorGenerator(0);

            document.getElementById('stacking-trigger-example').addEventListener('click', () => {
              alertCount++;
              color = colorIterator.next().value;

              const alert = document.createElement('div');
              alert.innerHTML = `
                <div class="d-flex justify-content-between">
                  <p class="mb-0"><strong>${alertCount}.</strong> Stacking alerts</p>
                  <button type="button" class="btn-close" data-mdb-dismiss="alert" aria-label="Close"></button>
                </div>
              `;
              alert.classList.add('alert', 'fade');

              document.body.appendChild(alert);

              const alertInstance = new mdb.Alert(alert, {
                color,
                stacking: true,
                hidden: true,
                width: '450px',
                position: 'bottom-right',
                autohide: true,
                delay: 5000,
              });

              alertInstance.show();
            });
          
        
    

Stacking - Container

You can also stack alerts inside the container

        
            
            <div class="container text-center" id="parent-stacking-container-example">
              <button type="button" class="btn btn-primary mb-5" id="stacking-container-trigger-example">
                Show notification
              </button>
            </div>
          
        
    
        
            
            let alertCount = 0;

            function* colorGenerator(i) {
              const colors = ['primary', 'warning', 'info', 'success', 'secondary', 'danger', 'light'];

              while (true) {
                yield colors[i];

                i++;

                if (i > colors.length - 1) {
                  i = 0;
                }
              }
            }

            const colorIterator = colorGenerator(0);

            document
              .getElementById('stacking-container-trigger-example')
              .addEventListener('click', () => {
                alertCount++;
                color = colorIterator.next().value;

                const alert = document.createElement('div');
                alert.innerHTML = `
                  <div class="d-flex justify-content-between">
                    <p class="mb-0"><strong>${alertCount}.</strong> Stacking alerts</p>
                    <button
                      type="button"
                      class="btn-close"
                      data-mdb-dismiss="alert"
                      aria-label="Close"
                    ></button>
                  </div>
                `;
                alert.classList.add('alert', 'fade');

                document.getElementById('parent-stacking-container-example').appendChild(alert);

                const alertInstance = new mdb.Alert(alert, {
                  color,
                  stacking: true,
                  container: '#parent-stacking-container-example',
                  hidden: true,
                  width: '250px',
                  position: 'top-right',
                  autohide: true,
                  delay: 5000,
                });

                alertInstance.show();
              });
          
        
    

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

Dismissal can be achieved with data attributes on a button within the alert as demonstrated below:

        
            
            <button type="button" class="btn-close" data-mdb-dismiss="alert" aria-label="Close"></button>
          
        
    

or on a button outside the alert using the data-mdb-target as demonstrated below:

        
            
            <button type="button" class="btn-close" data-mdb-dismiss="alert" data-mdb-target="#my-alert" aria-label="Close"></button>
          
        
    

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)
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'})
        
            
          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';