How to keep the final state of an animated element while the rest of the animation completes in CSS
Loading
How to keep the final state of an animated element while the rest of the animation completes in CSS
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Abhishek YadavPosted Nov 1, 2022, 1:08 PM
Deepak RawatPosted May 13, 2023, 7:19 PM
To keep the final state of an animated element while the rest of the animation completes in CSS, you can use the
animation-fill-modeproperty. By setting it toforwards, the element will retain the styles applied during the last keyframe of the animationthe
.boxelement will start with an opacity of 0 and move 200 pixels to the right over a duration of 2 seconds. Theanimation-fill-mode: forwards;ensures that once the animation completes, the element will retain the styles defined in the last keyframe (100% state). In this case, the final state is an opacity of 1 and a translation of 200 pixels to the right.By default,
animation-fill-modeis set tonone, which means the element will revert to its original state after the animation completes. Setting it toforwardskeeps the final state.Note that
animation-fill-modeonly affects the element once the animation finishes. If you want to keep the final state of an element while the rest of the animation is still in progress, you may need to consider using JavaScript or a JavaScript animation library to manually apply the styles at a specific point in the animation.Deepak RawatPosted Apr 25, 2023, 7:08 AM