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
*
*
UMD autoinits are enabled
by default. This means that you don't need to initialize
the component manually. However if you are using MDBootstrap ES format then you should pass
the required components to the initMDB
method.
Basic example
Click the buttons to launch the toasts.
Static example
Colors
Since 4.2.0 release our toasts have their own color palette, so we don't use
.bg-*
and .text-*
color classes. For a crisp edge, we removed the
default border with .border-0
.
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.
Toasts - API
Import
Importing components depends on how your application works. If you intend to use the MDBootstrap ES
format, you must
first import the component and then initialize it with the initMDB
method. If you are going to use the UMD
format,
just import the mdb-ui-kit
package.
Usage
Via data attributes
Using the Toasts component doesn't require any additional JavaScript code - simply add
data-mdb-toast-init
attribute to
.toast
and use other data attributes to set all options.
For ES
format, you must first import and call the initMDB
method.
Dismiss
Dismissal can be achieved with data attributes on a button within the toast as demonstrated below:
or on a button outside the toast using the data-mdb-target
as
demonstrated below:
Via JavaScript
Via jQuery
Note: By default, MDB does not include jQuery and you have to add it to the project on your own.
Options
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to
data-mdb-
, as in data-mdb-offset="10"
.
Name | Type | Default | Description |
---|---|---|---|
width
|
String / null | null |
Defines the width of a toast |
position
|
String | null |
Sets the position of the toast. The option includes a horizontal component:
top or bottom , and a vertical component: left ,
center , or right , with the horizontal part specified first
and connected with a dash. For example, top-left
|
color
|
String | null | null |
Sets background of a toast (basic MDB colors) |
offset |
Number | 10 |
Changes offset of toast |
container
|
String | null | '' |
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 |
Number | 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 | Description | Example |
---|---|---|
show |
Shows toast | myToast.show() |
hide |
Hides toast | myToast.hide() |
dispose
|
Removes a mdb.Toast instance |
myToast.dispose()
|
getInstance |
Static method which allows you to get the toast instance associated to a DOM element. | Toast.getInstance(toastEl) |
getOrCreateInstance |
Static method which returns the toast instance associated to a DOM element or create a new one in case it wasn't initialized. |
Toast.getOrCreateInstance(toastEl)
|
update |
Updates options of mdb.Toast instance |
myToast.update({position: 'top-center'})
|
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 |
CSS variables
As part of MDB’s evolving CSS variables approach, toast now use local CSS variables on
.toast
for enhanced real-time customization. Values for the CSS variables are set
via Sass, so Sass customization is still supported, too.