Search code examples
htmlcsscss-animationscss-gradients

CSS looping gradient Transition


I got a question please I want to create a CSS looping gradient animation but when I do it the animation is very static it's not animating smoothly below is an example what I'm currently working on. Hope you can help me please.

.slider1 {
  background: background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(109, 0, 255)) !important;
}

/* css animation not smooth */
.slider1 {
  opacity: .5;
  animation: myfirst 5s;
  -moz-animation: myfirst 5s infinite; /* Firefox */
  -webkit-animation: myfirst 5s infinite; /* Safari and Chrome */
}

@-moz-keyframes myfirst { /* Firefox */ 
  0% {
    background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(0, 174, 255))
  }
  50% {
    background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(135, 255, 0))
  }
  100% {
    background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(109, 0, 255))
  }
}

@-webkit-keyframes myfirst { /* Safari and Chrome */
  0% {
    background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(0, 174, 255))
  }
  50% {
    background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(135, 255, 0))
  }
  100% {
    background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(109, 0, 255))
  }
}
<div class="slider1">
  This some content
  <div class="wsite-spacer" style="height:600px;"></div>
</div>


Solution

  • basically your gradint has a fixed color on top, and a varying color in the bottom.

    If you construct this gradint as 2 different gradint overlayed, then you can move the one on the bottom and create the changes in color from the changes in position

    div {
        width: 400px;
        height: 300px;
        background-image: 
    linear-gradient(to top, transparent, red),    
    linear-gradient(to right, green, yellow, blue);
        background-size: 100% 100%, 2000% 100%;
        animation: move 5s infinite;
    }
    
    @keyframes move {
     from {background-position: center center, left center;}
     to {background-position: center center, right center;}
    }
    <div></div>