css - 防止图像与div重叠

时间:2017-04-20 19:26:04

标签: html css

我在页面上有一个图像,当用户将鼠标悬停在它上面时,我会显示带有一些信息的div。但是图像与div重叠。看起来像z-index问题,但我无法修复它 enter image description here

CSS:

.tip {
background-color: #ff6699;
background-repeat: no-repeat;
border-radius: 5px;
color: #ffffff;
left: 22px;
padding: 4px 10px;
position: absolute;
text-align: left;
top: -4px;
width: 250px;
display: none;
z-index: 3;
}


.search {
display: inline-block;
width: 17px;
height: 17px;
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAARCAMAAAAMs7fIAAAAV1BMVEX///9RSjxWT0HZ2NVbVEdYUUT8+/ttZ1vj4uCwraZhW07h4N19d23Gw7/X1dJ3cWaLhn3MysZmYFTz8/KfnJSDfnTp6Oe4ta+qp6CTj4Z6dWpeV0tybGGWN64GAAAAi0lEQVQYlWWPURKDIAxEQwCRRMGCVsXe/5wVxFrH/UnmJbuZAOxq52VmuNR2mJV+TG5IfvJBK3eAGNC7Bpq+UxQLWdDwaR5L89FrtfdkStWbPS8Y0dwJV/J07cltad5TTY6ESe7XedXidSw7hZSGLqAQStZEL/IXRl8IpB2tA6n+UB1kxA+0wB0R8RezpwVsoS4HoQAAAABJRU5ErkJggg==);
position: absolute;
cursor: pointer;
z-index: 3;
}

.search:hover .tip {
    display:block;
}

HTML:

<div >
 Hello <span class="search"><div class="tip">
 Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis
 </div></span>
 </div>
 <div >
 Hello AGain <span class="search"><div class="tip ">
 Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis
 </div></span>
  </div>

JS小提琴:https://jsfiddle.net/qub0ny5k/

2 个答案:

答案 0 :(得分:1)

您的class Button { void draw() { if (fill) drawRectangle(*fill); if (sprite) drawSprite(*sprite); if (font) drawText(name, *font); } std::optional<std::string> font; std::optional<std::string> sprite; std::optional<Color> fill; } class Button { void draw() { if (fill.alpha != 0) drawRectangle(fill); if (sprite != "") drawSprite(sprite); if (font != "") drawText(name, font); } std::string font; std::string sprite; Color fill; } 不需要.search 也不要将z-index包裹到div中。请改用span

&#13;
&#13;
div
&#13;
.search {
  display: inline-block;
  width: 17px;
  height: 17px;
  background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAARCAMAAAAMs7fIAAAAV1BMVEX///9RSjxWT0HZ2NVbVEdYUUT8+/ttZ1vj4uCwraZhW07h4N19d23Gw7/X1dJ3cWaLhn3MysZmYFTz8/KfnJSDfnTp6Oe4ta+qp6CTj4Z6dWpeV0tybGGWN64GAAAAi0lEQVQYlWWPURKDIAxEQwCRRMGCVsXe/5wVxFrH/UnmJbuZAOxq52VmuNR2mJV+TG5IfvJBK3eAGNC7Bpq+UxQLWdDwaR5L89FrtfdkStWbPS8Y0dwJV/J07cltad5TTY6ESe7XedXidSw7hZSGLqAQStZEL/IXRl8IpB2tA6n+UB1kxA+0wB0R8RezpwVsoS4HoQAAAABJRU5ErkJggg==);
  position: absolute;
  cursor: pointer;
  /* z-index: 3;   NOOOOOOOOOOOOOO */
}
.tip {
  background-color: #ff6699;
  background-repeat: no-repeat;
  border-radius: 5px;
  color: #ffffff;
  left: 22px;
  padding: 4px 10px;
  position: absolute;
  text-align: left;
  top: -4px;
  width: 250px;
  display: none;
  z-index: 3;
}
.search:hover .tip {
  display: block;
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

我给了.tip这个:

overflow:hidden;
z-index:2;

我删除了它z-index上的.search