πŸŽ›οΈ Mastering animation-composition in CSS

πŸ“Œ What Is animation-composition?

The animation-composition property controls how multiple animations applied to the same element combine their effects. Instead of one animation overriding the other, you choose whether they should:

  • πŸ”Ή replace β€” last animation wins
  • πŸ”Ή add β€” animations add together
  • πŸ”Ή accumulate β€” animations stack based on iteration count
>>β€œanimation-composition helps animations work together instead of fighting each other.”

🧩 Basic Syntax

Syntax

animation-composition: replace | add | accumulate;

🎯 Values Explained

ValueDescriptionUse Case
replaceEach animation overrides the previous oneDefault behavior
addAdds transform or property values togetherUse for layered effects like move + spin
accumulateEach iteration accumulates the effectUseful for incremental buildup animations

πŸ§ͺ Example Keyframes

Keyframes

@keyframes moveRight {
  to { transform: translateX(100px); }
}

@keyframes rotate {
  to { transform: rotate(45deg); }
}

🎬 Example 1: replace (Default)

replace example

.box {
  animation-name: moveRight, rotate;
  animation-duration: 1s, 1s;
  animation-composition: replace;
}

βœ” Only the rotate animation applies because it replaces moveRight.

🎬 Example 2: add (Combine Animations)

add example

.box {
  animation-name: moveRight, rotate;
  animation-duration: 1s, 1s;
  animation-composition: add;
}

βœ” Final effect = translate + rotate happening together
βœ” Perfect for combining movement, scaling, and rotation

🎬 Example 3: accumulate (Build Over Time)

accumulate example

.box {
  animation-name: moveRight;
  animation-duration: 1s;
  animation-iteration-count: 5;
  animation-composition: accumulate;
}

βœ” Each iteration adds 100px to the previous movement
βœ” Final movement after 5 iterations = 500px

🎨 Visual Example: Move + Scale + Rotate Combined

Combined layered animation

@keyframes scaleUp {
  to { transform: scale(1.3); }
}

.shape {
  animation-name: moveRight, scaleUp, rotate;
  animation-duration: 1s, 1s, 1s;
  animation-composition: add;
}

βœ” Move + Scale + Rotate all apply simultaneously

✨ Using animation-composition with Shorthand

Shorthand

animation: moveRight 1s, rotate 1s;
animation-composition: add;
PropertyWorks with composition?
motion-related transformsβœ” Yes
opacity/colorβœ” Yes (value stacking depends on property type)
layout properties⚠️ Not recommended β€” expensive

🧠 Best Practices

  • Use add for combining transform-based animations
  • Avoid mixing layout-changing properties (width, height) with composition
  • Use accumulate for progressive motion like steps or trails
  • Test combinations in DevTools Animation panel
  • Try to keep animations GPU-friendly (transform + opacity)

πŸ” Debugging Tips

  • If animations override each other unexpectedly β†’ you're using replace
  • If accumulation feels too fast β†’ adjust iteration count or keyframe values
  • Use add only when animations logically stack
  • Check browser support β€” some features are very new

🌐 Browser Support

  • βœ” Chrome 117+
  • βœ” Edge 117+
  • ❌ Firefox (not supported yet)
  • ⚠️ Safari (experimental)

Note

Since animation-composition is new, always provide graceful degradation.

πŸ”— Helpful Resources

>>β€œWith animation-composition, animations don’t compete β€” they collaborate.” ✨