83

How to align the tooltip the natural style: right bottom of the mouse pointer?

<!DOCTYPE html>
<html>
<head>
  <title>Tooltip with Image</title>
  <meta charset="UTF-8">
  <style type="text/css">
    .tooltip {
        text-decoration:none;
        position:relative;
    }
     .tooltip span {
        display:none;
    }
     .tooltip span img {
        float:left;
    }
     .tooltip:hover span {
        display:block;
        position:absolute;
        overflow:hidden;
    }
  </style>
</head>
<body>
  <a class="tooltip" href="http://www.google.com/">
    Google
    <span>
      <img alt="" src="http://www.google.com/images/srpr/logo4w.png">
    </span>
  </a>
</body>
</html>
John Slegers
  • 45,213
  • 22
  • 199
  • 169
Ben
  • 1,550
  • 1
  • 16
  • 24
  • 1
    Are you using jquery ? Or only css/javascript ? Do you need to follow the mouse pointer when hovering the element or just display one time the tooltip at a position when first hovering the object ? – Stéphane V Mar 29 '13 at 12:17
  • No jQuery, no JavaScript, only CSS. I don't need following, I just want the common tooltip behaviour. – Ben Mar 29 '13 at 12:28

7 Answers7

116

For default tooltip behavior simply add the title attribute. This can't contain images though.

<div title="regular tooltip">Hover me</div>

Before you clarified the question I did this up in pure JavaScript, hope you find it useful. The image will pop up and follow the mouse.

jsFiddle

JavaScript

var tooltipSpan = document.getElementById('tooltip-span');

window.onmousemove = function (e) {
    var x = e.clientX,
        y = e.clientY;
    tooltipSpan.style.top = (y + 20) + 'px';
    tooltipSpan.style.left = (x + 20) + 'px';
};

CSS

.tooltip span {
    display:none;
}
.tooltip:hover span {
    display:block;
    position:fixed;
    overflow:hidden;
}

Extending for multiple elements

One solution for multiple elements is to update all tooltip span's and setting them under the cursor on mouse move.

jsFiddle

var tooltips = document.querySelectorAll('.tooltip span');

window.onmousemove = function (e) {
    var x = (e.clientX + 20) + 'px',
        y = (e.clientY + 20) + 'px';
    for (var i = 0; i < tooltips.length; i++) {
        tooltips[i].style.top = y;
        tooltips[i].style.left = x;
    }
};
Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
  • 1
    Thank you very much! But I need images in tooltips, that's the point. And I don't want to use any JavaScript. Again, thank you anyway. – Ben Mar 29 '13 at 12:35
  • 1
    Impossible to follow the mouse without JavaScript unfortunately. Best you can do is display like you have in your example. Any reason for no JavaScript? – Daniel Imms Mar 29 '13 at 12:36
  • As stated above, I don't need any following. – Ben Mar 29 '13 at 12:39
  • You do need it if you want that functionality :) literally every browser now days has it turned on. – Daniel Imms Mar 29 '13 at 12:41
  • 1
    Adding *transition* to it and everything is perfect :) – Huy Le Mar 12 '17 at 07:26
  • Thanks for your code , make a multi line link to observe your bug :| –  Aug 26 '19 at 09:15
  • Is it correct, for each movement of mouse it runs the function window.onmousemove = function. – Prabha Jul 02 '20 at 20:16
15

One way to do this without JS is to use the hover action to reveal a HTML element that is otherwise hidden, see this codepen:

http://codepen.io/c0un7z3r0/pen/LZWXEw

Note that the span that contains the tooltip content is relative to the parent li. The magic is here:

ul#list_of_thrones li > span{
  display:none;
}
ul#list_of_thrones li:hover > span{
  position: absolute;
  display:block;
  ...
}

As you can see, the span is hidden unless the listitem is hovered over, thus revealing the span element, the span can contain as much html as you need. In the codepen attached I have also used a :after element for the arrow but that of course is entirely optional and has only been included in this example for cosmetic purposes.

I hope this helps, I felt compelled to post as all the other answers included JS solutions but the OP asked for a HTML/CSS only solution.

