如何为内部div

时间:2018-06-04 13:22:51

标签: css z-index

我正在创建一个表单,当用户将鼠标悬停在图标上时,会在该表单中添加关于该字段的图标。

以下是我的代码和css:

.tooltip {
  position: relative;
  display: inline-block;
  opacity: 1;
}

.tooltip .tooltiptext {
  visibility: hidden;
  width: 350px;
  background-color: #83837F;
  color: #fff;
  border-radius: 6px;
  padding: 5px 0;
  position: absolute;
  z-index: 1;
  top: 25px;
  padding: 5px;
}

.tooltip:hover .tooltiptext {
  visibility: visible;
}

.tooltiptext:before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  z-index: 100001;
  content: ' ';
  position: absolute;
  width: 0;
  height: 0;
  margin: 2px 0 0 8px;
  bottom: 100%;
  left: 0;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-bottom: 8px solid #83837F;
}
<div class="form-group">
  <div class="tooltip" style="float:right;">
    <img src="http://lorempixel.com/150/150/cats/1/">
    <span class="tooltiptext">
      Cusotm Text Cusotm Text Cusotm Text Cusotm Text Cusotm Text Cusotm Text Cusotm Text
    </span>
  </div>
</div>
<div class="form-group">
  <div class="tooltip" style="float:right;">
    <img src="http://lorempixel.com/150/150/cats/3/">
    <span class="tooltiptext">
      Cusotm Text Cusotm Text Cusotm Text Cusotm Text Cusotm Text Cusotm Text Cusotm Text
    </span>
  </div>
</div>

对于上面我首先悬停tooltip,然后其tooltiptext可见,但第二个tooltip img与其重叠。

我想将最高的z-index添加到tooltiptext,以便工具提示img位于其下方,不会与它重叠。

1 个答案:

答案 0 :(得分:2)

您好现在可以在.tooltip .tooltiptext中添加两个属性,例如left:10px; right:10px;,然后移至width: 350px;

如下所示是此代码的示例。

&#13;
&#13;
.tooltip {
  position: relative;
  display: inline-block;
  opacity: 1;
}

.tooltip .tooltiptext {
  visibility: hidden;
 
  background-color: #83837F;
  color: #fff;
  border-radius: 6px;
  padding: 5px 0;
  position: absolute;
  z-index: 1;
  top: 25px;
  padding: 5px;
left:10px;right:10px;
}

.tooltip:hover .tooltiptext {
  visibility: visible;
}

.tooltiptext:before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  z-index: 100001;
  content: ' ';
  position: absolute;
  width: 0;
  height: 0;
  margin: 2px 0 0 8px;
  bottom: 100%;
  left: 0;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-bottom: 8px solid #83837F;
}
&#13;
<div class="form-group">
  <div class="tooltip" style="float:right;">
    <img src="http://lorempixel.com/150/150/cats/1/">
    <span class="tooltiptext">
      Cusotm Text Cusotm Text Cusotm Text Cusotm Text Cusotm Text Cusotm Text Cusotm Text
    </span>
  </div>
</div>
<div class="form-group">
  <div class="tooltip" style="float:right;">
    <img src="http://lorempixel.com/150/150/cats/3/">
    <span class="tooltiptext">
      Cusotm Text Cusotm Text Cusotm Text Cusotm Text Cusotm Text Cusotm Text Cusotm Text
    </span>
  </div>
</div>
&#13;
&#13;
&#13;