CSS Animations

Enhance user experience

What is it?

CSS animations let you animate from one style to another. Like traditional animation, keyframes are utilized and can be used to manipulate properties such as transformations, transitions, timing, repeats, and more.

Advantages to CSS Animations

They're easy to use for simple animations; you can create them without even having to know JavaScript. They also run well, even under a moderate system load and can be powerful tool to improve userexperience and site design.

The Building Blocks of Animations

CSS animations are made up of two basic building blocks.

  1. Keyframes - define the stages and styles of the animation.
  2. Animation Properties - assign the @keyframes to a specific CSS element and define how it is animated.

Building Block #1: Keyframes

Keyframes define what the animation looks like at each stage of the animation timeline. Each @keyframe is composed of:

@keyframes bounceIn {
0% {
transform: scale(0.1);
opacity: 0;
}
60% {
transform: scale(1.2);
opacity: 1;
}
100% {
transform: scale(1);
}
}

Building Block #2: Animation Properties

Animation properties do two things:

  1. They assign the @keyframes to the elements that you want to animate.
  2. They define how it is animated.

You must add the following two animation properties for the animation to take effect:

div {
animation-duration: 2s;
animation-name: bounceIn;
}

Additional Animation Properties

animation-delay

Configures the delay between the time the element is loaded and the beginning of the animation sequence. The syntax animation-delay: 2s; tells this circle to react two seconds after a :hover .

animation-direction

Configures whether or not the animation should alternate direction on each run through the sequence or reset to the start point and repeat itself.

animation-duration

Configures the length of time that an animation should take to complete one cycle. (the example below has a duration of 5s, where as this one is 2s)

animation-iteration-count

Configures the number of times the animation should repeat; you can specify infinite to repeat the animation indefinitely (as seen above). This one however has an iteration of 3, when hovered on.

animation-play-state

Lets you pause and resume the animation sequence. Very helpful with :hover as sometimes a glitch effect can occur when element abruptly snaps back into it's original state. An animation-play-state: paused; and just change it on :hover to animation-play-state: running;.

animation-timing-function

Configures the timing of the animation; that is, how the animation transitions through keyframes, by establishing acceleration curves. This animation uses animation-timing-function: linear; to express the same speed from start to end

animation-fill-mode

Configures what values are applied by the animation before and after it is executing. When we define it with a forward; value, we are telling the browser that we want our animated element as defined in the last keyframe. This way, we don’t get that final jump back to the initial state of the element before it started animating