Anthony Cregan
  • 960
  • 11
  • 25
  • 4
    This is not relative to mouse pointer position. It is relative to DOM element. – CrazySabbath Sep 25 '17 at 09:01
  • 1
    My example is relative to the hovered element, not the pointer. Either the question has been updated or I've misunderstood, either way, the JS examples given are the correct solution, you cant do that with CSS alone. I've issued my vote elsewhere on this question accordingly. – Anthony Cregan Sep 25 '17 at 20:57
  • Not to be crass, but I don't see what's there to misunderstand, the title states '..relative to mouse pointer' – CrazySabbath Sep 26 '17 at 06:33
  • 1
    Not to be blunt but if you read the other posts you'll see that this question has been updated and expanded upon a lot, the question was not clear when I posted my answer, the question has since been expanded upon and clarified. I did not delete the answer because it points out that JS is required to achieve the effect required and if a HTML/CSS only solution is required then this is the closest you'll get without using JS too. – Anthony Cregan Sep 27 '17 at 10:57
  • 5
    This is a great solution for someone wanting HTML/CSS-only and has small-enough DOM elements that the tooltip appears pretty much where the mouse pointer is. In my case, "help" icons that, on hover, display richer content than the title attribute supports. +1 – bigdogwillfeed Nov 16 '17 at 02:06
11

I prefer this technique:

function showTooltip(e) {
  var tooltip = e.target.classList.contains("tooltip")
      ? e.target
      : e.target.querySelector(":scope .tooltip");
  tooltip.style.left =
      (e.pageX + tooltip.clientWidth + 10 < document.body.clientWidth)
          ? (e.pageX + 10 + "px")
          : (document.body.clientWidth + 5 - tooltip.clientWidth + "px");
  tooltip.style.top =
      (e.pageY + tooltip.clientHeight + 10 < document.body.clientHeight)
          ? (e.pageY + 10 + "px")
          : (document.body.clientHeight + 5 - tooltip.clientHeight + "px");
}

var tooltips = document.querySelectorAll('.couponcode');
for(var i = 0; i < tooltips.length; i++) {
  tooltips[i].addEventListener('mousemove', showTooltip);
}
.couponcode {
    color: red;
    cursor: pointer;
}

.couponcode:hover .tooltip {
    display: block;
}

.tooltip {
    position: absolute;
    white-space: nowrap;
    display: none;
    background: #ffffcc;
    border: 1px solid black;
    padding: 5px;
    z-index: 1000;
    color: black;
}
Lorem ipsum dolor sit amet, <span class="couponcode">consectetur
adipiscing<span class="tooltip">This is a tooltip</span></span>
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat. Duis aute irure dolor in <span
class="couponcode">reprehenderit<span class="tooltip">This is
another tooltip</span></span> in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est <span
class="couponcode">laborum<span class="tooltip">This is yet
another tooltip</span></span>.

(see also this Fiddle)

John Slegers
  • 45,213
  • 22
  • 199
  • 169
  • 1
    The above code works fine as long as the page does not contain vertical scroll bar, It it has max height this does not work. Go with normal tooltip with jquery style. – Prabha Jul 09 '20 at 04:36
  • 5px is an **assumption** - I have my mouse pointer set to large (>32px), I can never move my pointer out the way of the tooltip text. – c z Nov 03 '21 at 14:57
7

You can use jQuery UI plugin, following are reference URLs

Set track to TRUE for Tooltip position relative to mouse pointer eg.

$('.tooltip').tooltip({ track: true });
Atul Bhosale
  • 396
  • 5
  • 13
4

This CAN be done with pure html and css. It may not be the best way but we all have different limitations. There are 3 ways that could be useful depending on what your specific circumstances are.

  1. The first involves overlaying an invisible table over top of your link with a hover action on each cell that displays an image.

#imagehover td:hover::after{
  content: "             ";
  white-space: pre;
  background-image: url("http://www.google.com/images/srpr/logo4w.png");
  position: relative;
  left: 5px;
  top: 5px;
  font-size: 20px;
  background-color: transparent;
  background-position: 0px 0px;
  background-size: 60px 20px;
  background-repeat: no-repeat;
  
}


