CSS: Animation vs. Transition
Asked Answered
S

12

185

I understand how to perform both CSS3 transitions and animations. What is not clear is when to use which.

For example, if I want to make a ball bounce, it is clear that animation is the way to go. I could provide keyframes and the browser would do the intermediates frames and I'll have a nice animation going.

However, there are cases when a said effect can be achieved either way. A simple and common example would be implement the Facebook style sliding drawer menu:

This effect can be achieved through transitions like so:

.sf-page {
    transition: transform .2s ease-out;
}

.sf-page.out {
    transform: translateX(240px);
}

http://jsfiddle.net/NwEGz/

Or, through animations like so:

.sf-page {
    animation-duration: .4s;
    transition-timing-function: ease-out;
}

.sf-page.in {
    animation-name: sf-slidein;
    transform: translate3d(0, 0, 0);
}

.sf-page.out {
    animation-name: sf-slideout;
    transform: translateX(240px);
}

@keyframes sf-slideout {
    from { transform: translate3d(0, 0, 0); }
    to { transform: translate3d(240px, 0, 0); }
}
 
@keyframes sf-slidein {
    from { transform: translate3d(240px, 0, 0); }
    to { transform: translate3d(0, 0, 0); }
}

http://jsfiddle.net/4Z5Mr/

With HTML that looks like so:

<div class="sf-container">
    <div class="sf-page in" id="content-container">
        <button type="button">Click Me</button>
    </div>
    <div class="sf-drawer">
    </div>
</div>

And, this accompanying jQuery:

$("#content-container").click(function(){
    $("#content-container").toggleClass("out");
    // below is only required for css animation route
    $("#content-container").toggleClass("in");
});

What I'd like to understand is what are the pros and cons of these approaches.

  1. One obvious difference is that animating takes a whole lot more code.
  2. Animation gives better flexibility. I can have different animation for sliding out and in.
  3. Is there something that can be said about performance. Do both take advantage of hardware acceleration?
  4. Which is more modern and the way going forward?
Squishy answered 14/12, 2013 at 17:28 Comment(0)
E
249

It looks like you've got a handle on how to do them, just not when to do them.

A transition is an animation, just one that is performed between two distinct states - i.e. a start state and an end state. Like a drawer menu, the start state could be open and the end state could be closed, or vice versa.

If you want to perform something that does not specifically involve a start state and an end state, or you need more fine-grained control over the keyframes in a transition, then you've got to use an animation.

Elegant answered 15/12, 2013 at 1:41 Comment(1)
check out this article too kirupa.com/html5/css3_animations_vs_transitions.htm , it correctly points out that transitions are the way to go when doing javascript interactions.Telstar
I
43

I'll let the definitions speak for themselves (according to Merriam-Webster):

Transition: A movement, development, or evolution from one form, stage, or style to another

Animation: Endowed with life or the qualities of life; full of movement

The names appropriately fit their purposes in CSS

So, the example you gave should use transitions because it is only a change from one state to another

Immigrate answered 15/12, 2013 at 1:36 Comment(0)
E
36

A shorter answer, straight on point:

Transition:

  1. Needs a triggering element (:hover, :focus etc.)
  2. Only 2 animation states (start and end)
  3. Used for simpler animations (buttons, dropdown menus and so on)
  4. Easier to create but not so many animation/effect possibilities

Animation @keyframes:

  1. It can be used for endless animations
  2. Can set more than 2 states
  3. No boundaries

Both use CPU acceleration for a much smoother effect.

Ejaculate answered 14/1, 2019 at 11:17 Comment(2)
When you say "CPU acceleration", did you perhaps mean "GPU acceleration"? I believe that non-layout recalculating animations like "translate" get that benefitExaggerated
I would have added to this list that animations aren't supported by IE 10 while transitions are, if that's still relevant to anyone to make the choice.Intake
N
14
  1. Animation takes a lot more code unless you're using the same transition over and over, in which case an animation would be better.

  2. You can have different effects for sliding in and out without an animation. Just have a different transition on both the original rule and the modified rule:

    .two-transitions {
        transition: all 50ms linear;
    }
    
    .two-transitions:hover {
        transition: all 800ms ease-out;
    }
    
  3. Animations are just abstractions of transitions, so if the transition is hardware accelerated, the animation will be. It makes no difference.

  4. Both are very modern.

  5. My rule of thumb is if I use the same transition three times, it should probably be an animation. This is easier to maintain and alter in the future. But if you are only using it once, it is more typing to make the animation and maybe not worth it.

