Add a Tooltip to a Div

Add a tooltip to a div

For the basic tooltip, you want:

<div title="This is my tooltip">

like:

.visible {
height: 3em;
width: 10em;
background: yellow;
}
<div title="This is my tooltip" class="visible"></div>

How to add details to tooltip other than title

As in comments suggest, you should use Popovers instead of tooltip.

<link href="https://getbootstrap.com/docs/4.4/dist/css/bootstrap.min.css" rel="stylesheet" /><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script src="https://getbootstrap.com/docs/4.4/dist/js/bootstrap.bundle.min.js"></script><script>  $(function() {    $('[data-toggle="popover"]').popover()  })</script><h1>Popover Example</h1><button type="button" class="btn btn-lg btn-primary" data-toggle="popover" title="Popover title" data-content="Popove`enter code here`r content. example content for popover">Click to toggle popover</button>

How to Change div text to tooltip in bootstrap?

You will have to loop all divs and set their respective tooltip text from the div text

TRY THIS DEMO

// loop each classdiv and add data-toggle and title attributes$('.classdiv').each(function(ndx, elem) {    var $elem = $(elem);    var text = $elem.text().trim();    $elem.attr('title', text) // set title attribute        .text('') // empty the text         .data('toggle', 'tooltip') // add data-toggle = tooltip        .tooltip(); // activate bootstrap tooltip})
.classdiv {    width: initial;    display: inline-block;    margin: 45px 20px;    padding: 10px;    background: grey;}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script><div class="classdiv">11</div><div class="classdiv">22</div><div class="classdiv">33</div><div class="classdiv">44</div><div class="classdiv">55</div><div class="classdiv">66</div>

Positioning the tooltip to the left/beginning of the div

Your parent div is acting as the relative container for the absolute positioned child div that holds the tooltip. So it's doing as expected showing over the icon. Include the text to contain the tooltip into the container and it will display as expected. After you change the top/left values to a valid 0 value. Except when you do that, now every hover is cancelled out by the tooltip panel showing over top of the icon which gives you a nasty flashing tooltip effect. So you need to separate the two so while user has mouse over the icon then the tooltip displays and vice versa. Here's to get you started. Cheers!

.tooltips {
position: relative;
display: inline;
}

.tooltips .tooltiptext {
display: inline-flex;
align-items: center;
padding: 0 .25rem;
visibility: hidden;
background-color: #FFFFFF;
color: #7A7A7A;
border-radius: 3px;
font-size: 13px;
box-shadow: 0px 0px 10px -2px rgba(0,0,0,0.5);
position: absolute;
top: -3px;
left: -3px;
bottom: -3px;
right: 15px;
background-color: #f1f1f1;
}
.tooltipicon { display:inline; cursor: help }
.tooltipicon:hover { background-color: red; }
.tooltipicon:hover + .tooltiptext {
visibility: visible;
}
<div class="tooltips">
Geeignete Anzahl monatl. Besuche <div class="tooltipicon">♠</div>
<div class="tooltiptext">Tooltip text</div>
</div>

Angular: add a tooltip to a div in runtime

First, you create a directive as below,

import { Directive, ElementRef, HostListener, Input } from '@angular/core';

@Directive({
selector: '[hintText]',
})
export class HighlightDirective {
constructor(private el: ElementRef) {}

@Input()
set hintText(value: string[]) {
this.el.nativeElement.title = value;
}
}

then use the directive in your DOM element

<div [hintText]="hintText1">Place mouse pointer on me!</div>

<div [hintText]="hintText2">Place mouse pointer on me!</div>

dynamically change your hint text from the component,

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent implements OnInit {
hintText1: string;
hintText2: string;

ngOnInit(): void {
this.hintText1 = 'my text title 1';
this.hintText2 = 'my text title 2';
}
}

JavaScript - tooltip div on hover

Bootstrap does all this and more: http://getbootstrap.com/javascript/#tooltips

To put it in:

$('#example').tooltip(options)

And the markup being:

<div class="tooltip">
<div class="tooltip-inner">
Tooltip text here!
</div>
<div class="tooltip-arrow"></div>
</div>

Go to the link for a more detailed explanation on how to implement it

How to use a single Vanilla JS tooltip div to display 100+ different tooltips?

