Notification / Toast

Bootstrap notification / toast

Note: This documentation is for an older version of Bootstrap (v.4). A newer version is available for Bootstrap 5. We recommend migrating to the latest version of our product - Material Design for Bootstrap 5.
Go to docs v.5

Push notifications to your visitors with a 'toast', a lightweight and easily customizable alert message.

Toasts are lightweight notifications designed to mimic the push notifications that have been popularized by mobile and desktop operating systems. They’re built with flexbox, so they’re easy to align and position.


Bootstrap example

To encourage extensible and predictable toasts, we recommend that you a header and body. Toast headers use display: flex, allowing easy alignment of content thanks to our margin and flexbox utilities.

Toasts are as flexible as you need and have very little required markup. At a minimum, we require a single element to contain your “toasted” content and strongly encourage a dismiss button.

        
            

            <div role="alert" aria-live="assertive" aria-atomic="true" class="toast" data-autohide="false">
              <div class="toast-header">
                <svg class=" rounded mr-2" width="20" height="20" xmlns="http://www.w3.org/2000/svg"
                  preserveAspectRatio="xMidYMid slice" focusable="false" role="img">
                  <rect fill="#007aff" width="100%" height="100%" /></svg>
                <strong class="mr-auto">Bootstrap</strong>
                <small>11 mins ago</small>
                <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="toast-body">
                Hello, world! This is a toast message.
              </div>
            </div>

          
        
    
        
            
            $('.toast').toast('show');
          
        
    

Material example MDB Pro component

        
            

            <!-- Info message -->
            <a class="btn btn-info" onclick="toastr.info('Hi! I am info message.');">Info message</a>
            <!-- Warning message -->
            <a class="btn btn-warning" onclick="toastr.warning('Hi! I am warning message.');">Warning message</a>
            <!-- Success message -->
            <a class="btn btn-success" onclick="toastr.success('Hi! I am success message.');">Success message</a>
            <!-- Error message -->
            <a class="btn btn-danger" onclick="toastr.error('Hi! I am error message.');">Error message</a>

          
        
    

Translucent

Toasts are slightly translucent, too, so they blend over whatever they might appear over. For browsers that support the backdrop-filter CSS property, we’ll also attempt to blur the elements under a toast.

        
            

        <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
          <div class="toast-header">
            <svg class=" rounded mr-2" width="20" height="20" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice"
              focusable="false" role="img">
              <rect fill="#007aff" width="100%" height="100%" /></svg>
            <strong class="mr-auto">Bootstrap</strong>
            <small class="text-muted">11 mins ago</small>
            <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="toast-body">
            Hello, world! This is a toast message.
          </div>
        </div>

      
        
    

Stacking

When you have multiple toasts, we default to vertically stacking them in a readable manner.

        
            

        <div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-autohide="false">
          <div class="toast-header">
            <svg class="rounded mr-2" width="20" height="20" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice"
              focusable="false" role="img">
              <rect fill="#007aff" width="100%" height="100%" /></svg>
            <strong class="mr-auto">Bootstrap</strong>
            <small class="text-muted">just now</small>
            <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="toast-body">
            See? Just like this.
          </div>
        </div>

        <div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-autohide="false">
          <div class="toast-header">
            <svg class="rounded mr-2" width="20" height="20" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice"
              focusable="false" role="img">
              <rect fill="#007aff" width="100%" height="100%" /></svg>
            <strong class="mr-auto">Bootstrap</strong>
            <small class="text-muted">2 seconds ago</small>
            <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="toast-body">
            Heads up, toasts will stack automatically
          </div>
        </div>

      
        
    

Placement

Place toasts with custom CSS as you need them. The top right is often used for notifications, as is the top middle. If you’re only ever going to show one toast at a time, put the positioning style right on the .toast.

Bootstrap 11 mins ago
Hello, world! This is a toast message.
        
            

        <div aria-live="polite" aria-atomic="true" style="position: relative; min-height: 200px;">
          <div class="toast" style="position: absolute; top: 0; right: 0;" data-autohide="false">
            <div class="toast-header">
              <svg class="rounded mr-2" width="20" height="20" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice"
                focusable="false" role="img">
                <rect fill="#007aff" width="100%" height="100%" /></svg>
              <strong class="mr-auto">Bootstrap</strong>
              <small>11 mins ago</small>
              <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="toast-body">
              Hello, world! This is a toast message.
            </div>
          </div>
        </div>
      
        
    

For systems that generate more notifications, consider using a wrapping element so they can easily stack.

        
            

        <div aria-live="polite" aria-atomic="true" style="position: relative; min-height: 200px;">
          <!-- Position it -->
          <div style="position: absolute; top: 0; right: 0;">

            <!-- Then put toasts within -->
            <div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-autohide="false">
              <div class="toast-header">
                <svg class="rounded mr-2" width="20" height="20" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice"
                  focusable="false" role="img">
                  <rect fill="#007aff" width="100%" height="100%" /></svg>
                <strong class="mr-auto">Bootstrap</strong>
                <small class="text-muted">just now</small>
                <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="toast-body">
                See? Just like this.
              </div>
            </div>

            <div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-autohide="false">
              <div class="toast-header">
                <svg class="rounded mr-2" width="20" height="20" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice"
                  focusable="false" role="img">
                  <rect fill="#007aff" width="100%" height="100%" /></svg>
                <strong class="mr-auto">Bootstrap</strong>
                <small class="text-muted">2 seconds ago</small>
                <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="toast-body">
                Heads up, toasts will stack automatically
              </div>
            </div>
          </div>
        </div>
      
        
    

Accessibility

