ServerSideSkittles
ServerSideSkittles

Reputation: 2973

Animate CSS Gradient in Bootstrap nav

I am working with bootstrap atm and notice my simple css nav button transition wasn't working.

I put this down to bootstrap but then removed my gradient background and just had a solid background color and it worked.

Does a spec for CSS gradient transitions, does this exist? Is there any way I can do this? If not, what is the best solution?.

Below is my fiddle of with the gradient being the orange hover.

http://jsfiddle.net/MgcDU/10245/

The gradient is as follows, so it works somewhat in multiple browsers.

    background: -webkit-gradient(linear, 0 0, 0 100%, from(rgba(254, 204, 177, 1)), color-stop(0.5, rgba(241, 116, 50, 1)), color-stop(0.51, rgba(234, 85, 7, 1)), to(rgba(251, 149, 94, 1)));
    background: -webkit-linear-gradient(rgba(254, 204, 177, 1) 0%, rgba(241, 116, 50, 1) 50%, rgba(234, 85, 7, 1) 51%, rgba(251, 149, 94, 1) 100%);
    background: -moz-linear-gradient(rgba(254, 204, 177, 1) 0%, rgba(241, 116, 50, 1) 50%, rgba(234, 85, 7, 1) 51%, rgba(251, 149, 94, 1) 100%);
    background: -o-linear-gradient(rgba(254, 204, 177, 1) 0%, rgba(241, 116, 50, 1) 50%, rgba(234, 85, 7, 1) 51%, rgba(251, 149, 94, 1) 100%);
    background: linear-gradient(rgba(254, 204, 177, 1) 0%, rgba(241, 116, 50, 1) 50%, rgba(234, 85, 7, 1) 51%, rgba(251, 149, 94, 1) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#feccb1', endColorstr='#fb955e', GradientType=0);

Upvotes: 3

Views: 844

Answers (1)

Alexander O'Mara
Alexander O'Mara

Reputation: 60577

The reason linear-gradient fails where a solid color works is because linear-gradient actually creates an image. The longhand property it corresponds to is background-image, not background-color.

A background-image is not transitional, however using positioning and a pseudo-element, we can use the opacity property to emulate it. Here is a simple example that shows off this technique.

Example:

li {
    display: inline-block;
    width: 200px;
    height: 200px;
    background: black;
    text-align: center;
    line-height: 200px;
    font-size: 40px;
    /*Must have positioning.*/
    position: relative;
}
li a {
    color: white;
    /*Must have positoning.*/
    position: relative;
}
li:before {
    /*Make it fill the container.*/
    content: "";
    display: block;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    /*Create gradient.*/
    background: rgb(254, 204, 177);
    background: -webkit-gradient(linear, 0 0, 0 100%, from(rgba(254, 204, 177, 1)), color-stop(0.5, rgba(241, 116, 50, 1)), color-stop(0.51, rgba(234, 85, 7, 1)), to(rgba(251, 149, 94, 1)));
    background: -webkit-linear-gradient(rgba(254, 204, 177, 1) 0%, rgba(241, 116, 50, 1) 50%, rgba(234, 85, 7, 1) 51%, rgba(251, 149, 94, 1) 100%);
    background: -moz-linear-gradient(rgba(254, 204, 177, 1) 0%, rgba(241, 116, 50, 1) 50%, rgba(234, 85, 7, 1) 51%, rgba(251, 149, 94, 1) 100%);
    background: -o-linear-gradient(rgba(254, 204, 177, 1) 0%, rgba(241, 116, 50, 1) 50%, rgba(234, 85, 7, 1) 51%, rgba(251, 149, 94, 1) 100%);
    background: linear-gradient(rgba(254, 204, 177, 1) 0%, rgba(241, 116, 50, 1) 50%, rgba(234, 85, 7, 1) 51%, rgba(251, 149, 94, 1) 100%);
    /*Transition the opacity.*/
    opacity: 0;
    -webkit-transition: opacity 0.5s ease;
    -moz-transition: opacity 0.5s ease;
    -o-transition: opacity 0.5s ease;
    transition: opacity 0.5s ease;
}

li:hover:before {
    opacity: 1;
}
<li>
    <a href="#">test</a>
</li>

Upvotes: 4

Related Questions