1 |
efrain |
1 |
---
|
|
|
2 |
layout: docs
|
|
|
3 |
title: Alerts
|
|
|
4 |
description: Provide contextual feedback messages for typical user actions with the handful of available and flexible alert messages.
|
|
|
5 |
group: components
|
|
|
6 |
aliases:
|
|
|
7 |
- "/components/"
|
|
|
8 |
- "/docs/4.6/components/"
|
|
|
9 |
toc: true
|
|
|
10 |
---
|
|
|
11 |
|
|
|
12 |
## Examples
|
|
|
13 |
|
|
|
14 |
Alerts are available for any length of text, as well as an optional dismiss button. For proper styling, use one of the eight **required** contextual classes (e.g., `.alert-success`). For inline dismissal, use the [alerts jQuery plugin](#dismissing).
|
|
|
15 |
|
|
|
16 |
{{< example >}}
|
|
|
17 |
{{< alerts.inline >}}
|
|
|
18 |
{{- range (index $.Site.Data "theme-colors") }}
|
|
|
19 |
<div class="alert alert-{{ .name }}" role="alert">
|
|
|
20 |
A simple {{ .name }} alert—check it out!
|
|
|
21 |
</div>{{- end -}}
|
|
|
22 |
{{< /alerts.inline >}}
|
|
|
23 |
{{< /example >}}
|
|
|
24 |
|
|
|
25 |
{{< callout warning >}}
|
|
|
26 |
{{< partial "callout-warning-color-assistive-technologies.md" >}}
|
|
|
27 |
{{< /callout >}}
|
|
|
28 |
|
|
|
29 |
### Link color
|
|
|
30 |
|
|
|
31 |
Use the `.alert-link` utility class to quickly provide matching colored links within any alert.
|
|
|
32 |
|
|
|
33 |
{{< example >}}
|
|
|
34 |
{{< alerts.inline >}}
|
|
|
35 |
{{- range (index $.Site.Data "theme-colors") }}
|
|
|
36 |
<div class="alert alert-{{ .name }}" role="alert">
|
|
|
37 |
A simple {{ .name }} alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
|
|
|
38 |
</div>{{ end -}}
|
|
|
39 |
{{< /alerts.inline >}}
|
|
|
40 |
{{< /example >}}
|
|
|
41 |
|
|
|
42 |
### Additional content
|
|
|
43 |
|
|
|
44 |
Alerts can also contain additional HTML elements like headings, paragraphs and dividers.
|
|
|
45 |
|
|
|
46 |
{{< example >}}
|
|
|
47 |
<div class="alert alert-success" role="alert">
|
|
|
48 |
<h4 class="alert-heading">Well done!</h4>
|
|
|
49 |
<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>
|
|
|
50 |
<hr>
|
|
|
51 |
<p class="mb-0">Whenever you need to, be sure to use margin utilities to keep things nice and tidy.</p>
|
|
|
52 |
</div>
|
|
|
53 |
{{< /example >}}
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
### Dismissing
|
|
|
57 |
|
|
|
58 |
Using the alert JavaScript plugin, it's possible to dismiss any alert inline. Here's how:
|
|
|
59 |
|
|
|
60 |
- Be sure you've loaded the alert plugin, or the compiled Bootstrap JavaScript.
|
|
|
61 |
- If you're building our JavaScript from source, it [requires `util.js`]({{< docsref "/getting-started/javascript#util" >}}). The compiled version includes this.
|
|
|
62 |
- Add a dismiss button and the `.alert-dismissible` class, which adds extra padding to the right of the alert and positions the `.close` button.
|
|
|
63 |
- On the dismiss button, add the `data-dismiss="alert"` attribute, which triggers the JavaScript functionality. Be sure to use the `<button>` element with it for proper behavior across all devices.
|
|
|
64 |
- To animate alerts when dismissing them, be sure to add the `.fade` and `.show` classes.
|
|
|
65 |
|
|
|
66 |
You can see this in action with a live demo:
|
|
|
67 |
|
|
|
68 |
{{< example >}}
|
|
|
69 |
<div class="alert alert-warning alert-dismissible fade show" role="alert">
|
|
|
70 |
<strong>Holy guacamole!</strong> You should check in on some of those fields below.
|
|
|
71 |
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
|
|
|
72 |
<span aria-hidden="true">×</span>
|
|
|
73 |
</button>
|
|
|
74 |
</div>
|
|
|
75 |
{{< /example >}}
|
|
|
76 |
|
|
|
77 |
## JavaScript behavior
|
|
|
78 |
|
|
|
79 |
### Triggers
|
|
|
80 |
|
|
|
81 |
Enable dismissal of an alert via JavaScript:
|
|
|
82 |
|
|
|
83 |
```js
|
|
|
84 |
$('.alert').alert()
|
|
|
85 |
```
|
|
|
86 |
|
|
|
87 |
Or with `data` attributes on a button **within the alert**, as demonstrated above:
|
|
|
88 |
|
|
|
89 |
```html
|
|
|
90 |
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
|
|
|
91 |
<span aria-hidden="true">×</span>
|
|
|
92 |
</button>
|
|
|
93 |
```
|
|
|
94 |
|
|
|
95 |
Note that closing an alert will remove it from the DOM.
|
|
|
96 |
|
|
|
97 |
### Methods
|
|
|
98 |
|
|
|
99 |
| Method | Description |
|
|
|
100 |
| --- | --- |
|
|
|
101 |
| `$().alert()` | Makes an alert listen for click events on descendant elements which have the `data-dismiss="alert"` attribute. (Not necessary when using the data-api's auto-initialization.) |
|
|
|
102 |
| `$().alert('close')` | 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. |
|
|
|
103 |
| `$().alert('dispose')` | Destroys an element's alert. |
|
|
|
104 |
|
|
|
105 |
```js
|
|
|
106 |
$('.alert').alert('close')
|
|
|
107 |
```
|
|
|
108 |
|
|
|
109 |
### Events
|
|
|
110 |
|
|
|
111 |
Bootstrap's alert plugin exposes a few events for hooking into alert functionality.
|
|
|
112 |
|
|
|
113 |
| Event | Description |
|
|
|
114 |
| --- | --- |
|
|
|
115 |
| `close.bs.alert` | This event fires immediately when the `close` instance method is called. |
|
|
|
116 |
| `closed.bs.alert` | This event is fired when the alert has been closed (will wait for CSS transitions to complete). |
|
|
|
117 |
|
|
|
118 |
```js
|
|
|
119 |
$('#myAlert').on('closed.bs.alert', function () {
|
|
|
120 |
// do something...
|
|
|
121 |
})
|
|
|
122 |
```
|