【发布时间】:2020-03-13 10:32:04
【问题描述】:
我已经制作了一个响应式图像网格,并正在尝试为其添加悬停效果,以便图像获得深色叠加层并且一些文本淡入其中。但是,我一直很难实现它。
这是我的 HTML 结构。
<div class="tile">
<img src="some_image" class="animate">
<div class="overlay">
<p>Mahatma Gandhi</p>
</div>
这是我的 CSS
.gallery .row .tile:hover ~ .tile img {
transform: scale(1.2);
}
但是,将鼠标悬停在图像上时,它没有预期的行为。
怎么了?
编辑 我得到了悬停效果,现在我可以淡入文本了。
这是我的代码:
<div class="tile">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Tagore_Gandhi.jpg/220px-Tagore_Gandhi.jpg" class="animate">
<div class="overlay">
<div class="text">Mahatma Gandhi</div>
</div>
</div>
CSS
.tile {
position: relative;
width: 100%;
}
.image {
display: block;
width: 100%;
height: auto;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
background-color: rgba(0, 0, 0, 0.8);
}
.tile:hover .overlay {
opacity: 1;
}
这似乎可行,但我认为它没有某种“感觉”。所以我需要为图像添加比例效果。我该怎么做呢
【问题讨论】:
-
是的,它有点工作,但整个图像按比例放大,我希望图像包含在某种容器 div 中,我不知道该怎么做。
标签: html css image hover overlay