How to have multiple CSS transitions on an element?

AnimationCssCss Transitions

Animation Problem Overview


It's a pretty straightforward question but I can't find very good documentation on the CSS transition properties. Here is the CSS snippet:

    .nav a
{
	text-transform:uppercase;
	text-decoration:none;
	color:#d3d3d3;
	line-height:1.5 em;
	font-size:.8em;
	display:block;
	text-align:center;
	text-shadow: 0 -1.5em 0 rgba(255, 255, 255, 0.15);
	-webkit-transition: color .2s linear;
	-moz-transition: color .2s linear;
	-o-transition: color .2s linear;
	transition: color .2s linear;
	-webkit-transition: text-shadow .2s linear;
	-moz-transition: text-shadow .2s linear;
	-o-transition: text-shadow .2s linear;
	transition: text-shadow .2s linear;
}

.nav a:hover
{
	color:#F7931E;
	text-shadow: 0 1.5em 0 rgba(247, 147, 30, 0.15);
}

As you can see, the transition properties are overwriting eachother. As it stands, the text-shadow will animate, but not the color. How do I get them both to simultaneously animate? Thanks for any answers.

Animation Solutions


Solution 1 - Animation

Transition properties are comma delimited in all browsers that support transitions:

.nav a {
  transition: color .2s, text-shadow .2s;
}

ease is the default timing function, so you don't have to specify it. If you really want linear, you will need to specify it:

transition: color .2s linear, text-shadow .2s linear;

This starts to get repetitive, so if you're going to be using the same times and timing functions across multiple properties it's best to go ahead and use the various transition-* properties instead of the shorthand:

transition-property: color, text-shadow;
transition-duration: .2s;
transition-timing-function: linear;

Solution 2 - Animation

EDIT: I'm torn on whether to delete this post. As a matter of understanding the CSS syntax, it's good that people know all exists, and it may at times be preferable to a million individual declarations, depending on the structure of your CSS. On the other hand, it may have a performance penalty, although I've yet to see any data supporting that hypothesis. For now, I'll leave it, but I want people to be aware it's a mixed bag.

Original post:

You can also simply significantly with:

.nav a {
    transition: all .2s;
}

FWIW: all is implied if not specified, so transition: .2s; will get you to the same place.

Solution 3 - Animation

Something like the following will allow for multiple transitions simultaneously:

-webkit-transition: color .2s linear, text-shadow .2s linear;
   -moz-transition: color .2s linear, text-shadow .2s linear;
     -o-transition: color .2s linear, text-shadow .2s linear;
        transition: color .2s linear, text-shadow .2s linear;

Example: http://jsbin.com/omogaf/2

Solution 4 - Animation

If you make all the properties animated the same, you can set each separately which will allow you to not repeat the code.

 transition: all 2s;
 transition-property: color, text-shadow;

There is more about it here: https://stackoverflow.com/questions/9670075/css-transition-shorthand-with-multiple-properties

I would avoid using the property all (transition-property overwrites 'all'), since you could end up with unwanted behavior and unexpected performance hits.

Solution 5 - Animation

.nav a {
    transition: color .2s, text-shadow .2s;
}

Solution 6 - Animation

It's possible to make the multiple transitions set with different values for duration, delay and timing function. To split different transitions use ,

button{
  transition: background 1s ease-in-out 2s, width 2s linear;
  -webkit-transition: background 1s ease-in-out 2s, width 2s linear; /* Safari */
}

Reference: https://kolosek.com/css-transition/

Solution 7 - Animation

Here's a LESS mixin for transitioning two properties at once:

.transition-two(@transition1, @transition1-duration, @transition2, @transition2-duration) {
 -webkit-transition: @transition1 @transition1-duration, @transition2 @transition2-duration;
    -moz-transition: @transition1 @transition1-duration, @transition2 @transition2-duration;
      -o-transition: @transition1 @transition1-duration, @transition2 @transition2-duration;
          transition: @transition1 @transition1-duration, @transition2 @transition2-duration;
}

Solution 8 - Animation

It's also possible to avoid specifying the properties altogether.

#box {
  transition: 0.4s;
  position: absolute;
  border: 1px solid darkred;
  bottom: 20px; left: 20px;
  width: 200px; height: 200px;
  opacity: 0;
}

#box.on {
  opacity: 1;
  height: 300px;
  width: 500px;
 }

Solution 9 - Animation

In Sass you can achieve using below code

@mixin transition($transitions...) {
  $unfoldedTransitions: ();
  @each $transition in $transitions {
    $unfoldedTransitions: append($unfoldedTransitions, unfoldTransition($transition), comma);
  }
  
  -webkit-transition: $unfoldedTransitions;
  transition: $unfoldedTransitions;
}

@function unfoldTransition ($transition) {
  // Default values
  $property: all;
  $duration: .2s;
  $easing: null; // Browser default is ease, which is what we want
  $delay: null; // Browser default is 0, which is what we want
  $defaultProperties: ($property, $duration, $easing, $delay);

  // Grab transition properties if they exist
  $unfoldedTransition: ();
  @for $i from 1 through length($defaultProperties) {
    $p: null;
    @if $i <= length($transition) {
      $p: nth($transition, $i)
    } @else {
      $p: nth($defaultProperties, $i)
    }
    $unfoldedTransition: append($unfoldedTransition, $p);
  }

  @return $unfoldedTransition;
}
// Usage:   @include transition(width, height 0.3s ease-in-out);

All credit goes to tobiasahlin https://gist.github.com/tobiasahlin

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEric ThomaView Question on Stackoverflow
Solution 1 - AnimationcoreywardView Answer on Stackoverflow
Solution 2 - AnimationXMLView Answer on Stackoverflow
Solution 3 - AnimationDelan AzabaniView Answer on Stackoverflow
Solution 4 - AnimationCorey YoungView Answer on Stackoverflow
Solution 5 - AnimationLocorocoView Answer on Stackoverflow
Solution 6 - AnimationNesha ZoricView Answer on Stackoverflow
Solution 7 - AnimationMark PView Answer on Stackoverflow
Solution 8 - AnimationjerblackView Answer on Stackoverflow
Solution 9 - AnimationSourav SinghView Answer on Stackoverflow