Toasts MDB Pro component

Toasts - Bootstrap 5 & Material Design 2.0 components

Push notifications to your visitors with a 'toast', a lightweight and easily customizable alert message. Toasts are designed to mimic the push notifications that have been popularized by mobile and desktop operating systems.

Note: Read the API tab to find all available options and advanced customization


Basic example

Click the buttons to launch the toasts.


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

          <!-- Toasts -->
          <div
            class="toast fade mx-auto"
            id="basic-primary-example"
            role="alert"
            aria-live="assertive"
            aria-atomic="true"
            data-autohide="true"
            data-delay="2000"
            data-position="top-right"
            data-append-to-body="true"
            data-stacking="true"
            data-width="350px"
            data-color="primary"
          >
            <div class="toast-header text-white">
              <strong class="mr-auto">MDBootstrap</strong>
              <small>11 mins ago</small>
              <button type="button" class="btn-close btn-close-white" data-dismiss="toast" aria-label="Close"></button>
            </div>
            <div class="toast-body text-white">Primary Basic Example</div>
          </div>

          <div
            class="toast fade mx-auto"
            id="basic-secondary-example"
            role="alert"
            aria-live="assertive"
            aria-atomic="true"
            data-autohide="true"
            data-delay="2000"
            data-position="top-right"
            data-append-to-body="true"
            data-stacking="true"
            data-width="350px"
            data-color="secondary"
          >
            <div class="toast-header text-white">
              <strong class="mr-auto">MDBootstrap</strong>
              <small>11 mins ago</small>
              <button type="button" class="btn-close btn-close-white" data-dismiss="toast" aria-label="Close"></button>
            </div>
            <div class="toast-body text-white">Secondary Basic Example</div>
          </div>

          <div
            class="toast fade mx-auto"
            id="basic-success-example"
            role="alert"
            aria-live="assertive"
            aria-atomic="true"
            data-autohide="true"
            data-delay="2000"
            data-position="top-right"
            data-append-to-body="true"
            data-stacking="true"
            data-width="350px"
            data-color="success"
          >
            <div class="toast-header text-white">
              <strong class="mr-auto">MDBootstrap</strong>
              <small>11 mins ago</small>
              <button type="button" class="btn-close btn-close-white" data-dismiss="toast" aria-label="Close"></button>
            </div>
            <div class="toast-body text-white">Success Basic Example</div>
          </div>

          <div
            class="toast fade mx-auto"
            id="basic-danger-example"
            role="alert"
            aria-live="assertive"
            aria-atomic="true"
            data-autohide="true"
            data-delay="2000"
            data-position="top-right"
            data-append-to-body="true"
            data-stacking="true"
            data-width="350px"
            data-color="danger"
          >
            <div class="toast-header text-white">
              <strong class="mr-auto">MDBootstrap</strong>
              <small>11 mins ago</small>
              <button type="button" class="btn-close btn-close-white" data-dismiss="toast" aria-label="Close"></button>
            </div>
            <div class="toast-body text-white">Danger Basic Example</div>
          </div>

          <div
            class="toast fade mx-auto"
            id="basic-warning-example"
            role="alert"
            aria-live="assertive"
            aria-atomic="true"
            data-autohide="true"
            data-delay="2000"
            data-position="top-right"
            data-append-to-body="true"
            data-stacking="true"
            data-width="350px"
            data-color="warning"
          >
            <div class="toast-header text-white">
              <strong class="mr-auto">MDBootstrap</strong>
              <small>11 mins ago</small>
              <button type="button" class="btn-close btn-close-white" data-dismiss="toast" aria-label="Close"></button>
            </div>
            <div class="toast-body text-white">Warning Basic Example</div>
          </div>

          <div
            class="toast fade mx-auto"
            id="basic-info-example"
            role="alert"
            aria-live="assertive"
            aria-atomic="true"
            data-autohide="true"
            data-delay="2000"
            data-position="top-right"
            data-append-to-body="true"
            data-stacking="true"
            data-width="350px"
            data-color="info"
          >
            <div class="toast-header text-white">
              <strong class="mr-auto">MDBootstrap</strong>
              <small>11 mins ago</small>
              <button type="button" class="btn-close btn-close-white" data-dismiss="toast" aria-label="Close"></button>
            </div>
            <div class="toast-body text-white">Info Basic Example</div>
          </div>

          <div
            class="toast fade mx-auto"
            id="basic-light-example"
            role="alert"
            aria-live="assertive"
            aria-atomic="true"
            data-autohide="true"
            data-delay="2000"
            data-position="top-right"
            data-append-to-body="true"
            data-stacking="true"
            data-width="350px"
            data-color="light"
          >
            <div class="toast-header">
              <strong class="mr-auto">MDBootstrap</strong>
              <small>11 mins ago</small>
              <button type="button" class="btn-close" data-dismiss="toast" aria-label="Close"></button>
            </div>
            <div class="toast-body">Light Basic Example</div>
          </div>

          <div
            class="toast fade mx-auto"
            id="basic-dark-example"
            role="alert"
            aria-live="assertive"
            aria-atomic="true"
            data-autohide="true"
            data-delay="2000"
            data-position="top-right"
            data-append-to-body="true"
            data-stacking="true"
            data-width="350px"
            data-color="dark"
          >
            <div class="toast-header text-white">
              <strong class="mr-auto">MDBootstrap</strong>
              <small>11 mins ago</small>
              <button type="button" class="btn-close btn-close-white" data-dismiss="toast" aria-label="Close"></button>
            </div>
            <div class="toast-body text-white">Dark Basic Example</div>
          </div>
        

          const toasts = [ 'basic-primary-example', 
          'basic-secondary-example',
          'basic-success-example',
          'basic-danger-example',
          'basic-warning-example',
          'basic-info-example',
          'basic-light-example',
          'basic-dark-example',
          ];
          
          const triggers = [
          'basic-primary-trigger',
          'basic-secondary-trigger',
          'basic-success-trigger',
          'basic-danger-trigger',
          'basic-warning-trigger',
          'basic-info-trigger',
          'basic-light-trigger',
          'basic-dark-trigger',
          ];
          
          triggers.forEach((trigger, index) => {
            let basicInstance = mdb.Toast.getInstance(document.getElementById(toasts[index]));
            document.getElementById(trigger).addEventListener('click', () => {
              basicInstance.show();
            });
          });
        

