Alerts

Provide contextual feedback messages for typical user actions with the handful of available and flexible alert messages.

Bootstrap Alerts documentation

Default style

Use .alert-* classes.

                    
                      <div class="alert alert-primary alert-dismissible fade show pe-5" role="alert">
                        This is a primary alert—check it out!
                        <button type="button" class="close p-2" data-dismiss="alert" aria-label="Close">
                          <svg xmlns="http://www.w3.org/2000/svg" width="11.528" height="12" viewBox="0 0 11.528 12">
                            <path d="M.788.1l.055.05,4.92,5.122L10.684.151a.48.48,0,0,1,.7,0,.53.53,0,0,1,.048.67l-.048.058L6.463,6l4.92,5.122a.529.529,0,0,1,0,.727.48.48,0,0,1-.643.05l-.055-.05L5.764,6.728.843,11.849a.48.48,0,0,1-.7,0A.53.53,0,0,1,.1,11.18l.048-.058L5.065,6,.145.878a.529.529,0,0,1,0-.727A.48.48,0,0,1,.721.057Z" fill="#fff"/>
                          </svg>
                        </button>
                      </div>
                      <div class="alert alert-secondary alert-dismissible fade show pe-5" role="alert">
                        This is a secondary alert—check it out!
                        <button type="button" class="close p-2" data-dismiss="alert" aria-label="Close">
                          <svg xmlns="http://www.w3.org/2000/svg" width="11.528" height="12" viewBox="0 0 11.528 12">
                            <path d="M.788.1l.055.05,4.92,5.122L10.684.151a.48.48,0,0,1,.7,0,.53.53,0,0,1,.048.67l-.048.058L6.463,6l4.92,5.122a.529.529,0,0,1,0,.727.48.48,0,0,1-.643.05l-.055-.05L5.764,6.728.843,11.849a.48.48,0,0,1-.7,0A.53.53,0,0,1,.1,11.18l.048-.058L5.065,6,.145.878a.529.529,0,0,1,0-.727A.48.48,0,0,1,.721.057Z" fill="#1E1E1E"/>
                          </svg>
                        </button>
                      </div>
                      <div class="alert alert-success alert-dismissible fade show pe-5" role="alert">
                        This is a success alert—check it out!
                        <button type="button" class="close p-2" data-dismiss="alert" aria-label="Close">
                          <svg xmlns="http://www.w3.org/2000/svg" width="11.528" height="12" viewBox="0 0 11.528 12">
                            <path d="M.788.1l.055.05,4.92,5.122L10.684.151a.48.48,0,0,1,.7,0,.53.53,0,0,1,.048.67l-.048.058L6.463,6l4.92,5.122a.529.529,0,0,1,0,.727.48.48,0,0,1-.643.05l-.055-.05L5.764,6.728.843,11.849a.48.48,0,0,1-.7,0A.53.53,0,0,1,.1,11.18l.048-.058L5.065,6,.145.878a.529.529,0,0,1,0-.727A.48.48,0,0,1,.721.057Z" fill="#fff"/>
                          </svg>
                        </button>
                      </div>
                      <div class="alert alert-danger alert-dismissible fade show pe-5" role="alert">
                        This is a danger alert—check it out!
                        <button type="button" class="close p-2" data-dismiss="alert" aria-label="Close">
                          <svg xmlns="http://www.w3.org/2000/svg" width="11.528" height="12" viewBox="0 0 11.528 12">
                            <path d="M.788.1l.055.05,4.92,5.122L10.684.151a.48.48,0,0,1,.7,0,.53.53,0,0,1,.048.67l-.048.058L6.463,6l4.92,5.122a.529.529,0,0,1,0,.727.48.48,0,0,1-.643.05l-.055-.05L5.764,6.728.843,11.849a.48.48,0,0,1-.7,0A.53.53,0,0,1,.1,11.18l.048-.058L5.065,6,.145.878a.529.529,0,0,1,0-.727A.48.48,0,0,1,.721.057Z" fill="#fff"/>
                          </svg>
                        </button>
                      </div>
                      <div class="alert alert-warning alert-dismissible fade show pe-5" role="alert">
                        This is a warning alert—check it out!
                        <button type="button" class="close p-2" data-dismiss="alert" aria-label="Close">
                          <svg xmlns="http://www.w3.org/2000/svg" width="11.528" height="12" viewBox="0 0 11.528 12">
                            <path d="M.788.1l.055.05,4.92,5.122L10.684.151a.48.48,0,0,1,.7,0,.53.53,0,0,1,.048.67l-.048.058L6.463,6l4.92,5.122a.529.529,0,0,1,0,.727.48.48,0,0,1-.643.05l-.055-.05L5.764,6.728.843,11.849a.48.48,0,0,1-.7,0A.53.53,0,0,1,.1,11.18l.048-.058L5.065,6,.145.878a.529.529,0,0,1,0-.727A.48.48,0,0,1,.721.057Z" fill="#1E1E1E"/>
                          </svg>
                        </button>
                      </div>
                      <div class="alert alert-info alert-dismissible fade show pe-5" role="alert">
                        This is a info alert—check it out!
                        <button type="button" class="close p-2" data-dismiss="alert" aria-label="Close">
                          <svg xmlns="http://www.w3.org/2000/svg" width="11.528" height="12" viewBox="0 0 11.528 12">
                            <path d="M.788.1l.055.05,4.92,5.122L10.684.151a.48.48,0,0,1,.7,0,.53.53,0,0,1,.048.67l-.048.058L6.463,6l4.92,5.122a.529.529,0,0,1,0,.727.48.48,0,0,1-.643.05l-.055-.05L5.764,6.728.843,11.849a.48.48,0,0,1-.7,0A.53.53,0,0,1,.1,11.18l.048-.058L5.065,6,.145.878a.529.529,0,0,1,0-.727A.48.48,0,0,1,.721.057Z" fill="#1E1E1E"/>
                          </svg>
                        </button>
                      </div>
                      <div class="alert alert-light alert-dismissible fade show pe-5" role="alert">
                        This is a light alert—check it out!
                        <button type="button" class="close p-2" data-dismiss="alert" aria-label="Close">
                          <svg xmlns="http://www.w3.org/2000/svg" width="11.528" height="12" viewBox="0 0 11.528 12">
                            <path d="M.788.1l.055.05,4.92,5.122L10.684.151a.48.48,0,0,1,.7,0,.53.53,0,0,1,.048.67l-.048.058L6.463,6l4.92,5.122a.529.529,0,0,1,0,.727.48.48,0,0,1-.643.05l-.055-.05L5.764,6.728.843,11.849a.48.48,0,0,1-.7,0A.53.53,0,0,1,.1,11.18l.048-.058L5.065,6,.145.878a.529.529,0,0,1,0-.727A.48.48,0,0,1,.721.057Z" fill="#1E1E1E"/>
                          </svg>
                        </button>
                      </div>
                      <div class="alert alert-dark alert-dismissible fade show pe-5" role="alert">
                        This is a dark alert—check it out!
                        <button type="button" class="close p-2" data-dismiss="alert" aria-label="Close">
                          <svg xmlns="http://www.w3.org/2000/svg" width="11.528" height="12" viewBox="0 0 11.528 12">
                            <path d="M.788.1l.055.05,4.92,5.122L10.684.151a.48.48,0,0,1,.7,0,.53.53,0,0,1,.048.67l-.048.058L6.463,6l4.92,5.122a.529.529,0,0,1,0,.727.48.48,0,0,1-.643.05l-.055-.05L5.764,6.728.843,11.849a.48.48,0,0,1-.7,0A.53.53,0,0,1,.1,11.18l.048-.058L5.065,6,.145.878a.529.529,0,0,1,0-.727A.48.48,0,0,1,.721.057Z" fill="#fff"/>
                          </svg>
                        </button>
                      </div>
                    
                  