Toasts are intended to be small interruptions to your visitors or users, so to help those with screen readers and similar assistive technologies, you should wrap your toasts in an aria-live region. Changes to live regions (such as injecting/updating a toast component) are automatically announced by screen readers without needing to move the user’s focus or otherwise interrupt the user. Additionally, include aria-atomic="true" to ensure that the entire toast is always announced as a single (atomic) unit, rather than announcing what was changed (which could lead to problems if you only update part of the toast’s content, or when displaying the same toast content at a later point in time). If the information needed is important for the process, e.g. for a list of errors in a form, then use the alert component instead of toast.

Note that the live region needs to be present in the markup before the toast is generated or updated. If you dynamically generate both at the same time and inject them into the page, they will generally not be announced by assistive technologies.

You also need to adapt the role and aria-live level depending on the content. If it’s an important message like an error, use role="alert" aria-live="assertive", otherwise use the role="status" aria-live="polite" attributes.

As the content you’re displaying changes, be sure to update the delay timeout to ensure people have enough time to read the toast.

        
            

        <div class="toast" role="alert" aria-live="polite" aria-atomic="true" data-delay="10000">
          <div role="alert" aria-live="assertive" aria-atomic="true">...</div>
        </div>

      
        
    

When using autohide: false, you must add a close button to allow users to dismiss the toast.

        
            

        <div role="alert" aria-live="assertive" aria-atomic="true" class="toast" data-autohide="false">
          <div class="toast-header">
            <svg class="bd-placeholder-img rounded mr-2" width="20" height="20" xmlns="http://www.w3.org/2000/svg"
              preserveAspectRatio="xMidYMid slice" focusable="false" role="img">
              <rect fill="#007aff" width="100%" height="100%" /></svg>
            <strong class="mr-auto">Bootstrap</strong>
            <small>11 mins ago</small>
            <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="toast-body">
            Hello, world! This is a toast message.
          </div>
        </div>
      
        
    

JavaScript behavior


Usage

Initialize toasts via JavaScript:

        
            

        $('.toast').toast(option)
      
        
    

Usage via jQuery MDB Pro component"

        
            

        <a class="btn btn-info btn-lg" id="alert-target">Click me!</a>

      
        
    
        
            

        $("#alert-target").click(function () {
          toastr["info"]("I was launched via jQuery!")
        });

      
        
    

Options


Bootstrap Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-animation="".

Name Type Default Description
animation boolean true Apply a CSS fade transition to the toast
autohide boolean true Auto-hide the toast
delay number 500 Delay hiding the toast (ms)

Material Options MDB Pro component"

        
            

        toastr.options = {
          "closeButton": true, // true/false
          "debug": false, // true/false
          "newestOnTop": false, // true/false
          "progressBar": false, // true/false
          "positionClass": "md-toast-top-right", // md-toast-top-right / md-toast-top-left / md-toast-bottom-right /
          md-toast-bottom-left
          "preventDuplicates": false, true/false
          "onclick": null,
          "showDuration": "300", // in milliseconds
          "hideDuration": "1000", // in milliseconds
          "timeOut": "5000", // in milliseconds
          "extendedTimeOut": "1000", // in milliseconds
          "showEasing": "swing",
          "hideEasing": "linear",
          "showMethod": "fadeIn",
          "hideMethod": "fadeOut"
        }

      
        
    

Other options MDB Pro component"

        
            

        // Display a warning toast, with no title
        toastr.warning('My name is Inigo Montoya. You killed my father, prepare to die!')

        // Display a success toast, with a title
        toastr.success('Have fun storming the castle!', 'Miracle Max Says')

        // Display an error toast, with a title
        toastr.error('I do not think that word means what you think it means.', 'Inconceivable!')

        // Immediately remove current toasts without using animation
        toastr.remove()

        // Remove current toasts using animation
        toastr.clear()

        // Override global options
        toastr.success('We do have the Kapua suite available.', 'Turtle Bay Resort', {timeOut: 5000})

      
        
    

Methods

Asynchronous methods and transitions

All API methods are asynchronous and start a transition. They return to the caller as soon as the transition is started but before it ends. In addition, a method call on a transitioning component will be ignored.

$().toast(options)

Attaches a toast handler to an element collection.

.toast('show')

Reveals an element’s toast. It returns to the caller before the toast has actually been shown (i.e. before the shown.bs.toast event occurs). You have to manually call this method, otherwise, your toast won’t show.

        
            

        $('#element').toast('show')

      
        
    

.toast('hide')

Hides an element’s toast. Returns to the caller before the toast has actually been hidden (i.e. before the hidden.bs.toast event occurs). You have to manually call this method if you made autohide to false.

        
            

        $('#element').toast('hide')

      
        
    

.toast('dispose')

Hides an element’s toast. Your toast will remain on the DOM but won’t show anymore.

        
            

        $('#element').toast('dispose')

      
        
    

Events

Event Type Description
show.bs.toast This event fires immediately when the show instance method is called.
shown.bs.toast This event is fired when the toast has been made visible to the user.
hide.bs.toast This event is fired immediately when the hide instance method has been called.
hidden.bs.toast This event is fired when the toast has finished being hidden from the user.
        
            

        $('#myToast').on('hidden.bs.toast', function () {
          // do something…
        })

      
        
    

Notification creator MDB Pro component

toastr creator

Toast Type
Position


        
        
            

        toastr.options = {
          "closeButton": false,
          "debug": false,
          "newestOnTop": false,
          "progressBar": false,
          "positionClass": "md-toast-top-right",
          "preventDuplicates": false,
          "onclick": null,
          "showDuration": 300,
          "hideDuration": 1000,
          "timeOut": 5000,
          "extendedTimeOut": 1000,
          "showEasing": "swing",
          "hideEasing": "linear",
          "showMethod": "fadeIn",
          "hideMethod": "fadeOut"
        }