Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I animate background image size on hover smoothly in css?

Tags:

html

css

I have a div with a background image. On hover, I want the image to zoom in slightly. I have it working (see fiddle) but I want it to animate smoothly over 0.3 seconds and then go back smoothly instead of jumping.

How do I do this?

https://jsfiddle.net/ufgnroor/2/

HTML

                <div class="portfolio-item">

                </div>

CSS

.portfolio-item {
height: 300px;
position: relative;
text-align: center;
cursor: pointer;
margin-bottom: 20px;
background-position: center;
background-size: cover;
background-image: url('http://media4.popsugar-assets.com/files/2014/08/08/878/n/1922507/caef16ec354ca23b_thumb_temp_cover_file32304521407524949.xxxlarge/i/Funny-Cat-GIFs.jpg');
}

.portfolio-item:hover {
background-size: 150%;
-webkit-transition: all 0.3s ease-in-out;
}
like image 345
user3589485 Avatar asked Sep 03 '25 09:09

user3589485


1 Answers

For your case you have to set the background-size & the transition before the hover:

.portfolio-item {
    background-size: 100%;
    transition: all 0.3s ease-in-out;
}

.portfolio-item:hover {
    background-size: 150%;
}

https://jsfiddle.net/gv5pjqok/1/

like image 66
Lynch Cai Avatar answered Sep 04 '25 22:09

Lynch Cai