Search code examples
javascripthtmlcsstooltiphtml-lists

How to put a tooltip centered under a div?


How can I place the tooltip centered and directly under the red circle without Javascript? Thank you in advance! :-)

Here is an image of my problem and the marked position where it should be:

image

My code currently looks like this:

HTML code:

<li>
   <a class="ovm-oup_tooltip" href="#">
     <div id="ovm-oup_under_item_1" class="ovm-oup_under_item">
        <span>Text</span>
        <img src="Example_Logo.svg" alt="">
     </div>
   </a>
</li>

CSS code:

#ovm-oup_under ul{
  text-align: center;
  list-style-type: none;
  padding: 0;
}
#ovm-oup_under li{
  padding: 20px;
  margin: 15px;
  display: inline-block;
}
.ovm-oup_under_item{
  width: 96px;
  height: 96px;
  border-radius: 50%;
  box-shadow: 0 0 20px 5px rgba(0,0,0,.2);
  margin: 5px 5px 35px 5px;
}

.ovm-oup_tooltip {
    position: relative;
    display: inline;
}
.ovm-oup_tooltip span {
    position: absolute;
    width: auto;
    padding: 3px 15px;
    color: #FFFFFF;
    background: #000000;
    height: 30px;
    line-height: 30px;
    text-align: center;
    visibility: hidden;
    border-radius: 4px;
}
.ovm-oup_tooltip span:after {
    content: '';
    position: absolute;
    bottom: 100%;
    left: 50%;
    margin-left: -8px;
    width: 0; height: 0;
    border-bottom: 8px solid #000000;
    border-right: 8px solid transparent;
    border-left: 8px solid transparent;
}
.ovm-oup_tooltip:hover span {
    visibility: visible;
    margin: 0;
    top: 0%;
    left: 50%;
    z-index: 999;
}

Solution

  • One solution would be to combine absolute placement of the tooltip (relative to the parent div), with a translation transformation on the tooltip, which would achieve the required placement:

    /* Extracted styling to top of style sheet to show the required additions */
    
    .ovm-oup_tooltip span {
      /* Offset the tooltip 50% from left side of parent (div) width
      and 100% from top edge of parent height */
      left: 50%;
      top: 100%;
      
      /* Pull the tooltip back 50% of it's own width, nudge the tool
      tip downward 8px to account for arrow point */
      transform: translate(-50%, 8px);
    }
    
    .ovm-oup_under_item {
      /* Allow absolute placement of children (tooltip) */
      position: relative;
    }
    
    /* Your existing styling starts here */
    .ovm-oup_under_item {
      width: 96px;
      height: 96px;
      border-radius: 50%;
      box-shadow: 0 0 20px 5px rgba(0, 0, 0, .2);
      margin: 5px 5px 35px 5px;
    }
    
    .ovm-oup_tooltip {
      position: relative;
      display: inline;
    }
    
    .ovm-oup_tooltip span {
      position: absolute;
      width: auto;
      padding: 3px 15px;
      color: #FFFFFF;
      background: #000000;
      height: 30px;
      line-height: 30px;
      text-align: center;
      visibility: hidden;
      border-radius: 4px;
    }
    
    .ovm-oup_tooltip span:after {
      content: '';
      position: absolute;
      bottom: 100%;
      left: 50%;
      margin-left: -8px;
      width: 0;
      height: 0;
      border-bottom: 8px solid #000000;
      border-right: 8px solid transparent;
      border-left: 8px solid transparent;
    }
    
    .ovm-oup_tooltip:hover span {
      visibility: visible;
      margin: 0;
      /* Remove 
      top: 0%;
      */
      left: 50%;
      z-index: 999;
    }
    <a class="ovm-oup_tooltip" href="#">
      <div id="ovm-oup_under_item_1" class="ovm-oup_under_item">
        <span>Text</span>
        <img src="Example_Logo.svg" alt="">
      </div>
    </a>

    The idea here is to use absolute placement of the tooltip (ie the span), to position the top-left corner of that span at the horizontal center, and bottom edge of the parent div. The transform rule is then used to offset the tooltip span relative to it's own dimensions, to achieve the final center/baseline placement.

    You can merge the additions above with your existing styling - I extracted the changes to the top of the stylesheet to clarify the additons that were made to achieve the desired result. Hope that helps!