Bootstrap 5 Toast

Toast (Toast)

The Toast component is similar to a warning box, which will only display for a few seconds when something happens (such as when the user clicks a button, submits a form, etc.).

How to Create Toast

To create Toast, please use .toast classes, and add .toast-header and .toast-body.

Note:By default, toast is hidden. To display it, please use .show class. To close it, please use the <button> element and add data-bs-dismiss="toast":

<div class="toast show">
  <div class="toast-header">
    Toast Title
    <button type="button" class="btn-close" data-bs-dismiss="toast"></button>
  </div>
  <div class="toast-body">
    Text within the Toast body.
  </div>
</div>

Try It Yourself

Open Toast

To display Toast by clicking a button, you must initialize it with JavaScript: please select the specified element and call toast() Methods.

When you click the button, the following code will display all toast in the document:

Example

<script>
document.getElementById("toastbtn").onclick = function() {
  var toastElList = [].slice.call(document.querySelectorAll('.toast'))
  var toastList = toastElList.map(function(toastEl) {
    return new bootstrap.Toast(toastEl)
  )
  toastList.forEach(toast => toast.show())
}
</script>

Try It Yourself