Toasts
Bootstrap 5 Toast component
Bootstrap Toast component is a non-disruptive message in the corner of the interface. It provides quick "at-a-glance" feedback on the outcome of an action.
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.
Static example
Colors
Placement
You can set position of every notification using
data-mdb-position
attribute or update()
method.
Select horizontal / vertical alignment
Current position: top-right
Offset
You can also change offset of every notification using
data-mdb-offset
attribute or update()
method.
Container
You can display notification anywhere. Just put your toast in your target element and fill
data-mdb-container
attribute with id or class of parent or
update()
method.
Stacking
You can turn on/off stacking your notifications using
data-mdb-stacking
attribute or update()
method.
Stacking (container)
You can turn on/off stacking your notifications using
data-mdb-stacking
attribute or update()
method.
If you want to support our friends from Tailwind Elements you can also check out the Tailwind toast documentation.
Toasts - API
Usage
Via data attributes
<div
class="toast"
id="example-toast"
role="alert"
aria-live="assertive"
aria-atomic="true"
data-mdb-autohide="false"
data-mdb-position="..."
data-mdb-container="..."
data-mdb-offset="..."
>
<div class="toast-header">
<svg
class="bd-placeholder-img rounded me-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="me-auto">MDBootstrap</strong>
<small>11 mins ago</small>
<button
type="button"
class="ms-2 mb-1 close"
data-mdb-dismiss="toast"
aria-label="Close"
>
<span aria-hidden="true">×</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()
|
getInstance |
element | Static method which allows you to get the toast instance associated to a DOM element. | Toast.getInstance(toastEl) |
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.mdb.toast
|
Emitted when a toast has been toggled |
shown.mdb.toast
|
Emitted once toast is shown |
hide.mdb.toast
|
Emitted when a toast has been toggled |
hidden.mdb.toast
|
Emitted once toast is hidden |
const instance = mdb.Toast.getInstance(document.getElementById('show-example'));
instance.addEventListener('hidden.mdb.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';