Static example


          <div
            class="toast show fade mx-auto"
            id="static-example"
            role="alert"
            aria-live="assertive"
            aria-atomic="true"
            data-autohide="false"
          >
            <div class="toast-header">
              <strong class="mr-auto">MDBootstrap</strong>
              <small>11 mins ago</small>
              <button
                type="button"
                class="btn-close"
                data-dismiss="toast"
                aria-label="Close"
              ></button>
            </div>
            <div class="toast-body">Static Example</div>
          </div>
        

          const staticInstance = mdb.Toast.getInstance(document.getElementById('static-example'));
          staticInstance.show();
        

Colors


          <div class="row mb-5">
            <div class="col-md-3">
              <div
                class="toast show fade text-white"
                role="alert"
                aria-live="assertive"
                aria-atomic="true"
                data-color="info"
              >
                <div class="toast-header text-white">
                  <i class="fas fa-info-circle fa-lg mr-2"></i>
                  <strong class="mr-auto">MDBootstrap</strong>
                  <small>11 mins ago</small>
                  <button
                    type="button"
                    class="btn-close btn-close-white"
                    data-dismiss="toast"
                    aria-label="Close"
                  ></button>
                </div>
                <div class="toast-body">Hello, world! This is a toast message.</div>
              </div>
            </div>
            <div class="col-md-3">
              <div
                class="toast show fade text-white"
                role="alert"
                aria-live="assertive"
                aria-atomic="true"
                data-color="warning"
              >
                <div class="toast-header text-white">
                  <i class="fas fa-exclamation-triangle fa-lg mr-2"></i>
                  <strong class="mr-auto">MDBootstrap</strong>
                  <small>11 mins ago</small>
                  <button
                    type="button"
                    class="btn-close btn-close-white"
                    data-dismiss="toast"
                    aria-label="Close"
                  ></button>
                </div>
                <div class="toast-body">Hello, world! This is a toast message.</div>
              </div>
            </div>
            <div class="col-md-3">
              <div
                class="toast show fade text-white"
                role="alert"
                aria-live="assertive"
                aria-atomic="true"
                data-color="success"
              >
                <div class="toast-header text-white">
                  <i class="fas fa-check fa-lg mr-2"></i>
                  <strong class="mr-auto">MDBootstrap</strong>
                  <small>11 mins ago</small>
                  <button
                    type="button"
                    class="btn-close btn-close-white"
                    data-dismiss="toast"
                    aria-label="Close"
                  ></button>
                </div>
                <div class="toast-body">Hello, world! This is a toast message.</div>
              </div>
            </div>
            <div class="col-md-3">
              <div
                class="toast show fade text-white"
                role="alert"
                aria-live="assertive"
                aria-atomic="true"
                data-color="danger"
              >
                <div class="toast-header text-white">
                  <i class="fas fa-exclamation-circle fa-lg mr-2"></i>
                  <strong class="mr-auto">MDBootstrap</strong>
                  <small>11 mins ago</small>
                  <button
                    type="button"
                    class="btn-close btn-close-white"
                    data-dismiss="toast"
                    aria-label="Close"
                  ></button>
                </div>
                <div class="toast-body">Hello, world! This is a toast message.</div>
              </div>
            </div>
          </div>
        

          const instance = mdb.Toast.getInstance(document.getElementById('example'));
          instance.update({ color: 'info / warning / success / danger', });
        