With Icon

                    
                      <div class="alert alert-primary media" role="alert">
                        <i class="fas fa-info-circle mt-1 me-1"></i>
                        <div class="media-body" role="alert">
                          This is a primary alert with <a href="#0" class="alert-link">an example link</a>. Give it a click if you like.
                        </div>
                      </div>
                    
                  

Dismissing

Use .alert-dismissible class for .alert.

                    
                      <div class="alert alert-warning alert-dismissible fade show pe-5" role="alert">
                        <strong>Holy guacamole!</strong> You should check in on some of those fields below.
                        <button type="button" class="close p-2" data-dismiss="alert" aria-label="Close">
                          <svg xmlns="http://www.w3.org/2000/svg" width="11.528" height="12" viewBox="0 0 11.528 12">
                            <path d="M.788.1l.055.05,4.92,5.122L10.684.151a.48.48,0,0,1,.7,0,.53.53,0,0,1,.048.67l-.048.058L6.463,6l4.92,5.122a.529.529,0,0,1,0,.727.48.48,0,0,1-.643.05l-.055-.05L5.764,6.728.843,11.849a.48.48,0,0,1-.7,0A.53.53,0,0,1,.1,11.18l.048-.058L5.065,6,.145.878a.529.529,0,0,1,0-.727A.48.48,0,0,1,.721.057Z" fill="#1E1E1E"/>
                          </svg>
                        </button>
                      </div>
                    
                  

Additional content

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

                    
                      <div class="alert alert-success" role="alert">
                        <h4 class="alert-heading">Well done!</h4>
                        <p class="text-inherit">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="text-inherit mb-0">Whenever you need to, be sure to use margin utilities to keep things nice and tidy.</p>
                      </div>
                    
                  

JavaScript behavior

Triggers

Enable dismissal of an alert via JavaScript:

              
                var alertList = document.querySelectorAll('.alert')
                alertList.forEach(function (alert) {
                  new bootstrap.Alert(alert)
                })
              
            

Or with data attributes on a button within the alert, as demonstrated above:

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

Note that closing an alert will remove it from the DOM.

Methods

You can create an alert instance with the alert constructor, for example:

              
                var myAlert = document.getElementById('myAlert')
                var bsAlert = new bootstrap.Alert(myAlert)
              
            

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)
              
                var alertNode = document.querySelector('.alert')
                var alert = bootstrap.Alert.getInstance(alertNode)
                alert.close()
              
            

Events

Bootstrap’s alert plugin exposes a few events for hooking into alert functionality.

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.
              
                var myAlert = document.getElementById('myAlert')
                myAlert.addEventListener('closed.bs.alert', function () {
                  // do something, for instance, explicitly move focus to the most appropriate element,
                  // so it doesn't get lost/reset to the start of the page
                  // document.getElementById('...').focus()
                })