Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Bootstrap Tooltip: flickers when placed on top of button, but works fine when placed at the left/right/bottom

I am dealing with some weird behavior for an instance of bootstrap's tooltip.

The page I am working with has several buttons that, when hovered over, display tooltips with the description for the buttons' functionality. The tooltips are all displayed on top of the buttons, and, with the exception of one button, everything works fine. This one button displays the tooltip with a continuous flickering, the tooltip itself covers part of the button (instead of being completely on top of the button), and prevents the button from being properly clicked. If the "data-placement" for the tooltip is changed from "top" to "left"/"right"/"bottom", the tooltip is displayed correctly.

Additionally, the button that gives me problems is wrapped in a div that has "float: right;" assigned in the css. I am mentioning this because I noticed that if I remove the float, the tooltip works fine. Unfortunately, if I remove the float, the button itself loses its correct positioning.

While I could give up the "top" positioning for the tooltip, I was hoping that there might be an easy trick to this problem. Does anyone have any suggestions?

Thank you.

Update:

This StackOverflow question presents the same problem as the one I was encountering. I found the answer useful.

like image 405
AndraD Avatar asked Jan 14 '13 21:01

AndraD


People also ask

How do I stop my tooltip from flickering?

1 Answer. Show activity on this post. Set your tooltip "pointer-events" CSS property to "none".

How do I change the tooltip position in bootstrap?

How to position the tooltip - auto | top | bottom | left | right. When auto is specified, it will dynamically reorient the tooltip. 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.

How do I make my tooltip always visible?

Single element To make an element display its tooltip permanently, we use its showTooltipOn property. To make tooltip always be shown, set it to "always" .

What placement does the tooltip use by default?

Positioning Tooltips By default, the tooltip will appear on top of the element.


2 Answers

Add to the tooltip pointer-events: none; css rule, like

.tooltip {   pointer-events: none; } 

This will prevent tooltip from being target for mouse events and will resolve the issue.

like image 100
DicBrus Avatar answered Sep 21 '22 13:09

DicBrus


Read The Docs

The docs on Bootstrap 4 explicitly address this issue and also provide a solution:

Overflow auto and scroll

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':

$('#example').tooltip({ boundary: 'window' })

So we have to set the boundary option to 'window'.

From the docs on the boundary option:

Overflow constraint boundary of the tooltip. Accepts the values of 'viewport', 'window', 'scrollParent', or an HTMLElement reference (JavaScript only). For more information refer to Popper.js's preventOverflow docs.

Best Practice

That being said, the preferred way of initializing tooltips everywhere is to use '[data-toggle="tooltip"]' as selector:

$('[data-toggle="tooltip"]').tooltip({ boundary: 'window' }) 

Once And For All

Now if you are dynamically adding or removing elements (with tooltips) to the DOM or are even replacing the whole page without reloading it (i.e. by using a pushState library like PJAX) - or if you are just using modals (with tooltips) - you have to initialize those tooltips (again).

Fixing Tooltips On Elements

That's where the following function comes in handy, which destroys and recreates all tooltips. Call this function after you've added / removed an element to / from the DOM that has / had a tooltip attached.

function recreateTooltips() {     $('[data-toggle="tooltip"]').tooltip('dispose').tooltip({boundary: 'window'}); } 

This might seem expensive (and certainly is), but I've never noticed any performance impact even with dozens of tooltips on the same page.

Fixing Tooltips In Modals

To fix tooltips in modals, you simply bind the function from above to the 'shown.bs.modal' event, which fires after a modal is fully shown to the user.

/** Recreate tooltips upon showing modal */ $(document).on('shown.bs.modal', '*', function () {     recreateTooltips(); }); 
like image 45
Tobias Wicker Avatar answered Sep 25 '22 13:09

Tobias Wicker