Alerts
Provide contextual feedback messages for typical user actions with the handful of available and flexible alert messages.
Examples#
Alerts are available for any length of text, as well as an optional dismiss button. For proper styling, use one of the six required contextual classes (e.g., .alert-success
). For inline dismissal, use the cakes alert plugin.
<div class="alert alert-primary" role="alert">A simple Primary alert—check it out!</div>
<div class="alert alert-info" role="alert">A simple Info alert—check it out!</div>
<div class="alert alert-danger" role="alert">A simple Danger alert—check it out!</div>
<div class="alert alert-success" role="alert">A simple Success 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
.sr-only
class.
Link color#
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-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-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-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-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>
Icon aside#
A status icon on the page can highlight the intended use. We use display: flex;
to place the icon aside the content. Wrap your content inside a <div class="alert-content-with-icon">
and add <div class="alert-icon">ADD YOUR ICON</div>
in front of it. Thereby the icon must have a fixed size. The icon wrapper .alert-icon
adjusts to the width of the icon.
<div class="alert d-flex alert-primary" role="alert">
<div class="alert-icon">
<svg class="icon icon-4" xmlns="http://www.w3.org/2000/svg" role="img" focusable="false">
<title>Info</title>
<use xlink:href="../../.././_assets/images/icon__sprite.svg#information-circle">
</use>
</svg>
</div>
<div class="alert-content-with-icon">
<h4 class="alert-heading">Information!</h4>
<p class="mb-0">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>
</div>
</div>
<div class="alert d-flex alert-info" role="alert">
<div class="alert-icon">
<svg class="icon icon-4" xmlns="http://www.w3.org/2000/svg" role="img" focusable="false">
<title>Info</title>
<use xlink:href="../../.././_assets/images/icon__sprite.svg#information-circle">
</use>
</svg>
</div>
<div class="alert-content-with-icon">
<h4 class="alert-heading">Information!</h4>
<p class="mb-0">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>
</div>
</div>
<div class="alert d-flex alert-danger" role="alert">
<div class="alert-icon">
<svg class="icon icon-4" xmlns="http://www.w3.org/2000/svg" role="img" focusable="false">
<title>Danger</title>
<use xlink:href="../../.././_assets/images/icon__sprite.svg#exclamation-triangle">
</use>
</svg>
</div>
<div class="alert-content-with-icon">
<h4 class="alert-heading">Important information!</h4>
<p class="mb-0">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>
</div>
</div>
<div class="alert d-flex alert-success" role="alert">
<div class="alert-icon">
<svg class="icon icon-4" xmlns="http://www.w3.org/2000/svg" role="img" focusable="false">
<title>Success</title>
<use xlink:href="../../.././_assets/images/icon__sprite.svg#hook-circle">
</use>
</svg>
</div>
<div class="alert-content-with-icon">
<h4 class="alert-heading">Well done!</h4>
<p class="mb-0">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>
</div>
</div>
<div class="alert d-flex alert-gray" role="alert">
<div class="alert-icon">
<svg class="icon icon-4" xmlns="http://www.w3.org/2000/svg" role="img" focusable="false">
<title>Placeholder</title>
<use xlink:href="../../.././_assets/images/icon__sprite.svg#placeholder">
</use>
</svg>
</div>
<div class="alert-content-with-icon">
<h4 class="alert-heading">Insignificant information!</h4>
<p class="mb-0">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>
</div>
</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#
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 CAKE JavaScript.
- Add a dismiss button and the
.alert-dismissible
class, which positions the.close
button. - On the dismiss button, add the
data-dismiss="alert"
attribute, which triggers the JavaScript functionality. Be sure to use the<button>
element with it and place it as the first child of the alert 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-danger alert-dismissible fade show" role="alert">
<button type="button" class="close" data-controller="alert/close" aria-label="Close">
<svg class="icon icon-close" aria-hidden="true" xmlns="http://www.w3.org/2000/svg"
role="img" focusable="false">
<title>Close</title>
<use xlink:href="../../.././_assets/images/icon__sprite.svg#cross">
</use>
</svg>
</button>
<strong>Holy guacamole!</strong> You should check in on some of those fields below.</div>
Dismissing with icon aside#
<div class="alert alert-success d-flex alert-dismissible fade show" role="alert">
<div class="alert-icon">
<svg class="icon icon-4" xmlns="http://www.w3.org/2000/svg" role="img" focusable="false">
<title>Success</title>
<use xlink:href="../../.././_assets/images/icon__sprite.svg#hook-circle">
</use>
</svg>
</div>
<div class="alert-content-with-icon">
<button type="button" class="close" data-controller="alert/close" aria-label="Close">
<svg class="icon icon-close" aria-hidden="true" xmlns="http://www.w3.org/2000/svg"
role="img" focusable="false">
<title>Close</title>
<use xlink:href="../../.././_assets/images/icon__sprite.svg#cross">
</use>
</svg>
</button>
<h4 class="alert-heading">Well done!</h4>
<p class="mb-0">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>
</div>
</div>
JavaScript#
The alert does work without javascript. Only the dismissable alerts require our javascript to provide the closing functionality.
You can import our javascript bundle cake.js
to automatically use this feature. You only must ensure that the property data-controller="alert/close"
is set on the closing-button element of the alert.
Initialization#
To initialize the javascript with default configuration you could simply run the following code:
document.addEventListener ('DOMContentLoaded', () => {
window.cake.alert ();
});
Customization#
To customize the default behavior you can only embedd the alert.js
file into your mockups. Then you can initialize the functionality by calling:
document.addEventListener ('DOMContentLoaded', () => {
window.cake.alert (options = {
elements: [],
querySelector: 'button[data-controller=alert]'
});
});
elements [Array]
- provide the specific closing-button element of the alert (optional).
querySelector [String]
- provide a query-selector to select all closing-button elements of the alert. (optional, default:
button[data-controller="alert/close"]
)
- provide a query-selector to select all closing-button elements of the alert. (optional, default:
If you do provide the options.elements
the options.querySelector
option gets ignored. If you do not provide any options.elements
always the options.querySelector
is used!
Triggers#
Enable dismissal of an alert via data
attributes on a button within the alert, as demonstrated above:
<button type="button" class="close" data-controller="alert/close" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
Note that closing an alert will remove it from the DOM.
Methods#
Method | Description |
---|---|
element.closeAlert () | Closes 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. |
document.getElementById ("#alert-selector").closeAlert ()
Events#
CAKE's alert plugin exposes a few custom events for hooking into alert functionality.
Event | Description |
---|---|
alertClose | This event fires immediately when the closeAlert () instance method is called. |
alertClosed | This event is fired when the alert has been closed (will wait for CSS transitions to complete). |
document.getElementById ("#alert-selector").addEventListener('alertClosed', () => {
// do something...
})