How to Create and Customize Animations in CSS

Besides the transitions, there is also a similar concept called animation. Their main difference is that the transition requires you to define a trigger using pseudo-selectors, while the animation will play automatically once it is loaded.

To create an animation, you must define its keyframes using the @keyframes rule.

css
1@keyframes change-bg-color {
2  from {
3    background-color: white;
4  }
5  to {
6    background-color: darkviolet;
7  }
8}

In this example, the animation is named change-bg-color, and it changes the background-color property from white to darkviolet. To apply this animation, you must specify the animation-name and animation-duration properties.

css
1div {
2  /* . . . */
3
4  animation-name: change-bg-color;
5  animation-duration: 4s;
6}

Instead of using the keywords from and to, it is also possible to use percentage values, which allows you to define more than two keyframes.

css
1@keyframes change-bg-color {
2  0% {
3    background-color: white;
4  }
5  25% {
6    background-color: bisque;
7  }
8  50% {
9    background-color: crimson;
10  }
11  100% {
12    background-color: darkviolet;
13  }
14}
Open demo in new tab

Like transitions, animation allows you to define properties such as animation-timing-function and animation-delay. These work exactly the same as their transition counterparts.

css
1div {
2  /* . . . */
3
4  animation-name: change-bg-color;
5  animation-duration: 4s;
6
7  animation-timing-function: ease-in-out;
8  animation-delay: 2s;
9}

Animation iteration count

You can also define how many times you wish the animation to repeat using the animation-iteration-count property.

css
1div {
2  /* . . . */
3
4  animation-name: change-bg-color;
5  animation-duration: 4s;
6
7  animation-timing-function: ease-in-out;
8  animation-delay: 2s;
9
10  animation-iteration-count: 3;
11}
Open demo in new tab

Animation direction

The animation-direction property defines in which direction the animation will be played, and it accepts four different options:

  • normal: The animation is played forward.
  • reverse: The animation is played backward.
  • alternate: The animation is played forward first, then backward. Only works when animation-iteration-count is more than 1.
  • alternate-reverse: The animation is played backward first, then forward.
css
1div {
2  /* . . . */
3
4  animation-name: change-bg-color;
5  animation-duration: 4s;
6
7  animation-timing-function: ease-in-out;
8  animation-delay: 2s;
9
10  animation-iteration-count: 4;
11  animation-direction: alternate;
12}
Open demo in new tab

Animation fill mode

Lastly, the animation-fill-mode property determines how the element will be displayed before and after the animation is played. By default, the element will not retain any styles from the animation.

css
1@keyframes change-bg-color {
2  0% {
3    background-color: white;
4  }
5  25% {
6    background-color: bisque;
7  }
8  50% {
9    background-color: crimson;
10  }
11  100% {
12    background-color: darkviolet;
13  }
14}
15
16div {
17  /* . . . */
18
19  background-color: aqua;
20
21  animation-name: change-bg-color;
22  animation-duration: 4s;
23
24  animation-fill-mode: none;
25}
Open demo in new tab

When set to forwards, the element will retain the styles from the last keyframe of the animation after the animation is played.

When set to backwards, the element will adopt the styles from the first keyframe of the animation as soon as the animation is played.

When set to both, the element will retain the styles from the first keyframe before the animation starts (behaves like backwards) and the styles from the last keyframe after the animation is over (behaves like forwards).

The animation shorthand

Lastly, just like with the transitions, CSS offers a shorthand property called animation, which has the following syntax:

text
1animation: <animation_name> <animation_duration> <animation_timing_function> <animation_delay> <animation_iteration_count> <animation_direction>;
css
1div {
2  /* . . . */
3
4  animation: change-bg-color 4s ease-in-out 2s 4 alternate;
5}