If you’ve ever wanted to add a pause between each iteration of your CSS @keyframes
animation, you’ve probably been frustrated to find there’s no built-in way to do it in CSS. Sure, we can delay the start of a set of @keyframes
with animation-delay
, but there’s no way to add time between the first iteration through the keyframes and each subsequent run.
This came up when I wanted to adapt this shooting stars animation for use as the background of the homepage banner in a space-themed employee portal. I wanted to use fewer stars to reduce distraction from the main content, keep CPUs from melting, and still have the shooting stars seem random.
No pausing
For comparisons sake.
The “original” delay method
Here’s an example of where I applied the traditional keyframes delay technique to my fork of the shooting stars.
This approach involves figuring out how long we want the delay between iterations to be, and then compressing the keyframes to a fraction of 100%. Then, we maintain the final state of the animation until it reaches 100% to achieve the pause.
@keyframes my-animation {
/* Animation happens between 0% and 50% */
0% {
width: 0;
}
15% {
width: 100px;
}
/* Animation is paused/delayed between 50% and 100% */
50%, 100% {
width: 0;
}
}
I experienced the main drawback of this approach: each keyframe has to be manually tweaked, which is mildly painful and certainly prone to error. It’s also harder to understand what the animation is doing if it requires mentally transposing all the keyframes back up to 100%.
New technique: hide during the delay
Another technique is to create a new set of @keyframes
that is responsible for hiding the animation during the delay. Then, apply that with the original animation, at the same time.
.target-of-animation {
animation: my-awesome-beboop 1s, pause-between-iterations 4s;
}
@keyframes my-awesome-beboop {
...
}
@keyframes pause-between-iterations {
/* Other animation is visible for 25% of the time */
0% {
opacity: 1;
}
25% {
opacity: 1;
}
/* Other animation is hidden for 75% of the time */
25.1% {
opacity: 0;
}
100% {
opacity: 0;
}
}
A limitation of this technique is that the pause between animations must be an integer multiple of the “paused” keyframes. That’s because keyframes that repeat infinitely will immediately execute again, even if there are longer running keyframes being applied to the same element.
Interesting aside: When I started this article, I mistakenly thought that an easing function is applied at 0% and ends at 100%.. Turns out that the easing function is applied to each CSS property, starting at the first keyframe where a value is defined and ending at the next keyframe where a value is defined (e.g., an easing curve would be applied from 25% to 75%, if the keyframes were 25% { left: 0 } 75% { left: 50px}
). In retrospect, this totally makes sense because it would be hard to adjust your animation if it was a subset of the total easing curve, but my mind is slightly blown.
In the my-awesome-beboop
keyframes example above, my-awesome-beboop
will run three times behind the scenes during the pause-between-animations
keyframes before being revealed for what appears to be its second loop to the user (which is really the fifth time it’s been executed).
Here’s an example that uses this to add a delay between the shooting stars:
Can’t hide your animation during the delay?
If you need to keep your animation on screen during the delay, there is another option besides hiding. You can still use a second set of @keyframes
, but animate a CSS property in a way that counteracts or nullifies the motion of the primary animation. For example, if your main animation uses translateX
, you can animate left
or margin-left
in your set of delay @keyframes
.
Here’s a couple of examples:
Pause by changing transform-origin
:
Pause by counter-acting transform: translateX
by animating the left
property:
In the case of the pausing the translateX
animation, you’ll need to get fancier with the @keyframes
if you need to pause the animation for more than just a single iteration:
/* pausing the animation for three iterations */
@keyframes slide-left-pause {
25%, 50%, 75% {
left: 0;
}
37.5%, 62.5%, 87.5% {
left: -100px;
}
100% {
left: 0;
}
}
You may get some slight jitter during the pause. In the translateX
example above, there’s some minor vibration on the ball during the slide-left-pause
as the animations fight each other for dominance.
Wrap up
The best option performance-wise is to hide the element during the delay or animate transform
. Animating properties like left
, margin
, width
are much more intense on a processor than animating opacity
(although the contain
property appears to be changing that).
If you have any insights or comments on this idea, let me know!
Thanks to Yusuke Nakaya for the original shooting stars CSS animation that I forked on CodePen.