Placement

You can set position of every notification using data-position attribute or update() method.

Select horizontal / vertical alignment

Current position: top-right

          <div
            class="toast text-white"
            id="placement-example-toast"
            role="alert"
            aria-live="assertive"
            aria-atomic="true"
            data-autohide="false"
            data-position="..."
          >
            <div class="toast-header text-white">
              <strong class="mr-auto">MDBootstrap</strong>
              <small>11 mins ago</small>
              <button
                type="button"
                class="btn-close btn-close-white"
                data-dismiss="toast"
                aria-label="Close"
              ></button>
            </div>
            <div class="toast-body">Toast position</div>
          </div>
        

          const instance = mdb.Toast.getInstance(document.getElementById('position-example-toast'));
          instance.update({ position: 'top-right', });
        

Offset

You can also change offset of every notification using data-offset attribute or update() method.


          <div
            class="toast text-white"
            id="offset-toast"
            role="alert"
            aria-live="assertive"
            aria-atomic="true"
            data-autohide="false"
            data-offset="..."
            data-color="info"
          >
            <div class="toast-header text-white">
              <strong class="mr-auto">MDBootstrap</strong>
              <small>11 mins ago</small>
              <button
                type="button"
                class="btn-close btn-close-white"
                data-dismiss="toast"
                aria-label="Close"
              ></button>
            </div>
            <div class="toast-body">Toast offset</div>
          </div>
        

          const instance = mdb.Toast.getInstance(document.getElementById('offset-toast'));
          instance.update({ offset: 50, });
        

Container

