0

I am using the following code to change the background color of an element using css animations. Is there a way to change this in order to make it loop continuously?

    #circle {
      animation: colorchange 10s;
      -webkit-animation: colorchange 10s;
    }

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

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

3 Answers 3

2

You must use:

animation-iteration-count: infinite;
Sign up to request clarification or add additional context in comments.

Comments

2

If you take a look at the animation documentation you can indicate the number of iteration.

With the property animation-iteration-count or in the animation definition.

#circle {
  width:100px;
  height:100px;
  animation: colorchange 10s infinite;
  -webkit-animation: colorchange 10s infinite;
}

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

@-webkit-keyframes colorchange
{
  0%   {background: red;}
  25%  {background: yellow;}
  50%  {background: blue;}
  75%  {background: green;}
  100% {background: red;}
}
<div id="circle"></div>

Comments

1

You should add animation-iteration-count: infinite; to your #circle div's css block as following:

#circle {
    animation: colorchange 10s;
    -webkit-animation: colorchange 10s;
    animation-iteration-count: infinite;
    -webkit-animation-iteration-count: infinite;
}

The animation-iteration-count property specifies the number of times an animation should be played.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.