Skip to content Skip to sidebar Skip to footer

Loop Div Background-color Through All Rainbow Colors Gradually | Css

How to change the background-color of a div over time through all color of a rainbow and then set it back again to its original color and loop that process infinitely ? The code be

Solution 1:

You can create css animation to change background color. To make animation loop you can add infinite and to get smooth transition of colors you can use linear

div {
  background-color: #FF0000;
  animation: bgColor 5s infinite linear;
  width: 200px;
  height: 100px;
}
@keyframes bgColor {
  12.5% {
    background-color: #FF0000;
  }
  25% {
    background-color: #FFA500;
  }
  37.5% {
    background-color: #FFFF00;
  }
  50% {
    background-color: #7FFF00;
  }
  62.5% {
    background-color: #00FFFF;
  }
  75% {
    background-color: #0000FF;
  }
  87.5% {
    background-color: #9932CC;
  }
  100% {
    background-color: #FF1493;
  }
}
<div></div>

Solution 2:

You need to use keyframes to animate css

#rainbow {
background-color: blue;  
  float:left;
  width: 70px;
  height:70px;
  border: 1px solid white;
  -webkit-animation-name: example; /* Chrome, Safari, Opera */
  -webkit-animation-duration: 4s; /* Chrome, Safari, Opera */animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: infinite;
}

@keyframes example {
    0%   {background-color: red; }
    25%  {background-color: yellow; }
    50%  {background-color: blue; }
    75%  {background-color: green; }
    100% {background-color: red; }
}

/* Chrome, Safari, Opera */@-webkit-keyframes example {
    0%   {background-color: red; }
    25%  {background-color: yellow; }
    50%  {background-color: blue; }
    75%  {background-color: green; }
    100% {background-color: red; }
}
<divid="rainbow"></div>

Solution 3:

you can achieve this with keyframes

.rainbow {
  float:left;
  width: 70px;
  height:70px;
  border: 1px solid white;
 
}

.rainbow:last-child { 
  float:left;
  border: 1px solid white;
  clear:both;
}
@keyframescolor {
  0% {
    background-color: red; 
  }
  10% {
    background-color: orange; 
  }
  20% {
    background-color: yellow;  
  }
  30% {
    background-color: Chartreuse;  
  }
  40% {
    background-color: cyan; 
  }
  50% {
    background-color: blue; 
  }
  60% {
    background-color: DarkOrchid;  
  }
  70% {
    background-color: DeepPink;
  }
  80% {
    background-color: red; 
  }
  90% {
    background-color: red; 
  }
  100% {
    background-color: red;
  }
}

.rainbow {
   animation: color 8s infinite;
}
<divclass="rainbow">Original color</div><divclass="rainbow">After some time</div><divclass="rainbow">After some time</div><divclass="rainbow">After some time</div><divclass="rainbow">After some time</div><divclass="rainbow">After some time</div><divclass="rainbow">After some time</div><divclass="rainbow">After some time</div><divclass="rainbow">Come back to red and loop</div><brstyle="clear:both">
All of the above in just one div

Solution 4:

I made this in pure Javascript. (Also on github)

let r = 255let g = 0let b = 0functionrgb(r, g, b){
    return"rgb("+r+","+g+","+b+")"
}

functionmyTimer () {
    if (r < 255 && g == 0 && b == 0) {
        r++
    } elseif (r == 255 && g < 255 && b == 0) {
        g++
    } elseif (r > 0 && g == 255 && b == 0) {
        r--
    } elseif (r == 0 && g == 255 && b < 255) {
        b++
    } elseif (r == 0 && g > 0 && b == 255) {
        g--
    } elseif (r < 255 && g == 0 && b == 255) {
        r++
    } elseif (r == 255 && g== 0 && b > 0) {
        b--
    }
    document.body.style.backgroundColor = rgb(r, g, b)
}

setInterval(myTimer, 10)

Post a Comment for "Loop Div Background-color Through All Rainbow Colors Gradually | Css"