Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
---
2
layout: docs
3
title: Tooltips
4
description: Documentation and examples for adding custom Bootstrap tooltips with CSS and JavaScript using CSS3 for animations and data-attributes for local title storage.
5
group: components
6
toc: true
7
---
8
 
9
## Overview
10
 
11
Things to know when using the tooltip plugin:
12
 
13
- Tooltips rely on the 3rd party library [Popper](https://popper.js.org/) for positioning. You must include [popper.min.js]({{< param "cdn.popper" >}}) before bootstrap.js or use `bootstrap.bundle.min.js` / `bootstrap.bundle.js` which contains Popper in order for tooltips to work!
14
- If you're building our JavaScript from source, it [requires `util.js`]({{< docsref "/getting-started/javascript#util" >}}).
15
- Tooltips are opt-in for performance reasons, so **you must initialize them yourself**.
16
- Tooltips with zero-length titles are never displayed.
17
- Specify `container: 'body'` to avoid rendering problems in more complex components (like our input groups, button groups, etc).
18
- Triggering tooltips on hidden elements will not work.
19
- Tooltips for `.disabled` or `disabled` elements must be triggered on a wrapper element.
20
- When triggered from hyperlinks that span multiple lines, tooltips will be centered. Use `white-space: nowrap;` on your `<a>`s to avoid this behavior.
21
- Tooltips must be hidden before their corresponding elements have been removed from the DOM.
22
- Tooltips can be triggered thanks to an element inside a shadow DOM.
23
 
24
{{< callout info >}}
25
{{< partial "callout-info-sanitizer.md" >}}
26
{{< /callout >}}
27
 
28
{{< callout info >}}
29
{{< partial "callout-info-prefersreducedmotion.md" >}}
30
{{< /callout >}}
31
 
32
Got all that? Great, let's see how they work with some examples.
33
 
34
## Example: Enable tooltips everywhere
35
 
36
One way to initialize all tooltips on a page would be to select them by their `data-toggle` attribute:
37
 
38
```js
39
$(function () {
40
  $('[data-toggle="tooltip"]').tooltip()
41
})
42
```
43
 
44
## Examples
45
 
46
Hover over the links below to see tooltips:
47
 
48
<div class="bd-example tooltip-demo">
49
  <p class="muted">Placeholder text to demonstrate some <a href="#" data-toggle="tooltip" title="Default tooltip">inline links</a> with tooltips. This is now just filler, no killer. Content placed here just to mimic the presence of <a href="#" data-toggle="tooltip" title="Another tooltip">real text</a>. And all that just to give you an idea of how tooltips would look when used in real-world situations. So hopefully you've now seen how <a href="#" data-toggle="tooltip" title="Another one here too">these tooltips on links</a> can work in practice, once you use them on <a href="#" data-toggle="tooltip" title="The last tip!">your own</a> site or project.
50
  </p>
51
</div>
52
 
53
Hover over the buttons below to see the four tooltips directions: top, right, bottom, and left.
54
 
55
<div class="bd-example tooltip-demo">
56
  <div class="bd-example-tooltips">
57
    <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="top" title="Tooltip on top">Tooltip on top</button>
58
    <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="right" title="Tooltip on right">Tooltip on right</button>
59
    <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="bottom" title="Tooltip on bottom">Tooltip on bottom</button>
60
    <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="left" title="Tooltip on left">Tooltip on left</button>
61
    <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-html="true" title="<em>Tooltip</em> <u>with</u> <b>HTML</b>">Tooltip with HTML</button>
62
  </div>
63
</div>
64
 
65
```html
66
<button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="top" title="Tooltip on top">
67
  Tooltip on top
68
</button>
69
<button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="right" title="Tooltip on right">
70
  Tooltip on right
71
</button>
72
<button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="bottom" title="Tooltip on bottom">
73
  Tooltip on bottom
74
</button>
75
<button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="left" title="Tooltip on left">
76
  Tooltip on left
77
</button>
78
```
79
 
80
And with custom HTML added:
81
 
82
```html
83
<button type="button" class="btn btn-secondary" data-toggle="tooltip" data-html="true" title="<em>Tooltip</em> <u>with</u> <b>HTML</b>">
84
  Tooltip with HTML
85
</button>
86
```
87
 
88
## Usage
89
 
90
The tooltip plugin generates content and markup on demand, and by default places tooltips after their trigger element.
91
 
92
Trigger the tooltip via JavaScript:
93
 
94
```js
95
$('#example').tooltip(options)
96
```
97
 
98
{{< callout warning >}}
99
##### Overflow `auto` and `scroll`
100
 
101
Tooltip position attempts to automatically change when a parent container has `overflow: auto` or `overflow: scroll` like our `.table-responsive`, but still keeps the original placement's positioning. To resolve, set the `boundary` option to anything other than default value, `'scrollParent'`, such as `'window'`:
102
 
103
```js
104
$('#example').tooltip({ boundary: 'window' })
105
```
106
{{< /callout >}}
107
 
108
### Markup
109
 
110
The required markup for a tooltip is only a `data` attribute and `title` on the HTML element you wish to have a tooltip. The generated markup of a tooltip is rather simple, though it does require a position (by default, set to `top` by the plugin).
111
 
112
{{< callout warning >}}
113
##### Making tooltips work for keyboard and assistive technology users
114
 
115
You should only add tooltips to HTML elements that are traditionally keyboard-focusable and interactive (such as links or form controls). Although arbitrary HTML elements (such as `<span>`s) can be made focusable by adding the `tabindex="0"` attribute, this will add potentially annoying and confusing tab stops on non-interactive elements for keyboard users, and most assistive technologies currently do not announce the tooltip in this situation. Additionally, do not rely solely on `hover` as the trigger for your tooltip, as this will make your tooltips impossible to trigger for keyboard users.
116
{{< /callout >}}
117
 
118
```html
119
<!-- HTML to write -->
120
<a href="#" data-toggle="tooltip" title="Some tooltip text!">Hover over me</a>
121
 
122
<!-- Generated markup by the plugin -->
123
<div class="tooltip bs-tooltip-top" role="tooltip">
124
  <div class="arrow"></div>
125
  <div class="tooltip-inner">
126
    Some tooltip text!
127
  </div>
128
</div>
129
```
130
 
131
### Disabled elements
132
 
133
Elements with the `disabled` attribute aren't interactive, meaning users cannot focus, hover, or click them to trigger a tooltip (or popover). As a workaround, you'll want to trigger the tooltip from a wrapper `<div>` or `<span>`, ideally made keyboard-focusable using `tabindex="0"`, and override the `pointer-events` on the disabled element.
134
 
135
<div class="tooltip-demo">
136
{{< example >}}
137
<span class="d-inline-block" tabindex="0" data-toggle="tooltip" title="Disabled tooltip">
138
  <button class="btn btn-primary" style="pointer-events: none;" type="button" disabled>Disabled button</button>
139
</span>
140
{{< /example >}}
141
</div>
142
 
143
### Options
144
 
145
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-`, as in `data-animation=""`.
146
 
147
{{< callout warning >}}
148
Note that for security reasons the `sanitize`, `sanitizeFn` and `whiteList` options cannot be supplied using data attributes.
149
{{< /callout >}}
150
 
151
<table class="table table-bordered table-striped">
152
  <thead>
153
    <tr>
154
      <th style="width: 100px;">Name</th>
155
      <th style="width: 100px;">Type</th>
156
      <th style="width: 50px;">Default</th>
157
      <th>Description</th>
158
    </tr>
159
  </thead>
160
  <tbody>
161
    <tr>
162
      <td>animation</td>
163
      <td>boolean</td>
164
      <td>true</td>
165
      <td>Apply a CSS fade transition to the tooltip</td>
166
    </tr>
167
    <tr>
168
      <td>container</td>
169
      <td>string | element | false</td>
170
      <td>false</td>
171
      <td>
172
        <p>Appends the tooltip to a specific element. Example: <code>container: 'body'</code>. This option is particularly useful in that it allows you to position the tooltip in the flow of the document near the triggering element - which will prevent the tooltip from floating away from the triggering element during a window resize.</p>
173
      </td>
174
    </tr>
175
    <tr>
176
      <td>delay</td>
177
      <td>number | object</td>
178
      <td>0</td>
179
      <td>
180
        <p>Delay showing and hiding the tooltip (ms) - does not apply to manual trigger type</p>
181
        <p>If a number is supplied, delay is applied to both hide/show</p>
182
        <p>Object structure is: <code>delay: { "show": 500, "hide": 100 }</code></p>
183
      </td>
184
    </tr>
185
    <tr>
186
      <td>html</td>
187
      <td>boolean</td>
188
      <td>false</td>
189
      <td>
190
        <p>Allow HTML in the tooltip.</p>
191
        <p>If true, HTML tags in the tooltip's <code>title</code> will be rendered in the tooltip. If false, jQuery's <code>text</code> method will be used to insert content into the DOM.</p>
192
        <p>Use text if you're worried about XSS attacks.</p>
193
      </td>
194
    </tr>
195
    <tr>
196
      <td>placement</td>
197
      <td>string | function</td>
198
      <td>'top'</td>
199
      <td>
200
        <p>How to position the tooltip - auto | top | bottom | left | right.<br>When <code>auto</code> is specified, it will dynamically reorient the tooltip.</p>
201
        <p>When a function is used to determine the placement, it is called with the tooltip DOM node as its first argument and the triggering element DOM node as its second. The <code>this</code> context is set to the tooltip instance.</p>
202
      </td>
203
    </tr>
204
    <tr>
205
      <td>selector</td>
206
      <td>string | false</td>
207
      <td>false</td>
208
      <td>If a selector is provided, tooltip objects will be delegated to the specified targets. In practice, this is used to also apply tooltips to dynamically added DOM elements (<code>jQuery.on</code> support). See <a href="{{< param repo >}}/issues/4215">this</a> and <a href="https://codepen.io/team/bootstrap/pen/qBNGbYK">an informative example</a>.</td>
209
    </tr>
210
    <tr>
211
      <td>template</td>
212
      <td>string</td>
213
      <td><code>'&lt;div class="tooltip" role="tooltip"&gt;&lt;div class="arrow"&gt;&lt;/div&gt;&lt;div class="tooltip-inner"&gt;&lt;/div&gt;&lt;/div&gt;'</code></td>
214
      <td>
215
        <p>Base HTML to use when creating the tooltip.</p>
216
        <p>The tooltip's <code>title</code> will be injected into the <code>.tooltip-inner</code>.</p>
217
        <p><code>.arrow</code> will become the tooltip's arrow.</p>
218
        <p>The outermost wrapper element should have the <code>.tooltip</code> class and <code>role="tooltip"</code>.</p>
219
      </td>
220
    </tr>
221
    <tr>
222
      <td>title</td>
223
      <td>string | element | function</td>
224
      <td>''</td>
225
      <td>
226
        <p>Default title value if <code>title</code> attribute isn't present.</p>
227
        <p>If a function is given, it will be called with its <code>this</code> reference set to the element that the tooltip is attached to.</p>
228
      </td>
229
    </tr>
230
    <tr>
231
      <td>trigger</td>
232
      <td>string</td>
233
      <td>'hover focus'</td>
234
      <td>
235
        <p>How tooltip is triggered - click | hover | focus | manual. You may pass multiple triggers; separate them with a space.</p>
236
        <p><code>'manual'</code> indicates that the tooltip will be triggered programmatically via the <code>.tooltip('show')</code>, <code>.tooltip('hide')</code> and <code>.tooltip('toggle')</code> methods; this value cannot be combined with any other trigger.</p>
237
        <p><code>'hover'</code> on its own will result in tooltips that cannot be triggered via the keyboard, and should only be used if alternative methods for conveying the same information for keyboard users is present.</p>
238
      </td>
239
    </tr>
240
    <tr>
241
      <td>offset</td>
242
      <td>number | string | function</td>
243
      <td>0</td>
244
      <td>
245
        <p>Offset of the tooltip relative to its target.</p>
246
        <p>When a function is used to determine the offset, it is called with an object containing the offset data as its first argument. The function must return an object with the same structure. The triggering element DOM node is passed as the second argument.</p>
247
        <p>For more information refer to Popper's <a href="https://popper.js.org/docs/v1/#modifiers..offset.offset">offset docs</a>.</p>
248
      </td>
249
    </tr>
250
    <tr>
251
      <td>fallbackPlacement</td>
252
      <td>string | array</td>
253
      <td>'flip'</td>
254
      <td>Allow to specify which position Popper will use on fallback. For more information refer to
255
      Popper's <a href="https://popper.js.org/docs/v1/#modifiers..flip.behavior">behavior docs</a></td>
256
    </tr>
257
    <tr>
258
      <td>customClass</td>
259
      <td>string | function</td>
260
      <td>''</td>
261
      <td>
262
        <p>Add classes to the tooltip when it is shown. Note that these classes will be added in addition to any classes specified in the template. To add multiple classes, separate them with spaces: <code>'a b'</code>.</p>
263
        <p>You can also pass a function that should return a single string containing additional class names.</p>
264
      </td>
265
    </tr>
266
    <tr>
267
      <td>boundary</td>
268
      <td>string | element</td>
269
      <td>'scrollParent'</td>
270
      <td>Overflow constraint boundary of the tooltip. Accepts the values of <code>'viewport'</code>, <code>'window'</code>, <code>'scrollParent'</code>, or an HTMLElement reference (JavaScript only). For more information refer to Popper's <a href="https://popper.js.org/docs/v1/#modifiers..preventOverflow.boundariesElement">preventOverflow docs</a>.</td>
271
    </tr>
272
    <tr>
273
      <td>sanitize</td>
274
      <td>boolean</td>
275
      <td>true</td>
276
      <td>Enable or disable the sanitization. If activated <code>'template'</code> and <code>'title'</code> options will be sanitized. See the <a href="{{< docsref "/getting-started/javascript#sanitizer" >}}">sanitizer section in our JavaScript documentation</a>.</td>
277
    </tr>
278
    <tr>
279
      <td>whiteList</td>
280
      <td>object</td>
281
      <td><a href="{{< docsref "/getting-started/javascript#sanitizer" >}}">Default value</a></td>
282
      <td>Object which contains allowed attributes and tags</td>
283
    </tr>
284
    <tr>
285
      <td>sanitizeFn</td>
286
      <td>null | function</td>
287
      <td>null</td>
288
      <td>Here you can supply your own sanitize function. This can be useful if you prefer to use a dedicated library to perform sanitization.</td>
289
    </tr>
290
    <tr>
291
      <td>popperConfig</td>
292
      <td>null | object</td>
293
      <td>null</td>
294
      <td>To change Bootstrap's default Popper config, see <a href="https://popper.js.org/docs/v1/#Popper.Defaults">Popper's configuration</a></td>
295
    </tr>
296
  </tbody>
297
</table>
298
 
299
{{< callout info >}}
300
#### Data attributes for individual tooltips
301
 
302
Options for individual tooltips can alternatively be specified through the use of data attributes, as explained above.
303
{{< /callout >}}
304
 
305
### Methods
306
 
307
{{< callout danger >}}
308
{{< partial "callout-danger-async-methods.md" >}}
309
{{< /callout >}}
310
 
311
#### `$().tooltip(options)`
312
 
313
Attaches a tooltip handler to an element collection.
314
 
315
#### `.tooltip('show')`
316
 
317
Reveals an element's tooltip. **Returns to the caller before the tooltip has actually been shown** (i.e. before the `shown.bs.tooltip` event occurs). This is considered a "manual" triggering of the tooltip. Tooltips with zero-length titles are never displayed.
318
 
319
```js
320
$('#element').tooltip('show')
321
```
322
 
323
#### `.tooltip('hide')`
324
 
325
Hides an element's tooltip. **Returns to the caller before the tooltip has actually been hidden** (i.e. before the `hidden.bs.tooltip` event occurs). This is considered a "manual" triggering of the tooltip.
326
 
327
```js
328
$('#element').tooltip('hide')
329
```
330
 
331
#### `.tooltip('toggle')`
332
 
333
Toggles an element's tooltip. **Returns to the caller before the tooltip has actually been shown or hidden** (i.e. before the `shown.bs.tooltip` or `hidden.bs.tooltip` event occurs). This is considered a "manual" triggering of the tooltip.
334
 
335
```js
336
$('#element').tooltip('toggle')
337
```
338
 
339
#### `.tooltip('dispose')`
340
 
341
Hides and destroys an element's tooltip. Tooltips that use delegation (which are created using [the `selector` option](#options)) cannot be individually destroyed on descendant trigger elements.
342
 
343
```js
344
$('#element').tooltip('dispose')
345
```
346
 
347
#### `.tooltip('enable')`
348
 
349
Gives an element's tooltip the ability to be shown. **Tooltips are enabled by default.**
350
 
351
```js
352
$('#element').tooltip('enable')
353
```
354
 
355
#### `.tooltip('disable')`
356
 
357
Removes the ability for an element's tooltip to be shown. The tooltip will only be able to be shown if it is re-enabled.
358
 
359
```js
360
$('#element').tooltip('disable')
361
```
362
 
363
#### `.tooltip('toggleEnabled')`
364
 
365
Toggles the ability for an element's tooltip to be shown or hidden.
366
 
367
```js
368
$('#element').tooltip('toggleEnabled')
369
```
370
 
371
#### `.tooltip('update')`
372
 
373
Updates the position of an element's tooltip.
374
 
375
```js
376
$('#element').tooltip('update')
377
```
378
 
379
### Events
380
 
381
<table class="table table-bordered table-striped">
382
  <thead>
383
    <tr>
384
      <th style="width: 150px;">Event Type</th>
385
      <th>Description</th>
386
    </tr>
387
  </thead>
388
  <tbody>
389
    <tr>
390
      <td>show.bs.tooltip</td>
391
      <td>This event fires immediately when the <code>show</code> instance method is called.</td>
392
    </tr>
393
    <tr>
394
      <td>shown.bs.tooltip</td>
395
      <td>This event is fired when the tooltip has been made visible to the user (will wait for CSS transitions to complete).</td>
396
    </tr>
397
    <tr>
398
      <td>hide.bs.tooltip</td>
399
      <td>This event is fired immediately when the <code>hide</code> instance method has been called.</td>
400
    </tr>
401
    <tr>
402
      <td>hidden.bs.tooltip</td>
403
      <td>This event is fired when the tooltip has finished being hidden from the user (will wait for CSS transitions to complete).</td>
404
    </tr>
405
    <tr>
406
      <td>inserted.bs.tooltip</td>
407
      <td>This event is fired after the <code>show.bs.tooltip</code> event when the tooltip template has been added to the DOM.</td>
408
    </tr>
409
  </tbody>
410
</table>
411
 
412
```js
413
$('#myTooltip').on('hidden.bs.tooltip', function () {
414
  // do something...
415
})
416
```