I'm trying to implement a css effect where once the mouse hovers over an image, another image will appear in its place.
An example of this effect:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Change Image on Hover in CSS</title>
<style>
.card {
width: 130px;
height: 195px;
position: relative;
display: inline-block;
margin: 50px;
}
.card .img-top {
display: none;
position: absolute;
top: 0;
left: 0;
z-index: 99;
}
.card:hover .img-top {
display: inline;
}
</style>
</head>
<body>
<div class="card">
<img src="https://lh3.googleusercontent.com/proxy/yPBxt1JeUGqXWcCd9IRpOLIYSKWFLteV_m-ATsbTdYmpWM2iUIc36U6LfRP1tL31iAgC4YrwyUkqGwwJv0Jjy6lHPFC3y0Y" alt="Card Back">
<img src="https://i.pinimg.com/originals/23/48/8a/23488aa42a19433d34b72a5ee4ae5f14.jpg" class="img-top" alt="Card Front">
</div>
</body>
</html>
And the following is my own code:
<!DOCTYPE html>
<html>
<head>
<style>
.mid-one-container {
position: relative;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-evenly;
align-items: baseline;
align-content: center;
display: inline-block;
}
.mid-one-items {
order: 3;
flex-basis: 0;
}
.mid-one-items img {
width: 300px;
}
.mid-one-items .img-top {
display: none;
position: absolute;
left: 0;
top: 0;
z-index: 50;
}
.mid-one-items:hover .img-top {
display: inline;
}
</style>
</head>
<body>
<div class="mid-one-container">
<div class="mid-one-items">
<img src="https://static.nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5/egamzcy0qviwbrbhbfp3/fc-barcelona-2019-20-vapor-match-home-football-shirt-d16q4Q.jpg"
alt="front">
<img src="https://static.nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5,q_80/dzsv4wxmgirdghtx8nln/fc-barcelona-2019-20-vapor-match-home-football-shirt-d16q4Q.jpg"
class="img-top" alt="back">
<img src="https://c.static-nike.com/a/images/t_PDP_1280_v1/f_auto/m65penejikj8c00svcmo/stephen-curry-earned-city-edition-swingman-golden-state-warriors-mens-nba-connected-jersey-4Vr7FK.jpg"
alt="front">
<img src="https://c.static-nike.com/a/images/t_PDP_1280_v1/f_auto/fm4aihaj6diuyf1htuot/stephen-curry-earned-city-edition-swingman-golden-state-warriors-mens-nba-connected-jersey-4Vr7FK.jpg"
class="img-top" alt="back">
</div>
</div>
</body>
When I try to implement this feature in my flex box container, I get a couple unintended errors:
When I hover over the image on the left (blue and red jersey), the image transforms to the incorrect image (i.e. the image of the man in the yellow jersey)
when I hover over the man in the yellow jersey, it changes the image of the man in the blue and red jersey.
I suspect it has something to do with these two styles:
.mid-one-items .img-top {
display: none;
position: absolute;
left: 0;
top: 0;
z-index: 50;
}
.mid-one-items:hover .img-top {
display: inline;
}
Any help with some tips on having the correct image appear on hover as well as keeping the images in the same row would be greatly appreciated.