After much more trial and error fixed it like so:

///////////////////////////////////////////////////
// SHOW tooltips
///////////////////////////////////////////////////
let tooltipContainer = document.getElementById('tooltipContainer');
const tooltipBox = document.getElementById('tooltipBox');
document.addEventListener('mousemove', function checkHover(e) {
tooltipContainer.style.left = getWidth() > e.pageX + 400 ? e.pageX + 2 + 'px' : e.pageX - 402 + 'px';
tooltipContainer.style.top = e.pageY + 2 + 'px';
const allTooltips = document.getElementsByClassName('tooltip');
for (var i = 0; i < allTooltips.length; i++) {
if (allTooltips.item(i).querySelector(':hover')) {
tooltipBox.innerHTML = allTooltips.item(i).lastChild.innerHTML; tooltipBox.style.display = "block"; return;
}
}
tooltipBox.innerHTML = ""; tooltipBox.style.display = "none";
});

Basically every time the mouse moves there is a single MouseMove event that checks the mouse position, adjusts the position of the DIV, and finally runs through all elements with tooltip to check if any is hovered.

How to make tooltip appear beneath a div instead of covering it?

Just add a top value of the height of the hovered container (60px in your case) to your absolutely positioned tooltip element.

Also by the way, </br> is invalid markup. It should either be a standalone <br> or a self-closing <br/> in X(HT)ML.

.Timeline {  display: flex;  align-items: center;  height: 500px;  margin-left: 80px;}
.event1 { position: relative;}
.event1Bubble { position: absolute; background-color: rgba(158, 158, 158, 0.1); width: 130px; height: 60px; top: -70px; left: -15px; border-radius: 5px; box-shadow: inset 0 0 5px rgba(158, 158, 158, 0.64)}
.event1Bubble:after,.event1Bubble:before { content: ""; position: absolute; width: 0; height: 0; border-style: solid; border-color: transparent; border-bottom: 0;}
.event1Bubble:before { bottom: -10px; left: 13px; border-top-color: rgba(222, 222, 222, 0.66); border-width: 12px;}
.event1Bubble:after { bottom: -8px; left: 13px; border-top-color: #F6F6F6; border-width: 12px;}
.eventTime { display: flex;}
.DayDigit { font-size: 27px; font-family: "Arial Black", Gadget, sans-serif; margin-left: 10px; color: #4C4A4A;}
.Day { font-size: 11px; margin-left: 5px; font-weight: bold; margin-top: 10px; font-family: Arial, Helvetica, sans-serif; color: #4C4A4A;}
.MonthYear { font-weight: 600; line-height: 10px; color: #9E9E9E; font-size: 9px;}
.active { font-family: "Arial Black", Gadget, sans-serif; color: #228B22; font-size: 11px; text-transform: uppercase; display: flex; flex: 1; align-items: center; margin-left: 12px; margin-top: -2px;}
.tooltip { position: relative; display: inline-block;}
.tooltip .tooltiptext { visibility: hidden; width: 480px; background-color: black; font-family: Arial, Helvetica, sans-serif; color: #fff; text-align: left; padding: 5px 0; padding-left: 5px; border-radius: 6px; position: absolute; z-index: 1; top: 60px;}
.tooltip:hover .tooltiptext { visibility: visible;}
<div class="Timeline">  <div class="event1">    <div class="event1Bubble tooltip">      <span class="tooltiptext">id: 12345<br>                                    starts_on: 2019-03-07<br>                                    start_reason: something<br>                                    ends_on:<br>                                    end_reason:<br>                                    type: something          </span>      <div class="eventTime">        <div class="DayDigit">10</div>        <div class="Day">          Wednesday          <div class="MonthYear">September 2018</div>        </div>      </div>      <div class="active">Active</div>    </div>  </div></div>

How to create a tooltip?

The CSS Solution

There is a very simple solution to the problem at hand. What I essentially added is the following CSS code

word-wrap:break-word;

to the class that surrounds your tooltip text.

Here is a working demo

Also, this is a good read

What if I am using Angular UI?

For styling a tooltip from angular ui, I added tooltip-class="tooltip" to the tooltip code.

Here is a working demo with angular

This is a modified plunkr obtained from the Angular UI documentation



Related Topics



Leave a reply



Submit