Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating a CSS gradient background on the hover event

Tags:

html

css

I have some menu items that are styled using a background gradient on hover using the following styling:

#sidebar ul li a:hover {
    background-image: linear-gradient(bottom, rgb(68,68,68) 5%, rgb(51,51,51) 100%);
    background-image: -o-linear-gradient(bottom, rgb(68,68,68) 5%, rgb(51,51,51) 100%);
    background-image: -moz-linear-gradient(bottom, rgb(68,68,68) 5%, rgb(51,51,51) 100%);
    background-image: -webkit-linear-gradient(bottom, rgb(68,68,68) 5%, rgb(51,51,51) 100%);
    background-image: -ms-linear-gradient(bottom, rgb(68,68,68) 5%, rgb(51,51,51) 100%);

    background-image: -webkit-gradient(
        linear,
        left bottom,
        left top,
        color-stop(0.05, rgb(68,68,68)),
        color-stop(1, rgb(51,51,51))
    );
    color: #f0f0f0;
    border-top-left-radius: 4px;
    border-bottom-left-radius: 4px;
}

My question is, is it possible to make the new background (defined by gradients) slide in from the right using CSS3 transitions or animations? Or will I have to resort to using a sprite image or Javascript?

like image 228
James Dawson Avatar asked Jun 10 '26 00:06

James Dawson


1 Answers

Animation on gradients aren't supported yet. However this site provides a pleasing approach for a animated kind of feel on hover-

http://sapphion.com/2011/10/css3-gradient-transition-with-background-position/

Sample css:-

#DemoGradient{  
    background: -webkit-linear-gradient(#C7D3DC,#5B798E);  
    background: -moz-linear-gradient(#C7D3DC,#5B798E);  
    background: -o-linear-gradient(#C7D3DC,#5B798E);  
    background: linear-gradient(#C7D3DC,#5B798E);  

    -webkit-transition: background 1s ease-out;  
    -moz-transition: background 1s ease-out;  
    -o-transition: background 1s ease-out;  
    transition: background 1s ease-out;  

    background-size:1px 200px;  
    border-radius: 10px;  
    border: 1px solid #839DB0;  
    cursor:pointer;  
    width: 150px;  
    height: 100px;  
}  
#DemoGradient:Hover{  
    background-position:100px;  
} 
like image 67
Cdeez Avatar answered Jun 14 '26 20:06

Cdeez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!