我遇到这个问题,我将鼠标悬停在图像上以显示另一个图像,但是第一个图像仍然出现,而新图像不会改变高度和宽度,而是与另一个图像重叠。我仍然是HTML / CSS的新手所以我可能错过了一些简单的东西。这里是示例代码:
<img src="LibraryTransparent.png" id="Library" />
<style>
#Library {
height: 70px;
width: 120px;
}
#Library:hover {
background-image: url('LibraryHoverTrans.png');
height: 70px;
width: 120px;
}
</style>
答案 0 :(得分:0)
不知道这是否太多,但这有效:
HTML:
<div class="container">
<img src="https://placeimg.com/600/400/animals" alt="dog" />
<img src="https://placeimg.com/600/400/people" alt="people" />
</div>
CSS:
.container {
position: relative;
width: 600px;
height: 400px;
margin-left: 50px;
}
.container:hover img:nth-of-type(2) {
opacity: 1;
}
.container img {
position: absolute;
top: 0;
left: 0;
width: 500px;
height: 300px;
transition: opacity 1s ease-in-out 0s;
}
.container img:nth-of-type(1) {
opacity: 1;
}
.container img:nth-of-type(2) {
opacity: 0;
}