How to Use CSS3 Transitions & Animations to Highlight UI Changes

Designers and artists have a long history of experimenting with motion, effects, and different kinds of illusions with the aim of adding an extra layer to their work. The op art movement began to use optical illusions in the 1960s to give the impression of motion.

Since then newer and newer approaches have popped up, such as the recently popular kinetic art that extends the viewer’s perspective by using multidimensional movement. Motion also appeared in computer science with the invention of the first blinking cursor in 1967.

In front-end development DOM elements were usually animated by JavaScript before CSS3 was released, and it’s a method that still works, but the new properties introduced by CSS3 allows us to enhance our designs with different effects and motion in a more intuitive way.

The two main techniques CSS3 offers are transitions and animations. In this post we will take a look at what they are, what is the difference between them, and how you can make use of them.

Transitions

Transitions and animations are both used to visualize changes in the state of an HTML element by modifying one or more of its CSS properties.

The simplest form of state change visualization is altering the colour of a button or a link when it’s hovered on. When it happens, the element gets a slightly different style, which is usually noticed by the viewer as if something have moved on the screen.

Changing CSS properties of a link on hover (or focus, or click) is the oldest and simplest form of transitions, and it existed well before the CSS3 era.

Transitions are used when an HTML element changes from one predefined state to another. CSS3 introduced new properties that allow more sophisticated visualizations than before, such as timing functions or duration control.

We will take a look at the new CSS properties in the next section, after understanding how animations differ. For now, let’s see the most important things you need to know about transitions.

They always have a beginning and an end state.

The states between the start and end points are implicitly defined by the browser, we can’t change that with CSS.

They require explicit triggering, such as adding a new pseudoclass by CSS, or a new class by jQuery.

You can see a beautiful example of smartly utilized CSS3 transitions below, in which the author reveals hidden information in a way that’s non-intrusive but still steers users’ focus on the new content.

Animations

If we want to visualize state changes with more complicated movements, or if we don’t have an explicit trigger, e.g. if we want to start the effect when the page loads, animations are the way to go.

Animations make it possible to define a more complex path by setting and configuring our own keyframes. Keyframes are intermediate points in the course of the animation, that allows us to change the style of the animated element as many times as we want.

Although CSS3 offers great ways to build sophisticated animations, it’s usually harder to create them than transitions, that’s why there are many great animation libraries out there, which can facilitate our work.

The most important things you need to know about CSS3 animations include:

they don’t require explicit triggering, they can start on page load or when another DOM event takes place in the browser

they can be used in cases when transitions are used, for example when a new class or pseudoclass is added or removed (although it’s a less frequent use case)

they require us to define some keyframes (intermediate states)

we can specify the number, the frequency, and the style of these keyframes

In the example below you can see a cool animated dropdown menu. The animation starts when we click on the button. This is achieved by adding extra classes to the list elements with jQuery when the click event occurs.

These new classes are animated with specified @keyframes rules in the CSS file. The extra classes are removed by jQuery when the user clicks on the button the next time, and the menu becomes hidden again.

We specified the background property, because this is what will be changed during the transition.

We can alter more than one CSS property in one transition, in this case the code above would be modified like this: transition-property: background, border;. We can also use the transition-property: all;, if we don’t want to specify each property separately.

We can choose the shorthand transition property as well. If we do so, we always need to pay attention to the proper order of the inner properties (see the syntax in the docs).

If we want to create an animation, we are required to specify the related keyframes. The CSS properties need to be modified in separately defined @keyframesat-rules. Here is an example of how we can do this:

In the example above we created a very simple sliding effect. We defined the animation-name, then bound 3 keyframes to it which we specified in the @keyframes slide { ... } at-rules. The percentages refer to the duration of the animation, so 50% happens at 1.5s in the example.

We could use the shorthand animation property as well, or could define the keyframes with the more simple from {} to {} rule in the following way:

When building transitions and animations, you need to know that not all CSS properties can be animated, so it’s always a good idea to check the property you want to change in the CSS Animatable.

CSS3 animations and transitions worked with vendor prefixes for a long time, which we don’t have to use any more, however the Mozilla Developer Network still recommends to add the -webkit prefix for a while, as the support for Webkit-based browsers only recently achieved stability.