如何将工具提示置于 div 下方居中?

How to put a tooltip centered under a div?

如何在没有 Javascript 的情况下将工具提示居中放置在红色圆圈的正下方?先感谢您! :-)

这是我的问题的图像以及它应该在的标记位置:

我的代码目前是这样的:

HTML代码:

<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代码:

#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;
}

一种解决方案是在工具提示上组合 absolute placement of the tooltip (relative to the parent div), with a translation transformation,这将实现所需的位置:

/* 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>

这里的想法是使用工具提示的 absolute 放置(即 span),将该跨度的 top-left 角定位在水平中心和底部边缘parent div。 transform 规则然后用于偏移工具提示 span 相对于它自己的尺寸,以实现最终的 center/baseline 放置。

您可以将上面添加的内容与您现有的样式合并 - 我提取了样式表顶部的更改,以阐明为实现所需结果而添加的内容。希望对您有所帮助!