Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2020.
The commitStyles() method of the Web Animations API's Animation interface writes the computed values of the animation's current styles into its target element's style attribute.
It is primarily used to write the styles for the final state of an animation into the target element, so that the styling persists after the animation ends.
None.
None (undefined).
The commitStyles() method is primarily used to write the computed values for the final state of an animation into the target element's style attribute, so that the styling persists after the animation ends. This can be done when the animation has finished (that is, the Animation object's finished property has resolved).
On older browsers, you must specify the fill mode in order to be able to commit the styles to the element after the animation has finished.
The code below shows how you can animate an element named animatedElement, setting fill: "forwards" to persist the animation styles after it finishes. Once the animation is finished we commit the styles to the element with commitStyles().
As fill persists the animation indefinitely, once we've committed the styles, we cancel the animation.
Note that the same effect could be achieved with fill alone, but using indefinitely filling animations is discouraged. Animations take precedence over all static styles, so an indefinite filling animation can prevent the target element from ever being styled normally.
Note: You might also avoid having to explicitly save the final state by setting them as the element initial styles and animating to the final styles.
In newer browsers you do not need to set the fill mode (see the browser compatibility table for specific versions).
Note: There is no way to feature check for this new behavior. For now most code should continue to set fill as shown in the previous section.
The code below shows how you can animate an element named animatedElement, wait on the animation to complete using the finished property, and then commit the styles to the element with commitStyles(). Because we're not setting fill we don't have to cancel the animation afterwards.
commitStyles() works even if the animation has been automatically removed. After the element's styles have been committed they can be modified and replaced as normal.
This example demonstrates how you can use commitStyles() to save the computed styles at the end of the animation, both with and without using fill. It also provides an example of what happens if neither commitStyles() or fill are used, for comparison.
The example first displays two buttons labelled "commitStyles() only" and "commitStyles() with fill". Both buttons animate when you click them, and both buttons call commitStyles() to persist the final state of the animation. The difference is that "commitStyles() only" does not specify fill: "forwards" to persist the animation's final state. On browsers that don't match the current specification the final state may not be captured.
The code then displays a button "No commitStyles() or fill" that can be used for comparison, and a "Reset" button.
This code defines a click handler for the "commitStyles() only" button. This animates the button to move right or left when it is clicked. Note that commitStyles() is called immediately after the animation is finished.
This code defines a click handler for the "commitStyles() with fill" button. This also animates the button to move right or left when it is clicked. As it defines a fill it needs to cancel the animation afterwards.
Note that commitStyles() is called immediately after the animation is finished.
This code defines a click handler for the "No commitStyles() or fill" button. This also animates the button to move right or left when it is clicked. It doesn't define a fill and we don't cancel the animation.
Click the buttons to animate them. Note that the first button will "jump" at the end of the animation if the current browser still requires fill for styles to be committed after the end of the animation. The "No commitStyles() or fill" button always jumps at the end, because the final state is not saved.
| Web Animations # dom-animation-commitstyles |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Nov 7, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.