#imagehover table, #imagehover th, #imagehover td {
  border: 0px;
  border-spacing: 0px;
}
<a href="https://www.google.com">
<table id="imagehover" style="width:50px;height:10px;z-index:9999;position:absolute" cellspacing="0">
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>

</table>
Google</a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6v81Q+r/NUPq/zVD6v81Q+vrNkTtXQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/1dXV/+Tk5P/k5OT/5OTk/+Tk5P8AAAD/AAAAADpI7zA3ReuqNUPq+jVD6v81Q+r/NUPq/zVD6v81Q+r/NUPq/zVD6v81Q+r/NUPq/zVD6v81Q+vqNkTsgzdF8REAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAAAAAAAAA8SvEWNkTsZDZE66Q1Q+vSNUPr8DVD6v01Q+r6NkTr5zZE68Q3ReyQOEXtSj5M9QYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/8AD//8AAP/8AAA/+AAAH/AAAA/gAAAHwAAAB8AH4AOAH/gDgD/8AQB//gEA//4BAP4AAQH+AAEB/gABAf4AAQH+AAEB/gABAf4AAQD/AAEA////AH///4A///+AH/n/QAfg/wAAAH8AAAA/AAAAHwAAAB8AAAB/AQAA/wDAA/8=),auto;

}
<a href="https://www.google.com" id="googleLink">Google</a>
  1. While the normal title tooltip doesn't technically allow images it does allow any unicode characters including block letters and emojis which may suit your needs.

<a href="https://www.google.com" title="️️" alt="️️">Google</a>
3

We can achieve the same using "Directive" in Angularjs.

            //Bind mousemove event to the element which will show tooltip
            $("#tooltip").mousemove(function(e) {
                //find X & Y coodrinates
                x = e.clientX,
                y = e.clientY;

                //Set tooltip position according to mouse position
                tooltipSpan.style.top = (y + 20) + 'px';
                tooltipSpan.style.left = (x + 20) + 'px';
            });

You can check this post for further details. http://www.ufthelp.com/2014/12/Tooltip-Directive-AngularJS.html

AugustRush
  • 367
  • 4
  • 4
-1

function showTooltip(e) {
  var tooltip = e.target.classList.contains("tooltip")
      ? e.target
      : e.target.querySelector(":scope .tooltip");
  tooltip.style.left =
      (e.clientX + tooltip.clientWidth + 10 < document.documentElement.clientWidth)
          ? (e.clientX + 10 + "px")
          : (document.documentElement.clientWidth + 5 - tooltip.clientWidth + "px");
  tooltip.style.top =
      (e.clientY + tooltip.clientHeight + 10 < document.documentElement.clientHeight)
          ? (e.clientY + 10 + "px")
          : (document.documentElement.clientHeight + 5 - tooltip.clientHeight + "px");
}

var tooltips = document.querySelectorAll('.hastip');
for(var i = 0; i < tooltips.length; i++) {
  tooltips[i].addEventListener('mousemove', showTooltip);
}
.hastip {
    border-bottom: 1px dotted black;
    position: relative;
    display: inline-block;
    cursor: pointer;
}

.hastip:hover .tooltip {
    display: block;
}

.tooltip {
    position: fixed;
    white-space: nowrap;
    display: none;
    background-color: #ffffcc;
    border: 1px solid black;
    color: #000;
    padding: 5px;
    border-radius: 6px;
    z-index: 1000;
    font-size: 13pt;
    font-weight: initial;
}
Lorem ipsum dolor sit amet, <span class="hastip">consectetur
adipiscing<span class="tooltip">This is a tooltip</span></span>
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat. Duis aute irure dolor in <span
class="hastip">reprehenderit<span class="tooltip">This is
another tooltip</span></span> in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est <span
class="hastip">laborum<span class="tooltip">This is yet
another tooltip</span></span>.
Emily V
  • 1
  • 2