Html 隐藏容器溢出时显示工具提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39146047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display tooltip when container overflow is hidden
提问by Amir Gonnen
Is there a way, preferably without js, to position and display a tooltip above a container, when the container must have overflow:hidden
, without the tooltip be affected and clipped by the container?
有没有办法,最好没有 js,在容器上方定位和显示工具提示,当容器必须overflow:hidden
有时,工具提示不会受到容器的影响和剪切?
Here is an example to illustrate this problem:
下面是一个例子来说明这个问题:
.container {
overflow: hidden;
position: relative;
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
height: 70px;
background: lightblue;
text-align: center;
}
a.tooltips {
position: relative;
display: inline;
}
a.tooltips span {
position: absolute;
width: 140px;
color: #FFFFFF;
background: #000000;
height: 96px;
line-height: 96px;
text-align: center;
visibility: hidden;
border-radius: 8px;
box-shadow: 4px 3px 10px #800000;
}
a.tooltips 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;
}
a:hover.tooltips span {
visibility: visible;
opacity: 0.7;
top: 30px;
left: 50%;
margin-left: -76px;
z-index: 999;
}
<div class="container">
<a class="tooltips" href="#">Hover me for Tooltip
<span>Tooltip</span></a>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin porttitor elit neque, in condimentum ante pulvinar et. Donec et erat nulla. Vestibulum quis porta tellus. Curabitur non blandit metus. Vestibulum nec nisi quis urna tempor pharetra. Phasellus volutpat, arcu ac malesuada porttitor, erat diam facilisis ligula, eget aliquet nibh augue.
</div>
<div>
回答by FelipeAls
There's a way to display an element in these conditions, by having it absolutely positioned(as a simple wrapper) and containing a relatively positioned tooltip.
So you need to add an element.
One important condition: the parent with overflow: hidden
must not be positioned itself or the tooltip won't pop out/displayed above this parent.
有一种方法可以在这些条件下显示元素,方法是将元素绝对定位(作为简单的包装器)并包含相对定位的 tooltip。
所以你需要添加一个元素。
一个重要的条件:父级overflow: hidden
不能被定位,否则工具提示不会弹出/显示在这个父级之上。
- Codepen(I renamed your
.tooltips
class as.has-tooltip
and added 2 anothers) - My previous answer with a similar trick
- Codepen(我将您的
.tooltips
班级重命名为.has-tooltip
并添加了另外两个班级) - 我以前用类似的技巧回答
.container {
overflow: hidden;
/*position: relative;*/
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
height: 70px;
background: lightblue;
text-align: center;
}
.has-tooltip {
/*position: relative;*/
display: inline;
}
.tooltip-wrapper {
position: absolute;
visibility: hidden;
}
.has-tooltip:hover .tooltip-wrapper {
visibility: visible;
opacity: 0.7;
/*top: 30px;*/
/*left: 50%;*/
/*margin-left: -76px;*/
/* z-index: 999; defined above with value of 5 */
}
.tooltip {
display: block;
position: relative;
top: 2em;
right: 100%;
width: 140px;
height: 96px;
/*margin-left: -76px;*/
color: #FFFFFF;
background: #000000;
line-height: 96px;
text-align: center;
border-radius: 8px;
box-shadow: 4px 3px 10px #800000;
}
.tooltip: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;
}
<div class="container">
<a class="has-tooltip" href="#">Hover me for Tooltip
<span class="tooltip-wrapper"><span class="tooltip">Tooltip</span></span></a>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin porttitor elit neque, in condimentum ante pulvinar et. Donec et erat nulla. Vestibulum quis porta tellus. Curabitur non blandit metus. Vestibulum nec nisi quis urna tempor pharetra. Phasellus volutpat, arcu ac malesuada porttitor, erat diam facilisis ligula, eget aliquet nibh augue.
</div>
<div>
回答by Ganesh Putta
now i used position absolute to fixed for the tooltip, check it now
现在我使用绝对位置来固定工具提示,现在检查它
.container {
position: relative;
overflow:hidden;
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
height: 70px;
background: lightblue;
text-align: center;
z-index:9;
}
a.tooltips {
position: relative;
display: block;
}
a.tooltips span {
position: fixed;
width: 140px;
color: #FFFFFF;
background: #000000;
height: 96px;
line-height: 96px;
text-align: center;
visibility: hidden;
border-radius: 8px;
z-index:9999;
top:15px;
box-shadow: 4px 3px 10px #800000;
}
a.tooltips 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;
}
a:hover.tooltips span {
visibility: visible;
opacity: 0.7;
top: 40px;
left: 50%;
margin-left: -76px;
z-index: 999;
}
<div style="height:500px;">
<div class="container">
<a class="tooltips" href="#">Hover me for Tooltip
<span>Tooltip</span></a>
<div>
</div>