How to start several CSS transitions at once

The following is excerpted from CSS Animation: An Interactive Guide by Vicki Murley.

You've seen several examples throughout this book where a single CSS property is animated. But to create more advanced effects, you may want to animate more than one property at a time. The previous section showed multiple ways to define transitions for multiple CSS properties, on the same element. For example:

Or, if you prefer to use the long form properties:

To trigger the transitions on opacity and transform to begin, you might write JavaScript that changes the values of these two properties individually, like so:

This works, but there's a better approach when you need to trigger transitions on more than one property, on the same element. First, encapsulate the CSS properties that are transitioning in a CSS class. Then, when you want to trigger the transition, add or remove that class from the element using classList. classList is a DOM Element property which returns a token list of the class attribute on an element. Additionally, classList has four methods which simplify the process of adding, removing, toggling, and testing for the existence of a class on an element:

  • element.classList.add('className')
    Add this class to an element, if the element doesn't already have it.
  • element.classList.remove('className')
    Remove this class from the element.
  • element.classList.toggle('className')
    If the element has the class remove it, otherwise add it.
  • element.classList.contains('className')
    Returns true or false depending on whether the element's class list contains the class.

Let's walk through an example of using classList to trigger CSS transitions on several properties at once. In this example, the text HELLO WORLD rises upward from a position just below the middle of the page, while transitioning from transparent to opaque. Try it out on CodePen before moving on to the code.

The HTML element is simply some text inside a <div>, with two CSS classes applied:

The hello class styles and centers the text, and defines transitions for the transform and opacity properties. The hidden class makes the Hello World <div> transparent, and moves it downward:

With a line or two of JavaScript, you can trigger the transition to begin by removing the hidden class from the element. In this case, the transition is triggered when the page loads:

That's really all there is to it. It's just as easy to create a transition animation that moves the text on and off the screen — just use toggleClass instead of removeClass. When the hidden class is removed HELLO WORLD moves upward and becomes visible, and when hidden is added it moves downward and becomes transparent:

This example on CodePen shows the above code in action. You can tap or click anywhere on the main background image to toggle the hidden class. Try tapping or clicking when HELLO WORLD is in mid-transition, and notice how the text immediately switches direction.

Note that the CSS for the hello element hasn't changed — defining a transitions for transform and opacity on this element causes these properties to be animated whenever they change in any way. You'll learn more about defining different behavior for the "forward" and "reverse" directions of a transition later in this section.

For more, get your copy of CSS Animation: An Interactive Guide by Vicki Murley on iPad or Mac today!

This post may contain affiliate links. See our disclosure policy for more details.