More efficient CSS3 transitions

CSS transitions rarely happen in isolation. Adding transitions to groups of elements means there's more to keep track of and more opportunity to add variety. We can also take advantage of how our brains tend to see things to both save ourselves some work and make things a little more interesting

Shares

Elements of a web page tend to appear in groups, and we usually like to apply transitions and animations to them in groups too. Funny how that works. When we start thinking about how these transitions work together we can also start taking advantage of of a little shortcut thanks to the way our eyes like to trick us.

I should warn you up front, this is less of a tutorial on how to build a specific thing and more of a demonstration of a handy visual shortcut we can take when applying transitions to multiple things. It's a good one to keep up your sleeve for those times you feel like you're piling on too many transitions or aren't happy with applying the exact same transition to every single element you're working with.

Misdirection and our eyes on transitions

Our brains play a lot of helpful tricks on us. For example, when we see a number of objects in motion, we tend to project that motion onto other nearby objects as well. In animation theory this is referred to as misdirection. We see the additional motion because it is suggested by other motion happening around it, not because it actually happened. Tricky!

So why care about this misdirection stuff? Because it can save us some work while making things look a bit more interesting. We can write fewer transitions and make it look like there's more than that going on than there really is. Not a bad deal, really. Also, it's nice to mix things up. Applying the same transition verbatim to all the elements we're working with gets boring really fast.

Our starting point

Let's build a little something to show what I'm talking about. And let's make sure it includes kittens.

Let's get transitioning!

With our basic HTML in place, we'll be adding transitions to our list item's background fill and our image for each pet in our list.

We'll define our transitions for "all" properties to keep things simple, even though we'll only actually be changing one property per element in this case. We'll keep them relatively short and snappy too. Our containing .pet li has simple ease-out transition applied. And each image within it has an ever so slightly longer transition applied and defined as a cubic bezier. (Bezier equations were arrived at with the extremely helpful help of matthewlein.com/ceaser/).

We've only defined transitions for two of our elements, but as you can see above, we're changing properties for three of them on :hover. When you first viewed the demo, you probably noticed the colour change on each item's title in addition to the background colour and image scaling. However, you might not have noticed that there was no actual CSS transition applied to it.

With the soft transition of the background colour behind it and the image scaling above it, your eyes want to see that the text transitioned smoothly as well even though it was really just a hard cut to the new colour. In this situation it appears to move with the group to its hover state.

Of course, now that I've called your attention to it, or if your eagle eyes inferred a bit from the article's title, you were looking for it and the effect is lessened. But to the casual viewer, there's no difference, and you've just written one less transition than appears to be there. Not to mention made the overall motion of the group more varied and interesting.

And the moral of our story

When you mix elements that have a true transitions defined and those that just change with a hard cut to their new state you can get more interesting effects with fewer transitions written out. It's more of an art than a science, but we can pretty easily create situations where what appears to be going on visually is much more complicated than what we've actually coded behind the scenes.

Further reading

If you think animation theory sounds like a fun thing to know more about. Especially in the context of web design, here are a couple of good places to start: