我有一张图片,希望当鼠标悬停在图片上方时,会出现一个略带透明度的黑色背景覆盖整个图片,并且在图片顶部出现一个按钮。目前,我已经将按钮放在了预期位置,但是我无法让它在鼠标悬停时消失或出现。
<div class="container">
<img src="img_snow.jpg" alt="Snow">
<button class="btn">Button</button>
</div>
.container {
position: relative;
button {
position: absolute;
top: 60%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
button:hover {
background-color: black;
}
}
img {
background-image: url(${({ src }) => src});
background-repeat: no-repeat;
background-size: cover;
&:hover {
background-color: #000;
opacity: 0.5;
}
}