8

I'm trying to make small image gallery.On :hover there should appear a tooltip on the left side of every image. My HTML code looks like this:

.gallery {
  right: 247px;
  width: 220px;
}

.gallery li {
  display: inline;
  width: 100px;
  height: 100px;
  position: relative;
}

.gallery img {
  float: right;
  margin: 10px;
  box-shadow: 0 0 0 8px rgba(0, 0, 0, 0.3);
  -webkit-background-clip: padding-box;
  /* for Safari */
  background-clip: padding-box;
  /* for IE9+, Firefox 4+, Opera, Chrome */
}

.tooltip {
  width: 300px;
  height: 100px;
  display: none;
  position: absolute;
  right: -108px;
  top: 222px;
  z-index: 70;
  background-color: rgba(0, 0, 0, 0.4);
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(opacity=40)";
}

.gallery li:hover>.tooltip {
  display: inline;
  z-index: 70;
}

.gallery li:hover {
  background: #fff;
}

.gallery li:hover img {
  box-shadow: 0 0 0 8px rgba(255, 255, 255, 1);
}
<ul class="gallery">
  <li>
    <a href="#" class="thumb"><img src="img/pillow.jpg" alt=""></a>
    <div class="tooltip"></div>
  </li>
  <li>
    <a href="#" class="thumb"><img src="" alt=""></a>
    <div class="tooltip"></div>
  </li>
  <li>
    <a href="#" class="thumb"><img src="" alt=""></a>
    <div class="tooltip"></div>
  </li>
  <li>
    <a href="#" class="thumb "><img src="" alt=""></a>
    <div class="tooltip"></div>
  </li>
  <li>
    <a href="#" class="thumb"><img src="" alt=""></a>
    <div class="tooltip"></div>
  </li>
</ul>

The idea is, as said, that .tooltip has position:absolute and it should appear to the left of hovered <li>, but somehow it appears at the same place every time. What can be the reason for it?

isherwood
  • 58,414
  • 16
  • 114
  • 157
