I got a div that changes its background to an image when a user hovers over it. The background is doing fine when the user hovers over it and the transform effect is applied smoothly, but when the user removes the mouse the image disappears suddenly.
I don't want it to happen and want the image to have a zoom out effect just like the Text inside the div and once the effect is finished the background changes to the default one and do not disappear suddenly without any effects.
How can I make it possible?
Here's my div:
html,
body {
margin: 0px;
padding: 0px;
}
.wrapper {
max-width: 1200px;
text-align: center;
margin-left: auto;
margin-right: auto;
}
.container {
width: 45%;
margin: 20px;
height: 300px;
border: 3px solid #eee;
overflow: hidden;
position: relative;
display: inline-block;
cursor: pointer;
}
.child {
height: 100%;
width: 100%;
background-size: cover;
background-repeat: no-repeat;
-webkit-transition: all .5s;
-moz-transition: all .5s;
-o-transition: all .5s;
transition: all .5s;
background-color: darkgray;
}
span {
display: block;
font-size: 35px;
color: #ffffff !important;
font-family: sans-serif;
text-align: center;
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
height: 50px;
cursor: pointer;
text-decoration: none;
}
.container:hover .child,
.container:focus .child {
background-image: url(https://images.freeimages.com/images/large-previews/de7/energy-1-1176465.jpg);
-ms-transform: scale(1.2);
-moz-transform: scale(1.2);
-webkit-transform: scale(1.2);
-o-transform: scale(1.2);
transform: scale(1.2);
}
.container:hover .child:before,
.container:focus .child:before {
display: block;
}
.child:before {
content: "";
display: none;
height: 100%;
width: 100%;
position: absolute;
top: 0;
left: 0;
background-color: rgba(52, 73, 94, 0.75);
}
<div class="wrapper">
<div class="container">
<div class="child">
<span>Text</span>
</div>
</div>
</div>