Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css transition with linear gradient [duplicate]

I'm trying to add a transition to a button I have that's background is made with css linear-gradient but it's not working.

This is the css for my button.

a.button { background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,@green), color-stop(100%,#a5c956)); -webkit-transition: background 5s linear; }  a.button:hover { -webkit-gradient(linear, left top, left bottom, color-stop(0%,@greenhover), color-stop(100%,#89af37)) } 

If you're wondering about @green and @greenhover, I'm using .less to make my css.

Anything wrong with this? Any ideas?

like image 506
Johan Alkstål Avatar asked Sep 09 '11 14:09

Johan Alkstål


People also ask

How do you repeat a linear gradient in CSS?

CSS Syntax Default is 180deg. Defines the position of the starting-point of the gradient line. It consists of two keywords: the first one indicates the horizontal side, left or right, and the second one the vertical side, top or bottom. The order is not relevant and each of the keyword is optional.

Can CSS transition linear gradient?

In CSS, you can smoothly make transitions between two or more colors. CSS has two types of gradients: Linear gradient: It goes down/up/left/right/diagonally and makes smooth transitions of colors. To make a linear transition you first have to choose two color stops.

How do you add a transition background gradient in CSS?

In CSS, you can't transition a background gradient. It jumps from one gradient to the other immediately, with no smooth transition between the two. He documents a clever tactic of positioning a pseudo element covering the element with a different background and transitioning the opacity of that pseudo element.


1 Answers

Sadly, you really can't transition gradients for now.

So, the only workable workaround is using an extra element with needed gradient and transition it's opacity:

a.button {    position: relative;    z-index: 9;    display: inline-block;    padding: 0 10px;    background: -webkit-gradient(linear, 0 0, 0 100%, from(green), to(#a5c956));    background: -webkit-linear-gradient(top, green, #a5c956);    background: -moz-linear-gradient(top, green, #a5c956);    background: -o-linear-gradient(top, green, #a5c956);    background: linear-gradient(top, green, #a5c956);  }    .button-helper {    position: absolute;    z-index: -1;    top: 0;    left: 0;    width: 100%;    height: 100%;    opacity: 0;    background: -webkit-gradient(linear, 0 0, 0 100%, from(lime), to(#89af37));    background: -webkit-linear-gradient(top, lime, #89af37);    background: -moz-linear-gradient(top, lime, #89af37);    background: -o-linear-gradient(top, lime, #89af37);    background: linear-gradient(top, lime, #89af37);    -webkit-transition: opacity 1s linear;    -moz-transition: opacity 1s linear;    -o-transition: opacity 1s linear;    transition: opacity 1s linear;  }    a.button:hover .button-helper {    opacity: 1;  }
<a href="#" class="button"><span class="button-helper"></span>button</a>
like image 190
kizu Avatar answered Oct 16 '22 07:10

kizu