What’s the difference in using Transition: all and Transition: [specific property]?

Asked

Viewed 394 times

3

Making a simple CSS animation to increase the height of a div using transition: all and a question has arisen. Look at the example:

div{
   width: 100px;
   height: 30px;
   background: red;
   transition: all 1s ease;
}

div:hover{
   height: 100px;
}
<div>Passe o mouse</div>

I could use transition: height, since I just want to apply the transition in height, but also using the all the result is the same, since the all will apply the effect to any modified property (provided the property is supported to do so).

So it’s not always best to use the all since it takes all properties? Use all implies some disadvantage in relation to using a specific property in transition (as height, width, background-color etc.)? If this is indifferent, why not just use the all in any event?

  • 1

    In addition to performance, the programmer may want the effect to be applied to certain properties and not to all, or to apply different effects to different properties

1 answer

2


Face the big problem there is the performance. Use the transition:all consumes too much feature of the browser, because it will "try" animate all styles of the class, even if they do not have animation, it leaves the browser waiting for something that will not happen and ends up costing expensive for the performance of the browser, even more if you use this in all your css.

Another point is that like the transition:all your animation in yes loses performance, it can lose fluidity and gets "skipping steps", as Fpss loses in transition.

When you have to cheer up more than one thing with the transition you can do it this way.

div{
   width: 100px;
   height: 30px;
   background: red;
   transition: height 1s ease, background 1s ease;
}

div:hover{
   height: 100px;
   background: blue;
}
<div>Passe o mouse</div>

So overall the recommended is nay use the transition:all

Tip: You can add in your element transform:translateZ(0) to activate hardware acceleration via GPU when animating the element, this also works for animations with @keyframes. If it is of interest to you look also about the property will-change, but this is only worth it if you use it together with JS, because you need to add and remove before and after the animation to avoid consuming the browser resource without being animated.

  • Good! That’s what I wanted to know.

  • 1

    @dvd has an answer on Stackoverflow that also deals with this, I had read before, but I did not find the reference now to have pass. Chrome for example consumes a lot of memory, so everything to avoid this consumption is already valid, especially in Mobile

  • Hugo, let me ask you a quick question: if I have a site with only the index, like a hotpage, I’d better do the whole CSS in the same <style></style> or it’s always good to use one . external css?

  • @dvd I am just working on a project of this type, a singlepage page, I will give you a personal opinion, I will leave the . css from pro development, as it will be a static page at the end I will compile everything together including svg, only js I will call by request

  • @dvd yesterday I wrote fast by mobile, but when finishing the project, I will put all the minified css inside the tag style. The problem is that the project uses Bootstrap4 this will be the only . css that I will call indexed in head, the rest will go on <style>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.