Bootstrap Alerts

Notification component that shows alert message box on user action. Includes animated dismiss transition with icon and close button.

What is Bootstrap Alert

Component used to shows a notification message box popup when certain user action has been made. Bootstrap Alert uses Bootstrap color system, optionally with icons to provide different contextual feedback, like success, warning, or danger. Notification message can be any length. Torus Kit allows you to animate alert dismissing with smooth transition.

Bootstrap Alert Example

Alert box notification example, along with Torus Kit alert with icons and smooth animated dismissing transition on close click. Close button can be created using Torus Kit Shapes component and positioned with Custom position utility.

Overview

Alerts are available for any length of text, as well as an optional close button. For proper styling, use one of the eight required contextual classes (e.g., .alert-success). For inline dismissal, use the alerts JavaScript plugin.

<div class="alert alert-primary" role="alert">
A simple primary alert—check it out!
</div>
<div class="alert alert-secondary" role="alert">
A simple secondary alert—check it out!
</div>
<div class="alert alert-success" role="alert">
A simple success alert—check it out!
</div>
<div class="alert alert-info" role="alert">
A simple info alert—check it out!
</div>
<div class="alert alert-warning" role="alert">
A simple warning alert—check it out!
</div>
<div class="alert alert-danger" role="alert">
A simple danger alert—check it out!
</div>
<div class="alert alert-light" role="alert">
A simple light alert—check it out!
</div>
<div class="alert alert-dark" role="alert">
A simple dark alert—check it out!
</div>
<div class="alert alert-gray" role="alert">
A simple gray alert—check it out!
</div>
Conveying meaning to assistive technologies

Using color to add meaning only provides a visual indication, which will not be conveyed to users of assistive technologies – such as screen readers. Ensure that information denoted by the color is either obvious from the content itself (e.g. the visible text), or is included through alternative means, such as additional text hidden with the .visually-hidden class.

Alert styles

Custom alert color

You can create whole new alert styles using Torus Kit Background color palette, or with data-tor="bg(<color>)" attribute, together with other styling utilities. Here are couple of custom alert styles examples done with Torus Kit.

<div class="alert bg-maroon text-white" role="alert">
Alert with extended background color palette
</div>

<div class="alert bg-gray bg-opacity-20 shadow-risen center-v" role="alert">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-geo-alt me-2" viewBox="0 0 16 16">
<path d="M12.166 8.94c-.524 1.062-1.234 2.12-1.96 3.07A31.493 31.493 0 0 1 8 14.58a31.481 31.481 0 0 1-2.206-2.57c-.726-.95-1.436-2.008-1.96-3.07C3.304 7.867 3 6.862 3 6a5 5 0 0 1 10 0c0 .862-.305 1.867-.834 2.94zM8 16s6-5.686 6-10A6 6 0 0 0 2 6c0 4.314 6 10 6 10z"/>
<path d="M8 8a2 2 0 1 1 0-4 2 2 0 0 1 0 4zm0 1a3 3 0 1 0 0-6 3 3 0 0 0 0 6z"/>
</svg>
Alert with lighten background color, icon and risen shadow
</div>

<div class="alert alert-dismissible animate fade show bg-primary bg-darken-sm shadow-lg shadow-primary text-white rounded-pill" role="alert">
Pill border alert with color shadow glow
<div class="circle p-3 me-2 bg-white text-dark shadow-sm cursor-pointer position-absolute" data-tor="place.right place.middle" data-bs-dismiss="alert" aria-label="Close">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-x" viewBox="0 0 16 16">
<path d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z"/>
</svg>
</div>
</div>

<div class="alert text-white" role="alert" data-tor="bg(#269389)">
Alert with your own custom color
</div>

Alert interactions

<div class="alert bg-primary text-white" role="alert" data-tor="hover:bg-darken(md)">
Alert with darken background color on hover
</div>

<div class="alert bg-gray bg-opacity-20 shadow-risen" role="alert" data-tor="hover:[push.up(xs) shadow-offset.down(xs)]">
Alert with translate and sticky shadow on hover
</div>