You can display notification anywhere. Just put your toast in your target element and fill data-container attribute with id or class of parent or update() method.


          <div id="parent">
            <div
              class="toast"
              id="container-example-toast"
              role="alert"
              aria-live="assertive"
              aria-atomic="true"
              data-autohide="false"
              data-container="..."
            >
              <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"
                  role="img"
                  focusable="false"
                >
                  <rect width="100%" height="100%" fill="#007aff"></rect>
                </svg>
                <strong class="mr-auto">MDBootstrap</strong>
                <small>11 mins ago</small>
                <button
                  type="button"
                  class="btn-close"
                  data-dismiss="toast"
                  aria-label="Close"
                ></button>
              </div>
              <div class="toast-body">Toast container</div>
            </div>
          </div>
        

          const instance = mdb.Toast.getInstance(document.getElementById('container-example-toast'));
          instance.update({ container: '#parent', });
        

Stacking

You can turn on/off stacking your notifications using data-stacking attribute or update() method.


          <div
            class="toast text-white"
            id="stacking-toast-one"
            role="alert"
            aria-live="assertive"
            aria-atomic="true"
            data-autohide="true"
            data-delay="2000"
            data-position="top-right"
            data-color="info"
          >
            <div class="toast-header text-white">
              <strong class="mr-auto">MDBootstrap</strong>
              <small>11 mins ago</small>
              <button
                type="button"
                class="btn-close btn-close-white"
                data-dismiss="toast"
                aria-label="Close"
              ></button>
            </div>
            <div class="toast-body">First stacking element</div>
          </div>
          <div
            class="toast text-white"
            id="stacking-toast-two"
            role="alert"
            aria-live="assertive"
            aria-atomic="true"
            data-autohide="true"
            data-delay="2000"
            data-position="top-right"
            data-color="info"
          >
            <div class="toast-header text-white">
              <strong class="mr-auto">MDBootstrap</strong>
              <small>11 mins ago</small>
              <button
                type="button"
                class="btn-close btn-close-white"
                data-dismiss="toast"
                aria-label="Close"
              ></button>
            </div>
            <div class="toast-body">Second stacking element</div>
          </div>
        

          const instance = mdb.Toast.getInstance(document.getElementById('stacking-toast-one'));
          const secondInstance = mdb.Toast.getInstance(document.getElementById('stacking-toast-two')); 
          instance.update({ stacking: true, }); 
          secondInstance.update({ stacking: true, });
        

Toasts - API


Usage

Via data attributes


        <div
          class="toast"
          id="example-toast"
          role="alert"
          aria-live="assertive"
          aria-atomic="true"
          data-autohide="false"
          data-position="..."
          data-container="..."
          data-offset="..."
        >
          <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"
              role="img"
              focusable="false"
            >
              <rect width="100%" height="100%" fill="#007aff"></rect>
            </svg>
            <strong class="mr-auto">MDBootstrap</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">
            Example toast
          </div>
        </div>
      

Via JavaScript


      mdb.Toast.getInstance(document.getElementById('placement-example-toast')).update({
        position: ...,
        offset: ...,
        container: ...,
      });
      

Via jQuery

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


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

Options

Name Type Default Description
width String / null null Defines the width of a toast
position String 'top-left' Changes position of toast
color String Sets background of a toast (basic MDB colors)
offset Number '10' Changes offset of toast
container String - Defines id/class of parent element
autohide Boolean true Toasts will hide automatically or not
animation Boolean true Toasts will be animated or not
delay Boolean 500 Sets length of animation delay
appendToBody Boolean false Appends element to the end of body
stacking Boolean true Enables stacking notifications

Methods

Name Parameters Description Example
show - Shows toast instance.show()
hide - Hides toast instance.hide()
dispose - Removes a mdb.Toast instance instance.dispose()
update options (Object) Updates options of mdb.Toast instance instance.update({position: 'top-center'})

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

Events

Name Description
show.bs.toast Emitted when a toast has been toggled
shown.bs.toast Emitted once toast is shown
hide.bs.toast Emitted when a toast has been toggled
hidden.bs.toast Emitted once toast is hidden

        const instance = mdb.Toast.getInstance(document.getElementById('show-example'));
        instance.addEventListener('hidden.bs.toast', () => { // do something });
      

Import

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


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