Bootstrap 5 Toast

In Bootstrap 5, toasts are lightweight, customizable alert messages designed for unobtrusive notifications. They are typically used for short, non-blocking messages, like "Saved successfully" or "New message received."

Basic Toast :

A basic toast includes the following structure:

Example
<div class="toast show" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">Toast Header</strong>
<small class="text-body-secondary">Just now</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"> </button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>

Try it yourself

Toast Triggers:

Toasts can be triggered by buttons or other events.

Example
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">Notification</strong>
<small>Just now</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
This is triggered by a button click!
</div>
</div>

<script>
document.getElementById('showToast').addEventListener('click', function () {
const toastEl = document.querySelector('.toast');
const toast = new bootstrap.Toast(toastEl);
toast.show();
});
</script>

Try it yourself

Automatic and Manual Dismissal:

  • By default, toasts dismiss themselves automatically after a delay (autohide is true).
  • You can configure the delay using the data-bs-delay attribute or in JavaScript.
Notification
This toast will disappear after 3 seconds!
Notification
This toast will disappear after 3 seconds!
Example
<div class="row">
<div class="col-md-6">
<button type="button" class="btn btn-primary" id="autoshowToast">Automatic Dismissal after 3 second</button>

<div id="toast1" class="toast" data-bs-autohide="true" data-bs-delay="3000">
<div class="toast-header">
<strong class="me-auto">Notification</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"> </button>
</div>
<div class="toast-body">
This toast will disappear after 3 seconds!
</div>
</div>
</div>
<div class="col-md-6">
<button type="button" class="btn btn-primary" id="manualshowToast">Automatic Dismissal after 5 second</button>
<div id="toast2" class="toast" data-bs-autohide="true">
<div class="toast-header">
<strong class="me-auto">Notification</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"> </button>
</div>
<div class="toast-body">
This toast will disappear after 3 seconds!
</div>
</div>
</div>
</div>

<script>
document.getElementById('autoshowToast').addEventListener('click', function () {
const toastEl = document.querySelector('#toast1');
const toast = new bootstrap.Toast(toastEl);
toast.show();
});
document.getElementById('manualshowToast').addEventListener('click', function () {
const toastEl = document.querySelector('#toast2');
const toast = new bootstrap.Toast(toastEl, { autohide: true, delay: 5000 });
toast.show();
});
</script>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.