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.

Latest And Best Prime Day Deals

Amazon's Fire TV Cube is down to just $70 thanks to this Prime Day deal
Amazon Fire TV Cube
$69.99 $119.99 Save $50

Save $80 on the Neato D4 robot vacuum during this Prime Day Lightning deal
Neato Robotics D4 Alexa-enabled laser-guided robot vacuum cleaner
$319.99 $400.00 Save $80

Time is running out. And so is the supply. Grab it while you can.

Grab TCL's 32-inch 720p Roku TV for less than $100 in this Prime Day Lightning deal
TCL 32S325 32-inch 720p Roku TV
$99.99 $130.00 Save $30

Act fast while you can. These Lightning deals tend to sell out quick.

The Ring Alarm security system is reaching new low prices for Prime Day
Ring Alarm home security systems

Various configurations of the Ring Alarm are discounted to their best prices yet exclusively for Prime members at Amazon through Tuesday night to help keep your home secure.

The Sonos Beam Prime Day deal includes a $40 discount and 2 $50 Amazon gift cards
The Sonos Beam Prime Day deal includes a $40 discount and $100 in Amazon gift cards
$359.00 $499.00 Save $140

That's just so much savings in one deal. You'll have to wait for the physical gift cards, but that's basically $100 to spend however you want.

Prime Day dropped this PlayStation 4 console bundle to just $250
PlayStation 4 Slim 1TB console with Marvel's Spider-Man and Horizon Zero Dawn
$249.99 $359.98 Save $110

This deal on the PlayStation 4 Slim console saves you $50 off its regular price while also including Marvel's Spider-Man and Horizon Zero Dawn Complete Edition for free. You'll just need an Amazon Prime membership to snag it.

The newest device in the Echo family, the Show 5, is now down to just $50
Echo Show 5
$49.99 $89.99 Save $40

It's only been on the market since May, but it hasn't escaped the Prime Day price cuts.

Amp up your home security with these huge Prime Day discount on nearly all Ring products
Save on Ring products today only

Whether you need a video doorbell, whole home alarm system, or some lights to brighten a dark area, Amazon has it all marked down today!

More Prime Day Deals