Stripped personal data from development repository
Samo Penic
2019-02-20 83c3f647c35477564b77cbc5b36d37d793d5442a
commit | author | age
83c3f6 1 ---
SP 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.js](https://popper.js.org/) for positioning. You must include [popper.min.js]({{ site.cdn.popper }}) before bootstrap.js or use `bootstrap.bundle.min.js` / `bootstrap.bundle.js` which contains Popper.js in order for tooltips to work!
14 - If you're building our JavaScript from source, it [requires `util.js`]({{ site.baseurl }}/docs/{{ site.docs_version }}/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 {% include callout-info-prefersreducedmotion.md %}
25
26 Got all that? Great, let's see how they work with some examples.
27
28 ## Example: Enable tooltips everywhere
29
30 One way to initialize all tooltips on a page would be to select them by their `data-toggle` attribute:
31
32 {% highlight js %}
33 $(function () {
34   $('[data-toggle="tooltip"]').tooltip()
35 })
36 {% endhighlight %}
37
38 ## Examples
39
40 Hover over the links below to see tooltips:
41
42 <div class="bd-example tooltip-demo">
43   <p class="muted">Tight pants next level keffiyeh <a href="#" data-toggle="tooltip" title="Default tooltip">you probably</a> haven't heard of them. Photo booth beard raw denim letterpress vegan messenger bag stumptown. Farm-to-table seitan, mcsweeney's fixie sustainable quinoa 8-bit american apparel <a href="#" data-toggle="tooltip" title="Another tooltip">have a</a> terry richardson vinyl chambray. Beard stumptown, cardigans banh mi lomo thundercats. Tofu biodiesel williamsburg marfa, four loko mcsweeney's cleanse vegan chambray. A really ironic artisan <a href="#" data-toggle="tooltip" title="Another one here too">whatever keytar</a>, scenester farm-to-table banksy Austin <a href="#" data-toggle="tooltip" title="The last tip!">twitter handle</a> freegan cred raw denim single-origin coffee viral.
44   </p>
45 </div>
46
47 Hover over the buttons below to see the four tooltips directions: top, right, bottom, and left.
48
49 <div class="bd-example tooltip-demo">
50   <div class="bd-example-tooltips">
51     <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="top" title="Tooltip on top">Tooltip on top</button>
52     <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="right" title="Tooltip on right">Tooltip on right</button>
53     <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="bottom" title="Tooltip on bottom">Tooltip on bottom</button>
54     <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="left" title="Tooltip on left">Tooltip on left</button>
55     <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>
56   </div>
57 </div>
58
59 {% highlight html %}
60 <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="top" title="Tooltip on top">
61   Tooltip on top
62 </button>
63 <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="right" title="Tooltip on right">
64   Tooltip on right
65 </button>
66 <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="bottom" title="Tooltip on bottom">
67   Tooltip on bottom
68 </button>
69 <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="left" title="Tooltip on left">
70   Tooltip on left
71 </button>
72 {% endhighlight %}
73
74 And with custom HTML added:
75
76 {% highlight html %}
77 <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-html="true" title="<em>Tooltip</em> <u>with</u> <b>HTML</b>">
78   Tooltip with HTML
79 </button>
80 {% endhighlight %}
81
82 ## Usage
83
84 The tooltip plugin generates content and markup on demand, and by default places tooltips after their trigger element.
85
86 Trigger the tooltip via JavaScript:
87
88 {% highlight js %}
89 $('#example').tooltip(options)
90 {% endhighlight %}
91
92 {% capture callout %}
93 ##### Overflow `auto` and `scroll`
94
95 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'`:
96
97 {% highlight js %}
98 $('#example').tooltip({ boundary: 'window' })
99 {% endhighlight %}
100 {% endcapture %}
101 {% include callout.html content=callout type="warning" %}
102
103 ### Markup
104
105 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).
106
107 {% capture callout %}
108 ##### Making tooltips work for keyboard and assistive technology users
109
110 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. In addition, most assistive technologies currently do not announce the tooltip in this situation.
111
112 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.
113 {% endcapture %}
114 {% include callout.html content=callout type="warning" %}
115
116 {% highlight html %}
117 <!-- HTML to write -->
118 <a href="#" data-toggle="tooltip" title="Some tooltip text!">Hover over me</a>
119
120 <!-- Generated markup by the plugin -->
121 <div class="tooltip bs-tooltip-top" role="tooltip">
122   <div class="arrow"></div>
123   <div class="tooltip-inner">
124     Some tooltip text!
125   </div>
126 </div>
127 {% endhighlight %}
128
129 ### Disabled elements
130
131 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.
132
133 <div class="tooltip-demo">
134 {% capture example %}
135 <span class="d-inline-block" tabindex="0" data-toggle="tooltip" title="Disabled tooltip">
136   <button class="btn btn-primary" style="pointer-events: none;" type="button" disabled>Disabled button</button>
137 </span>
138 {% endcapture %}
139 {% include example.html content=example %}
140 </div>
141
142 ### Options
143
144 Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-`, as in `data-animation=""`.
145
146 <table class="table table-bordered table-striped">
147   <thead>
148     <tr>
149       <th style="width: 100px;">Name</th>
150       <th style="width: 100px;">Type</th>
151       <th style="width: 50px;">Default</th>
152       <th>Description</th>
153     </tr>
154   </thead>
155   <tbody>
156     <tr>
157       <td>animation</td>
158       <td>boolean</td>
159       <td>true</td>
160       <td>Apply a CSS fade transition to the tooltip</td>
161     </tr>
162     <tr>
163       <td>container</td>
164       <td>string | element | false</td>
165       <td>false</td>
166       <td>
167         <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>
168       </td>
169     </tr>
170     <tr>
171       <td>delay</td>
172       <td>number | object</td>
173       <td>0</td>
174       <td>
175         <p>Delay showing and hiding the tooltip (ms) - does not apply to manual trigger type</p>
176         <p>If a number is supplied, delay is applied to both hide/show</p>
177         <p>Object structure is: <code>delay: { "show": 500, "hide": 100 }</code></p>
178       </td>
179     </tr>
180     <tr>
181       <td>html</td>
182       <td>boolean</td>
183       <td>false</td>
184       <td>
185         <p>Allow HTML in the tooltip.</p>
186         <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>
187         <p>Use text if you're worried about XSS attacks.</p>
188       </td>
189     </tr>
190     <tr>
191       <td>placement</td>
192       <td>string | function</td>
193       <td>'top'</td>
194       <td>
195         <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>
196         <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>
197       </td>
198     </tr>
199     <tr>
200       <td>selector</td>
201       <td>string | false</td>
202       <td>false</td>
203       <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="https://github.com/twbs/bootstrap/issues/4215">this</a> and <a href="https://codepen.io/Johann-S/pen/djJYPb">an informative example</a>.</td>
204     </tr>
205     <tr>
206       <td>template</td>
207       <td>string</td>
208       <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>
209       <td>
210         <p>Base HTML to use when creating the tooltip.</p>
211         <p>The tooltip's <code>title</code> will be injected into the <code>.tooltip-inner</code>.</p>
212         <p><code>.arrow</code> will become the tooltip's arrow.</p>
213         <p>The outermost wrapper element should have the <code>.tooltip</code> class and <code>role="tooltip"</code>.</p>
214       </td>
215     </tr>
216     <tr>
217       <td>title</td>
218       <td>string | element | function</td>
219       <td>''</td>
220       <td>
221         <p>Default title value if <code>title</code> attribute isn't present.</p>
222         <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>
223       </td>
224     </tr>
225     <tr>
226       <td>trigger</td>
227       <td>string</td>
228       <td>'hover focus'</td>
229       <td>
230         <p>How tooltip is triggered - click | hover | focus | manual. You may pass multiple triggers; separate them with a space.</p>
231         <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>
232         <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>
233       </td>
234     </tr>
235     <tr>
236       <td>offset</td>
237       <td>number | string</td>
238       <td>0</td>
239       <td>Offset of the tooltip relative to its target. For more information refer to Popper.js's <a href="https://popper.js.org/popper-documentation.html#modifiers..offset.offset">offset docs</a>.</td>
240     </tr>
241     <tr>
242       <td>fallbackPlacement</td>
243       <td>string | array</td>
244       <td>'flip'</td>
245       <td>Allow to specify which position Popper will use on fallback. For more information refer to
246       Popper.js's <a href="https://popper.js.org/popper-documentation.html#modifiers..flip.behavior">behavior docs</a></td>
247     </tr>
248     <tr>
249       <td>boundary</td>
250       <td>string | element</td>
251       <td>'scrollParent'</td>
252       <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.js's <a href="https://popper.js.org/popper-documentation.html#modifiers..preventOverflow.boundariesElement">preventOverflow docs</a>.</td>
253     </tr>
254   </tbody>
255 </table>
256
257 {% capture callout %}
258 #### Data attributes for individual tooltips
259
260 Options for individual tooltips can alternatively be specified through the use of data attributes, as explained above.
261 {% endcapture %}
262 {% include callout.html content=callout type="info" %}
263
264 ### Methods
265
266 {% include callout-danger-async-methods.md %}
267
268 #### `$().tooltip(options)`
269
270 Attaches a tooltip handler to an element collection.
271
272 #### `.tooltip('show')`
273
274 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.
275
276 {% highlight js %}$('#element').tooltip('show'){% endhighlight %}
277
278 #### `.tooltip('hide')`
279
280 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.
281
282 {% highlight js %}$('#element').tooltip('hide'){% endhighlight %}
283
284 #### `.tooltip('toggle')`
285
286 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.
287
288 {% highlight js %}$('#element').tooltip('toggle'){% endhighlight %}
289
290 #### `.tooltip('dispose')`
291
292 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.
293
294 {% highlight js %}$('#element').tooltip('dispose'){% endhighlight %}
295
296 #### `.tooltip('enable')`
297
298 Gives an element's tooltip the ability to be shown. **Tooltips are enabled by default.**
299
300 {% highlight js %}$('#element').tooltip('enable'){% endhighlight %}
301
302 #### `.tooltip('disable')`
303
304 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.
305
306 {% highlight js %}$('#element').tooltip('disable'){% endhighlight %}
307
308 #### `.tooltip('toggleEnabled')`
309
310 Toggles the ability for an element's tooltip to be shown or hidden.
311
312 {% highlight js %}$('#element').tooltip('toggleEnabled'){% endhighlight %}
313
314 #### `.tooltip('update')`
315
316 Updates the position of an element's tooltip.
317
318 {% highlight js %}$('#element').tooltip('update'){% endhighlight %}
319
320 ### Events
321
322 <table class="table table-bordered table-striped">
323   <thead>
324     <tr>
325       <th style="width: 150px;">Event Type</th>
326       <th>Description</th>
327     </tr>
328   </thead>
329   <tbody>
330     <tr>
331       <td>show.bs.tooltip</td>
332       <td>This event fires immediately when the <code>show</code> instance method is called.</td>
333     </tr>
334     <tr>
335       <td>shown.bs.tooltip</td>
336       <td>This event is fired when the tooltip has been made visible to the user (will wait for CSS transitions to complete).</td>
337     </tr>
338     <tr>
339       <td>hide.bs.tooltip</td>
340       <td>This event is fired immediately when the <code>hide</code> instance method has been called.</td>
341     </tr>
342     <tr>
343       <td>hidden.bs.tooltip</td>
344       <td>This event is fired when the tooltip has finished being hidden from the user (will wait for CSS transitions to complete).</td>
345     </tr>
346     <tr>
347       <td>inserted.bs.tooltip</td>
348       <td>This event is fired after the <code>show.bs.tooltip</code> event when the tooltip template has been added to the DOM.</td>
349     </tr>
350   </tbody>
351 </table>
352
353 {% highlight js %}
354 $('#myTooltip').on('hidden.bs.tooltip', function () {
355   // do something…
356 })
357 {% endhighlight %}