html - 如何在图像悬停时显示 div?

标签 html css

我有一些这样的代码....

<div class="col-lg-8 col-xs-12 col-sm-7 col-md-8 "  style="vertical-align:top;">
  <img src="images/url.jpg" class="banner_img" />
  <div class="banner_caption">
    <h3>Banner caption here</h3>
  </div>
</div>

和像这样的 css...

.banner_caption{
    background-color:rgba(0,0,0,0.3);
    width:100%;
    padding:5px;
    position:absolute;
    bottom:-60px;
    display:none;
    left:0;
    color:#fff;
    transition-delay:1s;
    transition:all 1s;
}

.banner_img:hover .banner_caption{
    bottom:0px;
    display:block;
}

当我将鼠标悬停在图像上时,banner_caption div 应该会出现... 但我做不到……有人有解决这个问题的想法吗?

最佳答案

只要给这个:

.banner_img:hover + .banner_caption

加号 (+) 称为兄弟选择器。

.banner_img:hover + .banner_caption{
    bottom: 0;
    display: block;
}

你的不起作用的原因是:

  • .banner_img:hover .banner_caption:.banner_caption 位于 .banner_img 内。
  • .banner_img:hover + .banner_caption:.banner_caption.banner_img 相邻。

所以你需要使用上面的第二个。

关于html - 如何在图像悬停时显示 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32871600/

相关文章:

javascript - css 中文本的溢出隐藏

iphone - 如何在 iPad 和 iPhone 之间使用不同的 css 样式

html - 如何将一张图片对齐到另一张图片的底部?

css - 即 :11 hover on before not working

javascript - 存储目标容器的 HTML 和 CSS

javascript - 去除文本区域的发光、边框、垂直/水平滚动条

javascript - 自定义社交分享无法在 Windows 中打开?

html - 如何将 css 应用于不在禁用父级内部的 img?

Javascript:删除原型(prototype)继承中对 HTML 选择器的引用?

html - 如何在 flexbox 中跨行均匀分布元素?