Nuzzi answered 14/12, 2013 at 19:42 Comment(1)
I should also add that animations have keyframes, allowing you to do very complex and controlled progressions, which is kind of amazing.Nuzzi
K
4

Is there something that can be said about performance. Do both take advantage of h/w acceleration?

In modern browsers, h/w acceleration occurs for the properties filter, opacity and transform. This is for both CSS Animations and CSS Transitions.

Klenk answered 22/11, 2018 at 6:1 Comment(0)
F
3

Animations are just that - a smooth behavior of set of properties. In other words it specifies what should happen to a set of element's properties. You define an animation and describe how this set of properties should behave during the animation process.

Transitions on the other side specify how a property (or properties) should perform their change. Each change. Setting a new value for certain property, be it with JavaScript or CSS, is always a transition, but by default it is not smooth. By setting transition in the css style you define different (smooth) way to perform these changes.

It can be said that transitions define a default animation that should be performed every time the specified property has changed.

Frida answered 11/3, 2014 at 21:23 Comment(3)
I disagree with this definition as both specify how and whatImmigrate
I disagree with the whole language to begin with. If people could just stop trying to classify and wrap everything in a nonsensical uber-abstracted poetic human pseudo-language and get to the point instead then we the programmers reading the text would be better off.Entresol
Programmers are humans too. And this is often forgotten. We don't communicate with machines - compilers do that. We communicate with humans - those who read our specification, those who try to use our APIs, and those who eventually use our software. So, if a description being poetic helps someone understand a concept - it is much better than a formalistic, pseudo-deterministic specification, that serves nobody.Frida
V
1

.yourClass {
    transition: all 0.5s;
color: #00f;
margin: 50px;
font-size: 20px;
cursor: pointer;
}

.yourClass:hover {
    color: #f00;
}
<p class="yourClass"> Hover me </p>
Vulnerable answered 13/2, 2020 at 5:46 Comment(0)
S
1

CSS3 Transitions brought frontend developers a significant ability to modify the appearance and behavior of an element as relative to a change in his state. CSS3 animations extends this ability and allow to modify the appearance and behavior of an element in multiple keyframes, so transitions provides us the ability to change from one state to another, while that animations can set multiple points of transition within different keyframes.

So, let's look at this transition sample where applied a transition with 2 points, start point at left: 0 and an end point at left: 500px

.container {
  background: gainsboro;
  border-radius: 6px;
  height: 300px;
  position: relative;
}

.ball {
  transition: left 2s linear;
  background: green;
  border-radius: 50%;
  height: 50px;
  position: absolute;
  width: 50px;
  left: 0px;
}

.container:hover .ball{
  left: 500px;
}
<div class="container">
  <figure class="ball"></figure>
</div>

The above can be also created via animation like so:

@keyframes slide {
  0% {
    left: 0;
  }
  100% {
    left: 500px;
  }
}

.container {
  background: gainsboro;
  border-radius: 6px;
  height: 200px;
  position: relative;
}

.ball {
  background: green;
  border-radius: 50%;
  height: 50px;
  position: absolute;
  width: 50px;
}

.container:hover .ball {
  animation: slide 2s linear;
}
<div class="container">
  <figure class="ball"></figure>
</div>

And if we would like another in-between point, it would be possible to achieve only via animation, we can add another keyFrame to achieve this and this is the real power of animation over transition:

@keyframes slide {
  0% {
    left: 0;
  }
  50% {
    left: 250px;
    top: 100px;
  }
  100% {
    left: 500px;
  }
}

.container {
  background: gainsboro;
  border-radius: 6px;
  height: 200px;
  position: relative;
}

.ball {
  background: green;
  border-radius: 50%;
  height: 50px;
  position: absolute;
  width: 50px;
}