vitalym
  • 893
  • 6
  • 12
  • 29
  • 2
    The parent of any `position:absolute;` element needs to have its own position declared to be something other than static for it to be relative to that parent. try adding `position:relative;` to your `.thumb` links. – PlantTheIdea Mar 15 '14 at 17:16
  • possible duplicate of [Position absolute but relative to parent](http://stackoverflow.com/questions/10487292/position-absolute-but-relative-to-parent) – bjb568 Mar 15 '14 at 17:43
  • If i set position:relative to anchor (or to image), tooltip changes the place it appears, but the main problem remains: tooltip appears at the exactly same place, doesn't matter which li/img/a is focused. – vitalym Mar 15 '14 at 18:03
  • [Here's my very simple & lightweight package for this](https://github.com/yairEO/position) – vsync Oct 28 '22 at 19:19

5 Answers5

1

You need to set the li to position:relative, not the a or img, since the li is the actual ancestor; the other two are siblings.

Dre
  • 2,933
  • 2
  • 18
  • 21
0

Try -

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

.tooltip:hover:after {
  background: #333;
  background: rgba(0, 0, 0, .4);
  border-radius: 5px;
  bottom: 26px;
  color: #fff;
  content: attr(tooltip);
  right: -180px;
  top: 222px;
  padding: 5px 15px;
  position: absolute;
  z-index: 98;
  width: 300px;
  height: 100px;
}
<img src="https://via.placeholder.com/100" tooltip="This is a tooltip" alter="alternative text" />
isherwood
  • 58,414
  • 16
  • 114
  • 157
0
.gallery li {
    display: inline;
    width: 100px;
    height: 100px;
    position: relative;
}

This style isn't correct, inline elements doesn't have height and actually width, try display: inline-block

Also, you set display: inline for position: absolute element, more correct also is display: block|inline-block

.gallery img {
    float: right;
    margin: 10px;
    box-shadow: 0 0 0 8px rgba(0, 0, 0, 0.3);
    -webkit-background-clip: padding-box; /* for Safari */
    background-clip: padding-box; /* for IE9+, Firefox 4+, Opera, Chrome */
}

Above code also seems strange, why you need float: right for image, probably you wanted this style for li element, but than don't use display: inline or inline-block

Attenzione
  • 833
  • 9
  • 12
0

Even if your positioning problem is solved, your method won't work on touchscreens such as tablets and phones. This method does, with a bit of Javascript, and solves your problem as well:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Light-weight Tooltip by FC</title>
<style>
h2 {
    text-align: center;
    margin-bottom: 2em;
}
span.tool {
    position: relative;
    display: inline-block;
    border-bottom: 1px dotted black;
}
span.tool:hover {
    cursor: help;
}
span.tip {
    position: absolute;
    bottom: 20px;
    left: 0px;
    display: block;
    width: auto;
    white-space: nowrap;
    font-size: .9em;
    border: 0px solid black; /* change as desired */
    border-radius: 6px;
    padding: 1px 5px;
    background: #eee;
    background: linear-gradient(top, #eee, #ccc);
    background: -moz-linear-gradient(top, #eee, #ccc);
    background: -o-linear-gradient(top, #eee, #ccc);
    background: -webkit-linear-gradient(top, #eee, #ccc);
    background: -webkit-gradient(linear, left top, left bottom, from(#eee), to(#ccc));
    background: -ms-linear-gradient(top, #eee, #ccc);
    filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr=#eeeeee,EndColorStr=#cccccc);
    zoom: 1;
    visibility: hidden;
}
</style>
</head>
<body>

    <h2>Light-weight Tooltip by FC</h2>

    <p>The <span class="tool">WHO<span class="tip">World Health Organization</span></span> was established in 1948.</p>

    <p>
        It is part of the
        <span class="tool">UN
            <span class="tip">United Nations, <br>the successor of the <br>League of Nations</span>
        </span>,
        which was established in 1945.
    </p>

    <hr>

    <p>Explanation and notes:</p>

    <ul>
        <li>The method consists of local nested spans ('tools' with 'tips' inside), positioned relative-absolute.</li>
        <li>If the same tips are to be used several times throughout the page or website, the tip spans can be populated centrally with Javascript or server-side scripting.</li>
        <li>In the current code the width of the tips is set to <i>auto</i>, and controlled with &lt;br&gt;s in the tip text. Change to fixed width as desired.</li>
        <li>With the current code, tablet users must tap (= <i>onclick</i>) rather than press-hold (= <i>onmousedown</i>). It is assumed that that is the intuitive thing most tablet users do, but it can be changed to press-hold.</li>
        <li>The HTML is valid and the code works in IE8+ as well.</li>
        <li>For the sake of completeness: IE9 does not form a border-radius when the element has no declared border, or a border-width of 0.</li>
    </ul>

<script>
    var tools = document.querySelectorAll('.tool'); // mind the dot
    var nrOfTools = tools.length;
    for (var i=0; i<nrOfTools; i++) {
        if ('ontouchstart' in window || (window.DocumentTouch && document instanceof DocumentTouch)) {
            tools[i].onclick = function() {
                if (this.children[0].style.visibility == '' || this.children[0].style.visibility == 'hidden')
                    this.children[0].style.visibility = 'visible';
                else
                    this.children[0].style.visibility = 'hidden';
            }
        }
        else {
            tools[i].onmouseover = function() {
                this.children[0].style.visibility = 'visible';
            }
            tools[i].onmouseout = function() {
                this.children[0].style.visibility = 'hidden';
            }
        }
    }
</script>
</body>
</html>

Live demo on http://jsbin.com/nerul/6/edit?html,output.

0

try this :

.gallery {
        right: 247px;
        width: 220px;
      }

      .gallery li {
        display: block;
        padding: 10px;
        background-color: red;
        margin-top: 10px;
        position: relative;
      }

      .gallery li:hover {
        background: #fff;
        background-color: navy;
      }

      .gallery img {
        margin: 10px;
        box-shadow: 0 0 0 8px navy;
      }
      .tooltip {
        width: 300px;
        height: 100px;
        display: none;
        position: absolute;
        right: -308px;
        z-index: 3;
        background-color: green;
      }

      .gallery li:hover > .tooltip {
        display: inline;
        z-index: 70;
      }

      .gallery li:hover img {
        box-shadow: 0 0 0 8px red;
      }
 <ul class="gallery">
      <li>
        <a href="#" class="thumb"><img src="img/pillow.jpg" alt="" /></a>
        <div class="tooltip"></div>
      </li>
      <li>
        <a href="#" class="thumb"><img src="" alt="" /></a>
        <div class="tooltip"></div>
      </li>
      <li>
        <a href="#" class="thumb"><img src="" alt="" /></a>
        <div class="tooltip"></div>
      </li>
      <li>
        <a href="#" class="thumb"><img src="" alt="" /></a>
        <div class="tooltip"></div>
      </li>
      <li>
        <a href="#" class="thumb"><img src="" alt="" /></a>
        <div class="tooltip"></div>
      </li>
    </ul>
Ali
  • 61
  • 3