悬停在图片上时尝试显示div

时间:2019-10-01 19:00:23

标签: html css hover

当我将鼠标悬停在图像上时,我试图使包含图像的某些信息的div出现,我的代码似乎不起作用。

HTML:

< div class="batmobile-info">This is Batman's batmobile, he uses it to chase his enemies or to flee from the police.< /div>
 < img class="batmobile" src="images/batmobile.png" alt="batmobile" >

CSS:

.batmobile{
position:relative;
width:500px;
height:200px;
top:350px;
left:200px;
}

.batmobile-info{
visibility:hidden;
}

.batmobile:hover .batmobile-info{
background-color:white;
text-align:center;
width:290px;
height:40px;
position:absolute;
top:500px;
left:700px;
visibility: visible;
}

3 个答案:

答案 0 :(得分:0)

如果您颠倒元素的顺序,那么imgdiv之前会为您提供:

<img class="batmobile" src="images/batmobile.png" alt="batmobile">
<div class="batmobile-info">This is Batman's batmobile, he uses it to chase his enemies or to flee from the police.</div>

然后,您可以使用以下CSS在悬停时显示div

.batmobile:hover + .batmobile-info {
  visibility: visible;
}

+adjacent sibling selector-超级有用的选择器!

答案 1 :(得分:0)

完成这项工作的一种方法是使用容器,在其中将imgdiv放入信息。

我使用opacity代替了visibility,因为opactity是您可以在过渡中使用的属性。

* { 
  box-sizing: border-box;
}
html, body {
  margin: 0;
}
/* contiainer to center my content, not relevant to question*/
.main-container {
  margin-top: 2rem;
  height: calc(100vh - 2rem);
  display: flex;
  align-items: center;
  justify-content: space-around;
}
/* image and info container */
.img-container {
  display: inline-block;
  position: relative;
  border: 2px solid #333;
}
/* style for div with information */
.img-info {
  position: absolute;
  background-color: rgba(0, 0, 0, 0.8);
  color: #eee;
  transition: opacity .3s ease-in-out;
  opacity: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: .5rem;
}
/* information positioning styles */
.img-info-overlay {
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
.img-info-above {
  left: -2px;
  top: -2rem;
  height: 2rem;
  width: calc(100% + 4px);
}
/* this part show information on hover */
.img-container:hover .img-info {
  opacity: 1;
}
<div class="main-container">
  <div class="img-container">
    <img src="https://dummyimage.com/250x100/fff/aaa" alt="Filler image">
    <div class="img-info img-info-overlay">Some info about this image</div>
  </div>
  <div class="img-container">
    <img src="https://dummyimage.com/250x100/fff/aaa" alt="Filler image">
    <div class="img-info img-info-above">Some info about this image</div>
  </div>
</div>

答案 2 :(得分:0)

问题出在HTML和CSS上,您需要将图像和文本放在一个父块中并从中开始样式化。我会避免绝对定位,并尝试使用opacity CSS property,以防您需要向文本块(changed)状态添加一些额外的过渡效果。签出代码并运行代码段:

.batmobile:hover
body {background:#ccc}

.batmobile {
  position: relative;
  width: 500px;
  height: 200px
}

.batmobile div p {
  visibility: hidden;
  background-color: white;
  width: 300px;
  height: 40px;
  margin: 0px auto; /* Think you wanted to center the block as width is less than parent */
  text-align: center /*In case you need to center text only */
}

.batmobile:hover div p {
  visibility: visible;
}

.batmobile img {width: 100%}