当容器必须具有overflow:hidden时,是否有一种方法(最好没有js)在容器上方放置并显示工具提示,而工具提示不会受到容器的影响和限制?

这是说明此问题的示例:

.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>

最佳答案

在这种情况下,有一种方法可以显示元素,方法是将其绝对放置在(作为简单包装程序)中,而包含相对定位的工具提示
因此,您需要添加一个元素。
一个重要条件:带有overflow: hidden的父级一定不能放置在自己的位置,否则工具提示不会弹出/显示在该父级上方。

  • Codepen(我将您的.tooltips类重命名为.has-tooltip,并添加了2个其他名称)
  • My previous answer with a similar trick


  •  .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>

    关于html - 隐藏容器溢出时显示工具提示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39146047/

    10-10 21:44