CSS - animation-composition Property



The CSS property animation-composition defines the composite operation to be applied when multiple animations act on the same property at the same time.

  • In CSS animations, properties targeted by @keyframes have associated effect stacks.

  • The value of the effect stack results from the combination of the underlying property value in the CSS style rule with the effect value of the property in keyframe.

  • The animation-composition property defines the method for combining these values for a specific property.

Possible Values

  • replace - The default value is the effect value, which takes precedence over the underlying value of the property.

  • add - The effect value increases the existing property value through an additive operation. For animation types where the add operation is not commutative, the order of operands is the base value followed by the effect value.

  • accumulate - The effect and the underlying values are merged. In animation types where the addition operation is not commutative, the order of operands is the base value followed by the effect value.

Syntax

animation-composition = <single-animation-composition>#  

<single-animation-composition> = replace | add | accumulate  

Applies To

All the HTML elements.

CSS animation-composition - replace Value

The following example demonstrates the usage of value replace in animation-composition. The replace composition (#replace-demo) causes the box to be reset to its starting position at the beginning of each animation iteration and completely replaces the previous state.

<html>
<head>
<style>
   @keyframes moveRight {
      0%, 100% {
      transform: translateX(0);
      background-color: lightblue;
      }
      50% {
      transform: translateX(100px);
      background-color: lightcoral;
      }
   }
   .box {
      width: 100px;
      height: 100px;
      margin: 20px;
      display: inline-block;
      animation-duration: 2s;
      animation-iteration-count: infinite;
      border: 2px solid #333;
   }
   #replace-demo {
      animation-name: moveRight;
      animation-composition: replace;
   }
   .container {
      text-align: center;
      margin-top: 50px;
   }
   .label {
      font-weight: bold;
      margin-bottom: 10px;
   }
</style>
</head>
<body>
   <div class="container">
      <div class="label">Replace Composition</div>
      <div class="box" id="replace-demo"></div>
   </div>
</body>
</html>

CSS animation-composition - add Value

The following example demonstrates the usage of add value in animation-composition. The add composition (#add-demo) leads to an additive effect in which the box is shifted further to the right with each iteration, as the shift is accumulated.

<html>
<head>
<style>
   @keyframes moveRight {
      0%, 100% {
      transform: translateX(0);
      background-color: lightblue;
      }
      50% {
      transform: translateX(100px);
      background-color: lightcoral;
      }
   }
   .box {
      width: 100px;
      height: 100px;
      margin: 20px;
      display: inline-block;
      animation-duration: 2s;
      animation-iteration-count: infinite;
      border: 2px solid #333;
   }
   #add-demo {
      animation-name: moveRight;
      animation-composition: add;
   }
   .container {
      text-align: center;
      margin-top: 50px;
   }
   .label {
      font-weight: bold;
      margin-bottom: 10px;
   }
</style>
</head>
<body>
   <div class="container">
      <div class="label">Add Composition</div>
      <div class="box" id="add-demo"></div>
   </div>
</body>
</html>

CSS animation-composition - accumulate Value

The following example demonstrates the usage of accumulate value in animation-composition. The accumulate composition (#accumulate-demo) also accumulates transformations but, unlike add, retains the previous states without resetting, leading to a continuous build-up of translations over multiple iterations.

<html>
<head>
<style>
   @keyframes moveRight {
      0%, 100% {
      transform: translateX(0);
      background-color: lightblue;
      }
      50% {
      transform: translateX(100px);
      background-color: lightcoral;
      }
   }
   .box {
      width: 100px;
      height: 100px;
      margin: 20px;
      display: inline-block;
      animation-duration: 2s;
      animation-iteration-count: infinite;
      border: 2px solid #333;
   }
   #accumulate-demo {
      animation-name: moveRight;
      animation-composition: accumulate;
   }
   .container {
      text-align: center;
      margin-top: 50px;
   }
   .label {
      font-weight: bold;
      margin-bottom: 10px;
   }
</style>
</head>
<body>
   <div class="container">
      <div class="label">Accumulate Composition</div>
      <div class="box" id="accumulate-demo"></div>
   </div>
</body>
</html>
Advertisements