I want to create similar effect as shown on the gif below. It should support image and image overlay of different dimensions.
I have successfully created beforementioned effect, however the image overlay did not cover the container when it had different dimension from the image.
.container {
display: inline-block;
position: relative;
width: 150px;
height: 150px;
}
.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;
}
.container:hover .overlay {
opacity: 1;
}
.overlay img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="container">
<img src="https://cozytux.com/wp-content/uploads/2014/07/placehold.it-500x750.gif" alt="Avatar" class="image">
<div class="overlay">
<img src="https://i.pinimg.com/originals/03/8b/b8/038bb804bd1611a46e4b1bafc4c6bca3.jpg">
</div>
</div>
<div class="container">
<img src="https://www.kingsporttn.gov/wp-content/uploads/2021/09/500x500.png" alt="Avatar" class="image">
<div class="overlay">
<img src="https://driverslicenserestorers.com/wp-content/uploads/placeholder-500x500.gif">
</div>
</div>
2
Answers
If both image have the same size, and the size known, you may use a single img tag and the box-sizing propertie. This option will aloow you to use both image in your tag. The one to show at first in the src attribute and the other one as a background.
Below a snippet to show the idea, hover the image
You can even add a transition and play with the padding value to decide from which side to hide/show the other image.
here is a few more examples:
Here is an example using CSS var() in case that each duo of image are of different sizes. Css var() will avoid to rewrite a rule for each case:
example below:
NOTE if both images are of different sizes or ratio, you have background-size and object-fit to resize or clamp them. Give us some examples of pairs of different sizes/ratio to show you some ways/ideas.
You need to fit the image. I’ll recommend to have same size for both images.