Toast.JS Logo

A Simple Toast Notification Library

Currently v1.1.0

What is a "toast"?

Toast is not only a delicious breakfast food but also a simple, time limited notification. A toast message is a quick and easy way to alert a user that some action has occurred and after a certain amount of time has passed, the toast message will disappear.

Toast toast.js

Toasts are a simple, time dependent notification.

Multiple open toasts not supported

Be sure not to open a toast while another is still visible. Showing more than one toast at a time requires custom code.

Examples

Static example

A rendered toast with body.

Live example

Toggle a toast via JavaScript by clicking the button below. It will slide up and fade in from the bottom of the page.

Optional styles

Multiple ways to style supported

The stylings for toasts can be applied in two different ways: through .toast-* or with toast options.

Default stylings

By default, the stylings for .toast-default will be applied to any unstyled toast.

Toasts have three total styles available: .toast-default, .toast-round, and .toast-square.

Optional sizes

Toasts have two optional sizes, available via modifier classes to be placed on a .toast-dialog.

Remove animation

For toasts that simply appear rather than fade in to view, remove the .fade class from your toast markup.

Usage

The toast plugin toggles your hidden content on demand, via data attributes or JavaScript. It also adds a .toast-backdrop to provide a click area for dismissing shown toasts when clicking outside the toast.

Via data attributes

Activate a toast without writing JavaScript. Set data-toggle="toast" on a controller element, like a button, along with a data-target="#foo" or href="#foo" to target a specific toast to toggle.

Via JavaScript

Call a toast with id myToast with a single line of JavaScript:

Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-backdrop="".

Name type default description
backdrop boolean or the string 'static' true Includes a toast-backdrop element. Alternatively, specify static for a backdrop which doesn't close the toast on click.
keyboard boolean true Closes the toast when escape key is pressed.
show boolean true Shows the toast when initialized.
style string 'default' Styles the toast. Available styles: .toast-default, .toast-round, or .toast-square
timeout number 3000 The amount of time to display the toast for before it dismisses itself.

Timeout option

By adjusting the value for data-timeout, you can control how long the toast will stay visible for.

Methods

.toast(options)

Activates your content as a toast. Accepts an optional options object.

.toast('toggle')

Manually toggles a toast. Returns to the caller before the toast has actually been shown or hidden (i.e. before the shown.bs.toast or hidden.bs.toast event occurs).

.toast('show')

Manually opens a toast. Returns to the caller before the toast has actually been shown (i.e. before the shown.bs.toast event occurs).

.toast('hide')

Manually hides a toast. Returns to the caller before the toast has actually been hidden (i.e. before the hidden.bs.toast event occurs).

Events

Bootstrap's toast class exposes a few events for hooking into toast functionality.

All toast events are fired at the toast itself (i.e. at the <div class="toast">).

Event type description
show.bs.toast This event fires immediately when the show instance method is called. If caused by a click, the clicked element is available as the relatedTarget property of the event.
shown.bs.toast This event is fired when the toast has been made visible to the user (will wait for CSS transitions to complete). If caused by a click, the clicked element is available as the relatedTarget property of the event.
hide.bs.toast This event is fired immediately when the hide instance method has been called.
hidden.bs.toast This event is fired when the toast has finished being hidden from the user (will wait for CSS transitions to complete).