gpt4 book ai didi

javascript - 如何将工具提示放在 div 下居中?

转载 作者:行者123 更新时间:2023-12-04 15:34:22 25 4
gpt4 key购买 nike

如何在没有 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;
}

最佳答案

一种解决方案是结合 absolute placement工具提示的(相对于父 div),带有 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 ),以将该跨度的左上 Angular 定位在水平中心和父 div 的底部边缘。 transform然后使用规则来偏移工具提示 span相对于它自己的尺寸,以实现最终的中心/基线放置。

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

关于javascript - 如何将工具提示放在 div 下居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60243416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com