如何使工具提示居中显示在一个div下方?

4

如何在不使用JavaScript的情况下将工具提示居中并直接放在红圆圈下面?谢谢! :-)

这是我的问题的图像和应该标记的位置:

image

我目前的代码如下:

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;
}
1个回答

4
一种解决方案是将工具提示的绝对定位(相对于父级div)与移动变换结合使用,从而实现所需的位置。

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

这里的想法是使用工具提示(即)的绝对位置,将该的左上角定位于父
的水平中心和底部边缘。然后使用transform规则相对于其自身的尺寸来偏移工具提示,以实现最终的居中/基线对齐。
您可以将上述添加内容与您现有的样式合并-我提取了样式表的顶部更改,以澄清为实现所需结果所做的添加。希望这有所帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接