user6224087
user6224087

Reputation:

CSS transition on background image change of DIV

I tried to apply transition property to give an effect when image changes on hover but it seems to not work. Please have a look and help me out.

.ow-outer {
    background-color: #fff;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    border: 1px solid #fff;
    text-align: center;
    padding: 20px;
    background-image: url(../images/team-canada-light.png);
    background-size: 120px;
    background-repeat: no-repeat;
    background-position: center;
    transition: all 0.3s ease-in-out;
}
.ow-outer:hover {
    background-image: url(../images/team-canada.png);
}

Upvotes: 3

Views: 17495

Answers (4)

Phantom
Phantom

Reputation: 377

Use background css property instead of background-image property it will work...

    .ow-outer {
    background-color: #fff;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    border: 1px solid #fff;
    text-align: center;
    padding: 20px;
    background: url('https://www.hello.com/img_/hello_logo_hero.png');
    background-size: 120px;
    background-repeat: no-repeat;
    background-position: center;
    transition: all 5s ease-in-out;
}
.ow-outer:hover {
    background: url('https://images.chesscomfiles.com/uploads/v1/blog/287126.d6272c47.630x354o.36990d3fc701.png');
}
<div class="ow-outer"></div>

Upvotes: 0

Mary Oleksiuk
Mary Oleksiuk

Reputation: 105

Use opacity for transitions. It doesnt work with images

Upvotes: 1

Asons
Asons

Reputation: 87191

Transition on background-image doesn't work cross browser, so use a pseudo element instead

Using opacity

.ow-outer {
    position: relative;
    background-color: #fff;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    border: 1px solid #fff;
    text-align: center;
    padding: 20px;
    background: url(http://placehold.it/200)  no-repeat center;
    background-size: 120px;
}
.ow-outer::before {
    content: '';
    position: absolute;
    left: 0; top: 0; right:0; bottom: 0;
    background: url(http://placehold.it/200/f00) no-repeat center;
    background-size: inherit;
    opacity: 0;
    transition: opacity 0.3s ease-in-out;
}
.ow-outer:hover::before {
    opacity: 1;
}
<div class="ow-outer"></div>

Using transform: scale

.ow-outer {
    position: relative;
    background-color: #fff;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    border: 1px solid #fff;
    text-align: center;
    padding: 20px;
    background: url(http://placehold.it/200)  no-repeat center;
    background-size: 120px;
}
.ow-outer::before {
    content: '';
    position: absolute;
    left: 0; top: 0; right:0; bottom: 0;
    background: url(http://placehold.it/200/f00) no-repeat center;
    background-size: inherit;
    transform: scale(0);
    transition: transform 0.3s ease-in-out;
}
.ow-outer:hover::before {
    transform: scale(1);
}
<div class="ow-outer"></div>

Upvotes: 8

Sahil Dhir
Sahil Dhir

Reputation: 4250

Here is the solution. Used images from some example..

.bgImg {
  width: 500px;
  height: 500px;
  background-image: url(http://i.imgur.com/tmM8Bpy.jpg);
  -webkit-transition: background-image 0.5s linear;
  -moz-transition: background-image 0.5s linear;
  -ms-transition: background-image 0.5s linear;
  transition: background-image 0.5s linear;
}

.bgImg:hover {
  background-image: url(http://i.imgur.com/FWqOONj.jpg);
}
<div class="bgImg"></div>

Upvotes: 1

Related Questions