bootstrap/site/docs/4.1/components/toasts.md

5.5 KiB

layout title description group toc
docs Toasts Push notifications to your visitors with a toast, a lightweight and easily customizable alert message. components true

Toasts are lightweight notifications designed to mimic the push notifications that have been popularized by mobile and desktop operating systems. They're built with flexbox, so they're easy to align and position.

Examples

A basic toast can include a header (though it doesn't strictly need one) with whatever contents you like. The header is also display: flex, so .mr-auto and .ml-auto can be used for easy pushing of content, as well as all our flexbox utilities.

{% capture example %}
Bootstrap 11 mins ago
Hello, world! This is a toast message.
{% endcapture %} {% include example.html content=example %}

They're slightly translucent, too, so they blend over whatever they might appear over. For browsers that support backdrop-filter, we'll also attempt to blur the elements under a toast.

{% capture example %}
Bootstrap 11 mins ago
Hello, world! This is a toast message.
{% endcapture %} {% include example.html content=example %}

Plus, they'll easily stack.

{% capture example %}
Bootstrap just now
See? Just like this.
Bootstrap 2 seconds ago
Heads up, toasts will stack automatically
{% endcapture %} {% include example.html content=example %}

Accessibility

Toasts are intended to be small interruptions to your visitors or users, so to help those on screen readers, you should wrap your toasts in an aria-live region. This allows screen readers the ability to see suggested interruptions without any visual cues.

{% highlight html %}

...
{% endhighlight %}

Placement

Place toasts with custom CSS as you need them. The top right is often used for notifications, as is the top middle. If you're only ever going to show one toast at a time, put the positioning styles right on the .toast.

{% capture example %}
Bootstrap 11 mins ago
Hello, world! This is a toast message.
{% endcapture %} {% include example.html content=example %}

For systems that generate more notifications, consider using a wrapping element so they can easily stack.

{% capture example %}
<!-- Then put toasts within -->
<div class="toast">
  <div class="toast-header">
    <img class="rounded mr-2" data-src="holder.js/20x20?size=1&text=.&bg=#007aff" alt="">
    <strong class="mr-auto">Bootstrap</strong>
    <small class="text-muted">just now</small>
  </div>
  <div class="toast-body">
    See? Just like this.
  </div>
</div>

<div class="toast">
  <div class="toast-header">
    <img class="rounded mr-2" data-src="holder.js/20x20?size=1&text=.&bg=#007aff" alt="">
    <strong class="mr-auto">Bootstrap</strong>
    <small class="text-muted">2 seconds ago</small>
  </div>
  <div class="toast-body">
    Heads up, toasts will stack automatically
  </div>
</div>
{% endcapture %} {% include example.html content=example %}

You can also get fancy with flexbox utilities.

{% capture example html %}
<!-- Then put toasts within -->
<div class="toast">
  <div class="toast-header">
    <img class="rounded mr-2" data-src="holder.js/20x20?size=1&text=.&bg=#007aff" alt="">
    <strong class="mr-auto">Bootstrap</strong>
    <small>11 mins ago</small>
  </div>
  <div class="toast-body">
    Hello, world! This is a toast message.
  </div>
</div>
{% endcapture %} {% include example.html content=example %}