.container:hover .ball {
   animation: slide 2s linear;
}
<div class="container">
  <figure class="ball"></figure>
</div>
Saguenay answered 26/1, 2022 at 14:40 Comment(0)
F
0

transition can go reverse from middle of the way, but animation replay the keyframes from start to end.

const transContainer = document.querySelector(".trans");

transContainer.onclick = () => {
  transContainer.classList.toggle("trans-active");
}


const animContainer = document.querySelector(".anim");
animContainer.onclick = () => {
  if(animContainer.classList.contains("anim-open")){
    animContainer.classList.remove("anim-open");
    animContainer.classList.add("anim-close");
  }else{
    animContainer.classList.remove("anim-close");
    animContainer.classList.add("anim-open");
  }
}
*{
  font: 16px sans-serif;
}

p{
  width: 100%;
  background-color: #ff0;
}
.sq{
  width: 80px;
  height: 80px;
  margin: 10px;
  background-color: #f00;
  display: flex;
  justify-content: center;
  align-items: center;
}

.trans{
  transition: width 3s;
}
.trans-active{
  width: 200px;
}

.anim-close{
  animation: closingAnimation 3s forwards;
}
.anim-open{
  animation: openingAnimation 3s forwards;
}

@keyframes openingAnimation {
  from{width: 80px}
  to{width: 200px}
}
@keyframes closingAnimation {
  from{width: 200px}
  to{width: 80px}
}
<p>Try click them before reaching end of movement:</p>
<div class="sq trans">Transition</div>
<div class="sq anim">Animation</div>

in addition, if you want the javascript to listen for end of transition, you'll get one event for each property that you change. for example transition: width 0.5s, height 0.5s. the transitionend event will trigger two times, one for width and one for height.

Fleming answered 20/4, 2022 at 20:37 Comment(0)
M
0

Just a summary, thanks to this post, there are 5 main differences between CSS transitions vs CSS animations:

1/ CSS transitions:

  • Animate an object from one state to another, implicitly by browser
  • Cannot loop
  • Need a trigger to run (:hover, :focus)
  • Simple, less code, limited powerful
  • Easy to work in JavaScript

2/ CSS animations:

  • Freely switch between multiple states, with various properties and time frame
  • Can loop
  • Don’t need any kind of external trigger
  • More complex, more code, more flexible
  • Hard to work in JavaScript due to syntax for manipulating keyframes
Machree answered 1/1, 2023 at 11:0 Comment(0)
V
-1

I believe CSS3 animation vs CSS3 transition will give you the answer you want.

Basically below are some takeaways :

  1. If performance is a concern, then choose CSS3 transition.
  2. If state is to be maintained after each transition, then choose CSS3 transition.
  3. If the animation needs to be repeated, choose CSS3 animation. Because it supports animation-iteration-count.
  4. If a complicated animation is desired. Then CSS3 animation is preferred.
Vanillin answered 17/6, 2015 at 15:35 Comment(3)
#2 and #3 aren't trueImmigrate
I'm not sure how #3 is untrue? It seems a reasonable point to consider, and the animation-iteration-count seems to be a valid property: developer.mozilla.org/en-US/docs/Web/CSS/…Leptospirosis
Two things with that: 1) if a transition is started again later it is still a "repeat" even if it's not immediately after the first iteration. 2) you can have a transition that fakes immediate repeats depending on the animation. See my CSS-Tricks article for more on what I mean.Immigrate
S
-1

Don't bother yourself which is better. My give away is that, if you can solve your problem with just one or two lines of code then just do it rather than writing bunch of codes that will result to similar behavior. Anyway, transition is like a subset of animation. It simply means transition can solve certain problems while animation on the other hand can solve all problems. Animation enables you to have control of each stage starting from 0% all the way to 100% which is something transition cannot really do. Animation require you writing bunch of codes while transition uses one or two lines of code to perform the same result depending on what you are working on. Coming from the point of JavaScript, it is best to use transition. Anything that involve just two phase i.e. start and finish use transition. Summary, if it is stressful don't use it since both can produce similar result

Sneck answered 7/5, 2020 at 0:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.