1

Hi I've been learning css for 2 months , so i met a problem with animation . I want my code to finish at lime , but animation finishes , and div again becomes red.

    @keyframes example{
        from{background-color: red}
        to{background-color: lime}
    }


    .animate{
        width:200px;
        height:100px;
        background-color: red;
        animation-name: example;
        animation-direction: alternate;
        animation-duration: 4s;
        animation-iteration-count: 1;
    }
M G G M
  • 21
  • 2

2 Answers2

1

You should've done this to your code .

    @keyframes example{
        from{background-color: red}
        to{background-color: lime}
    }


    .animate{
        width:200px;
        height:100px;
        background-color: red;
        animation-name: example;
        animation-direction: alternate;
        animation-duration: 4s;
        animation-iteration-count: 1;
        animation-fill-mode:forwards;
    }
Tiko
  • 1,241
  • 11
  • 19
1

Change the background-color from red to lime.

@keyframes example{
  from{background-color: red}
  to{background-color: lime}
}

.animate{
  width:200px;
  height:100px;
  background-color: lime;
  animation-name: example;
  animation-direction: alternate;
  animation-duration: 4s;
  animation-iteration-count: 1;
}
<div class="animate"></div>
James
  • 303
  • 2
  • 12