<div class="alert alert-primary alert-dismissible fade show shadow-primary" role="alert" data-tor-parent="hover" data-tor="hover:shadow(sm)">
Alert with revealing close button and colored shadow on hover
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close" data-tor="hover(p):[fade.in rotate.from(180deg)]"></button>
</div>

Use the .alert-link utility class to quickly provide matching colored links within any alert.

<div class="alert alert-primary" role="alert">
A simple primary alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-secondary" role="alert">
A simple secondary alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-success" role="alert">
A simple success alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-info" role="alert">
A simple info alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-warning" role="alert">
A simple warning alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-danger" role="alert">
A simple danger alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-light" role="alert">
A simple light alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-dark" role="alert">
A simple dark alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-gray" role="alert">
A simple gray alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>

Additional content

Alerts can also contain additional HTML elements like headings, paragraphs and dividers.

<div class="alert alert-success" role="alert">
<h4 class="alert-heading">Well done!</h4>
<p>Aww yeah, you successfully read this important alert message. This example text is going to run a bit longer so that you can see how spacing within an alert works with this kind of content.</p>
<hr>
<p class="mb-0">Whenever you need to, be sure to use margin utilities to keep things nice and tidy.</p>
</div>

Dismissing

Standard dismissing

Using the alert JavaScript plugin, it’s possible to dismiss any alert inline. Here’s how:

  • Be sure you’ve loaded the alert plugin, or the compiled Bootstrap JavaScript.
  • Add a close button and the .alert-dismissible class, which adds extra padding to the right of the alert and positions the close button.
  • On the close button, add the data-bs-dismiss="alert" attribute, which triggers the JavaScript functionality. Be sure to use the <button> element with it for proper behavior across all devices.
  • To animate alerts when dismissing them, be sure to add the .fade and .show classes.

You can see this in action with a live demo:

<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>Holy guacamole!</strong> You should check in on some of those fields below.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

When an alert is dismissed, the element is completely removed from the page structure. If a keyboard user dismisses the alert using the close button, their focus will suddenly be lost and, depending on the browser, reset to the start of the page/document. For this reason, we recommend including additional JavaScript that listens for the closed.bs.alert event and programmatically sets focus() to the most appropriate location in the page. If you're planning to move focus to a non-interactive element that normally does not receive focus, make sure to add tabindex="-1" to the element.

Custom alert hide animation

You can easily change the Bootstrap alert hide animation using TorusKit Effects. Be sure to include .fade class, otherwise the alert will close immediately without any animations.

Because the .alert uses .show class do display itself, you’ll need to use show: trigger and set the starting state of the effect. It means, if you want to create “shrink” dismissing animation for example, you need to use show:scale.from(0) that scales the element from 0 immediately when element gets .show class, and vice-versa, it scales it down to 0 when it lose the .show class

<div class="alert alert-primary alert-dismissible fade show" role="alert" data-tor="show:scale.from(0)">
<strong>Holy guacamole!</strong> You should check in on some of those fields below.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

<div class="alert alert-danger alert-dismissible fade show" role="alert" data-tor="show:[rotateX.from(90deg) @--tor-translateZ(-5rem; 0rem) pull.down(full)] slow">
<strong>Holy guacamole!</strong> You should check in on some of those fields below.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

Alert with icon

To properly align icons with alert text, use .center-v helper class that change the display property to flex with align-items: center;. In some cases you need to wrap the centered content into separate wrapper with .center-v class. We’ve used Bootstrap icons here.

<div class="alert alert-secondary center-v" role="alert">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-chat-left-dots-fill me-2" viewBox="0 0 16 16">
<path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H4.414a1 1 0 0 0-.707.293L.854 15.146A.5.5 0 0 1 0 14.793V2zm5 4a1 1 0 1 0-2 0 1 1 0 0 0 2 0zm4 0a1 1 0 1 0-2 0 1 1 0 0 0 2 0zm3 1a1 1 0 1 0 0-2 1 1 0 0 0 0 2z"/>
</svg>
Torus Kit custom shrink effect on Bootstrap alert
</div>

