I am using Bootstrap's tooltip script and have a problem when I have the tooltip on text that gets truncated. I am using CSS to truncate the text, and then have a tooltip on that text so you can mouse over and see the full text. My problem is that when the text is truncated, the tooltip still centers on the element as if it wasn't truncated.
Aside from this issue, I'd just rather it be styled like the following anyway. I would like to have it so the tooltip aligns to the left of the element, and the arrow on the tooltip goes to the left as well. Or if the tooltip is on the side of the element then it gets aligned to the top and the arrow is on the top.
Basically it looks like this right now:
################
# TOOLTIP #
################
v
Some really long text th...
I would like it to look like this:
################
# TOOLTIP #
################
v
Some really long text th...
Or for on the side like this:
################ > Some really long text th...
# TOOLTIP #
################
Thank you.
[EDIT] I've firgured out the moving of the arrow, that's just simple css. But the aligning of the tooltip itself seems to be part of the javascript.
tooltipped-align-left-1: This class is used on the tooltipped container to align the tooltip to the left of the element with an arrow more intended. tooltipped-align-left-2: This class is used on the tooltipped container to align the tooltip to the left of the element with an arrow less intended.
The tooltip text is placed inside an inline element (like <span>) with class="tooltiptext" . CSS: The tooltip class use position:relative , which is needed to position the tooltip text ( position:absolute ). Note: See examples below on how to position the tooltip.
Positioning of tooltips So to position a tooltip as your requirement just add data-bs-placement=”top/right/left/bottom” attribute to your <a> or <button> tag to change tooltip position.
Here is how to do it with position: absolute and relative . On the container, set position: relative , and display: table (shrink to fit). For the position: absolute tooltip, set top: 0 , and left: 100% (moves to the right edge of the relative container).
To achieve the following layout:
element
^
################
# TOOLTIP #
################
CSS
.tooltip.bottom .tooltip-arrow {
left: 90% !important;
}
JS
$('[data-toggle=tooltip]').on('inserted.bs.tooltip', function() {
$('.tooltip').css('opacity', 0);
});
$(''[data-toggle=tooltip]'').on('shown.bs.tooltip', function() {
var $tooltip = $('.tooltip');
$tooltip.css('left', $tooltip.offset().left - $tooltip.width() / 2);
$tooltip.css('opacity', 1);
});
There is a small delay due to hiding and showing the tooltip but the positioning works fine regardless of the tooltip size.
I think you'll need to adjust the tooltip-arrow
CSS and the position of the entire tooltip
. This will get you close to it, but it may need more tweaks...
.tooltip-arrow CSS
.tooltip.right .tooltip-arrow{
top: 5%;
margin-top: 0px;
}
.tooltip.top .tooltip-arrow{
left: 6%;
bottom:1px;
}
jQuery function to adjust placement of the tooltip..
$("[data-placement=right]").hover(function(){
$('.tooltip').css('top',parseInt($('.tooltip').css('top')) + 8 + 'px')
});
EDIT: You can also position the .tooltip with CSS only. Alternate demo
.tooltip.right {
margin-top:8px;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With