transition background images with css2

it’s currently not possible to transition background images or css3 gradients and here I offer a way to do it simple by pure css ( without jquery )

you should have 2images  once for normal view and other for hover view

first you need to make a “container” for the <img>, it will contain normal and hover states at the same time:

<div class="images-container">
    <img class="normal" src="http://lorempixel.com/400/200/animals/9/">
    <img class="hover" src="http://lorempixel.com/400/200/animals/10/">
</div>

 

and pure css trick is :

div{
    position: relative;
    width: 400px;
    height: 200px;
}
div > img{
    display: block;
    width: 400px;
    height: 200px;
    position: absolute;
    /* transitions */
    -webkit-transition: all .4s ease;
       -moz-transition: all .4s ease;
        -ms-transition: all .4s ease;
         -o-transition: all .4s ease;
            transition: all .4s ease;

}
/* solution 2: css2 solution, hide/show elements by class */

div > img.normal{ 
    filter: alpha(opacity=100); /* IE stuff */
    opacity: 1;
    z-index: 2;
}
div > img.hover{
    filter: alpha(opacity=0); /* IE stuff */
    opacity: 0;
    z-index: 1;
}
/* hover */
div:hover > img.normal{ 
    filter: alpha(opacity=0);
    opacity: 0;
    z-index: 1;
}
div:hover > img.hover{ 
    filter: alpha(opacity=100);
    opacity: 1;
    z-index: 2;
}

css3 method is like this but without adding class ( normal and hover ) to your images  just  using n-th-child method here is article I told that how to use it !

have fun 😉

You May Also Like

About the Author: mohammad

Leave a Reply

Your email address will not be published. Required fields are marked *