<div class="alert alert-danger alert-dismissible" role="alert">
<div class="center-v">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-exclamation-triangle-fill me-2" viewBox="0 0 16 16">
<path d="M8.982 1.566a1.13 1.13 0 0 0-1.96 0L.165 13.233c-.457.778.091 1.767.98 1.767h13.713c.889 0 1.438-.99.98-1.767L8.982 1.566zM8 5c.535 0 .954.462.9.995l-.35 3.507a.552.552 0 0 1-1.1 0L7.1 5.995A.905.905 0 0 1 8 5zm.002 6a1 1 0 1 1 0 2 1 1 0 0 1 0-2z"/>
</svg>
Torus Kit custom hide effect on Bootstrap alert
</div>
<hr>
<p class="mb-0 small opacity-70">Make sure you have entered email and password correctly.</p>
</div>

JavaScript behavior

Triggers

Enable dismissal of an alert via JavaScript:

var alertList = document.querySelectorAll('.alert')
alertList.forEach(function (alert) {
  new bootstrap.Alert(alert)
})

Or with data attributes on a button within the alert, as demonstrated above:

<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>

Note that closing an alert will remove it from the DOM.

Methods

You can create an alert instance with the alert constructor, for example:

var myAlert = document.getElementById('myAlert')
var bsAlert = new bootstrap.Alert(myAlert)

This makes an alert listen for click events on descendant elements which have the data-bs-dismiss="alert" attribute. (Not necessary when using the data-api’s auto-initialization.)

MethodDescription
closeCloses an alert by removing it from the DOM. If the .fade and .show classes are present on the element, the alert will fade out before it is removed.
disposeDestroys an element's alert. (Removes stored data on the DOM element)
getInstanceStatic method which allows you to get the alert instance associated to a DOM element, you can use it like this: bootstrap.Alert.getInstance(alert)
var alertNode = document.querySelector('.alert')
var alert = bootstrap.Alert.getInstance(alertNode)
alert.close()

Events

Bootstrap’s alert plugin exposes a few events for hooking into alert functionality.

EventDescription
close.bs.alertFires immediately when the close instance method is called.
closed.bs.alertFired when the alert has been closed and CSS transitions have completed.
var myAlert = document.getElementById('myAlert')
myAlert.addEventListener('closed.bs.alert', function () {
  // do something, for instance, explicitly move focus to the most appropriate element,
  // so it doesn't get lost/reset to the start of the page
  // document.getElementById('...').focus()
})

Bootstrap Alerts FAQ

List of FAQs and how to guides for Alerts.

How to use Bootstrap Alert

Use .alert component if you need to provide contextual feedback messages for typical user actions. Distinguish between them with proper styling, by using one of the eight required contextual classes (e.g., .alert-success)

How to show alert on button click

Create a trigger <button> that will show alert on click. We’re creating .btn .btn-primary button with #alert-btn id:

<button id="alert-btn" class="btn btn-primary">Show me alert</button>

Than you need to add a script that will create an .alert element in DOM. Place this script into <script> tag. We’re using .alert-primary in this example.

// Trigger button
let button = document.querySelector("#alert-btn");

// Alert container
let container = document.querySelector("#alert-container");

// Create click event listener on trigger button
button.addEventListener("click", () => {

  // When user click on trigger #button, insert .alert element into #container
  container.innerHTML =
    `<div class="alert alert-primary alert-dismissible fade" role="alert">
      Primary alert with custom close button
      <button type="button" class="btn-close" data-dismiss="alert" aria-label="Close"></button>
      </div>`
  ;
});

To add a smooth shrinking animation check the Animated dismissing section.

How to auto close alert

Using simple setTimeout JS function, it’s possible to close an .alert after specific time. This example will close the #alert alert after 2000ms.

<!-- Alert element -->
<div id="alert" class="alert alert-primary" role="alert"></div>
// Get alert element
const alert = document.querySelector("#alert");
// Create Bootstrap alert instance
const bsAlert = new bootstrap.Alert(alert);

// Dismiss time out
setTimeout(() => {
  bsAlert.close();
}, 2000);

How to change alert background colors

Add .alert-* contextual class from Torus Kit’s enhanced Bootstrap color scheme to the .alert element. With Torus Kit Pro you can change the alert box background color to any using .bg-<color> class. It’s even possible to change the alert border color using .border-<color> class.