I often use these sorts of animation sequences for showing hidden content. In the example below, the background, heading, description and button fade in and slide down in sequence:
Each element needs to perform its transition at a precise time in relation to the its siblings. In the past I would’ve written the CSS for this animation sequence like this:
There are a few issues with this code that makes it difficult to read and update:
- It specifies that the heading transition starts after 0.1 seconds. But what I really want is to specify that the heading transition starts after the background transition ends.
- If we change the speed of one animation step, we’ll need to manually update the timing of the rest of the sequence.
- Our transition delays and durations are mixed in with our styling CSS, making it more difficult to scan and understand the sequence of animations.
Let’s step back and reconsider our approach by visualizing a timeline of the animation sequence we’re creating. First the background (BG) animates, then the heading, then the description, and finally the button.
Using variables, we can write CSS to match the timeline above and solve all three of the issues with our previous code.
- Transition delays are defined relative to previous animation steps instead of being set to specific times.
- Since each transition delay is relative to the previous animation step’s timing, changing one step’s speed updates the whole sequence.
- The CSS for our timeline is all in one place instead of scattered throughout our CSS file.
Here’s an example of how we could achieve this in CSS. (The examples in this article are using CSS custom properties, but the same technique could be achieved using SASS or LESS variables if you need to support IE11.)
First, let’s set the duration of each step of our animation:
Next, we calculate when each animation step should begin (their
transition-delays). We can do this by adding the previous animation step’s start time and duration. This gives us the time that the previous animation step ended:
Finally, we can reference those properties to set the transition timing of our elements:
This technique makes it easier to understand and change the animation timeline. Changing the speed of one transition automatically ripples throughout the whole animation sequence without any manual work.
I find this a lot easier to work with, but there’s still a problem: it’s difficult to change the overall speed of the animation sequence. If I decide the whole sequence should be faster or slower, I’ll need to change the speed of each animated element.
By making all of the durations relative to a
--base-speed variable, we can easily speed up or slow down the whole sequence. This also simplifies debugging, since you can view the sequence in slow motion by changing one value.
I added some sliders to the animation so you can control the CSS timeline by updating the base speed or individual elements’ relative speeds. (Move the sliders to the right to slow things down.)
This technique can be scaled up to build some truly complex animations. I decided to build a CSS and SVG Rube Goldberg machine to put it to the test:
I built this animation using SVG imagery, but the underlying strategy remains the same. Each element’s delay is calculated by adding the previous animation’s start time and duration. For this more complicated example I had to use a few new tricks:
- In some cases I wanted the next animation step to start slightly before the previous step ended. In that case I multiplied the previous duration by a fraction or decimal.
- I used CSS keyframe animations in addition to transitions. These were timed using
- When the SVG is wider than the viewport, I added a horizontal panning transition to the entire SVG. I used the duration of all animation steps added together to make sure this was timed correctly.
Here’s what this CSS timeline ended up looking like:
Once again, I’ve separated out the animation timeline from the rest of the styles, allowing me to change the timeline without digging through a bunch of other CSS rules.
This complex animation sequence was built entirely with CSS. All it takes to play the entire sequence is toggling a single class!
I’ve just started experimenting with this technique and I’m excited to keep exploring the possibilities and building more animations on the web!