Animation: commitStyles() method

Baseline Widely available

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.

Syntax

js
commitStyles()

Parameters

None.

Return value

None (undefined).

Description

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).

commitStyles() alongside fill mode

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().

js
// Start the animation
const animation = animatedElement.animate(
  { transform: "translate(100px)" },
  { duration: 500, fill: "forwards" },
);

// Wait for the animation to finish
await animation.finished;
// Commit animation state to he animatedElement style attribute
animation.commitStyles();
// Cancel the animation
animation.cancel();

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.

commitStyles() without setting fill mode

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.

js
// Start the animation
const animation = animatedElement.animate(
  { transform: "translate(100px)" },
  { duration: 500 },
);

// Wait for the animation to finish
await animation.finished;

// Commit animation state to the animatedElement style attribute
animation.commitStyles();

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.

Examples

Animation with and without using fill

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.

HTML

html
<button class="commit-styles">commitStyles() only</button>
<button class="commit-with-fill">commitStyles() with fill</button>
<button class="no-commit-or-fill">No commitStyles() or fill</button>

JavaScript

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.

js
let offset1 = 0;

const commitStyles = document.querySelector(".commit-styles");

commitStyles.addEventListener("click", async (event) => {
  // Start the animation
  offset1 = 100 - offset1;
  const animation = commitStyles.animate(
    { transform: `translate(${offset1}px)` },
    { duration: 500 },
  );

  // Wait for the animation to finish
  await animation.finished;
  // Commit animation state to style attribute
  animation.commitStyles();
});

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.

js
const commitStylesWithFill = document.querySelector(".commit-with-fill");
let offset2 = 0;

commitStylesWithFill.addEventListener("click", async (event) => {
  // Start the animation
  offset2 = 100 - offset2;
  const animation = commitStylesWithFill.animate(
    { transform: `translate(${offset2}px)` },
    { duration: 500, fill: "forwards" },
  );

  // Wait for the animation to finish
  await animation.finished;
  // Commit animation state to style attribute
  animation.commitStyles();
  // Cancel the animation
  animation.cancel();
});

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.

js
const noCommitStylesOrFill = document.querySelector(".no-commit-or-fill");
let offset3 = 0;

noCommitStylesOrFill.addEventListener("click", async (event) => {
  // Start the animation
  offset3 = 100 - offset3;
  const animation = noCommitStylesOrFill.animate(
    { transform: `translate(${offset3}px)` },
    { duration: 500 },
  );
});

Result

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.

Specifications

Specification
Web Animations
# dom-animation-commitstyles

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
commitStyles
Chrome – Full support
Chrome 84 (Release date: 2020-07-27)
footnote Full support
Edge – Full support
Edge 84 (Release date: 2020-07-16)
footnote Full support
Firefox – Full support
Firefox 75 (Release date: 2020-04-07)
footnote Full support
Opera – Full support
Opera 70 (Release date: 2020-07-27)
footnote Full support
Safari – Full support
Safari 13.1 (Release date: 2020-03-24)
footnote Full support
Chrome Android – Full support
Chrome Android 84 (Release date: 2020-07-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 79 (Release date: 2020-07-28)
footnote Full support
Opera Android – Full support
Opera Android 60 (Release date: 2020-09-23)
footnote Full support
Safari on iOS – Full support
Safari on iOS 13.4 (Release date: 2020-03-24)
footnote Full support
Samsung Internet – Full support
Samsung Internet 14 (Release date: 2021-04-17)
footnote Full support
WebView Android – Full support
WebView Android 84 (Release date: 2020-07-27)
footnote Full support
WebView on iOS – Full support
WebView on iOS 13.4 (Release date: 2020-03-24)
footnote Full support
commitStyles() automatically fills values
Experimental
Chrome – No support
Chrome
footnote No support
Edge – No support
Edge
footnote No support
Firefox – Full support
Firefox 142 (Release date: 2025-08-19)
footnote Full support
Opera – No support
Opera
footnote No support
Safari – No support
Safari
footnote No support
Chrome Android – No support
Chrome Android
footnote No support
Firefox for Android – Full support
Firefox for Android 142 (Release date: 2025-08-19)
footnote Full support
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.

See also