We use cookies to enhance your experience on the site
CodeWorlds

Animation Shorthand

In CSS, we can use shorthand to define multiple animation properties at once. The animation shorthand allows you to define different aspects of an animation in a single line, which can make code more concise and readable.

The animation Property

The animation property is a shorthand that combines the following properties:

  • animation-name
  • animation-duration
  • animation-timing-function
  • animation-delay
  • animation-iteration-count
  • animation-direction
  • animation-fill-mode
  • animation-play-state

Syntax

Below is the syntax of the animation property, with an example:

1.animation-example {
2  animation: slide 3s ease-in 1s 2 alternate both paused;
3}

This shorthand can be broken down into its corresponding parts:

  • slide: animation name (corresponds to animation-name).
  • 3s: animation duration (corresponds to animation-duration).
  • ease-in: animation timing function (corresponds to animation-timing-function).
  • 1s: animation delay (corresponds to animation-delay).
  • 2: number of animation repetitions (corresponds to animation-iteration-count).
  • alternate: animation direction (corresponds to animation-direction).
  • both: animation fill mode (corresponds to animation-fill-mode).
  • paused: animation play state (corresponds to animation-play-state).

Notes

  • Values must appear in a specific order to be correctly interpreted.
  • Not all properties need to be defined; you can omit some of them, but values must be given in the correct order.

Example

Here is a more detailed example of the animation shorthand:

1@keyframes slide {
2  0% { transform: translateX(0); }
3  100% { transform: translateX(100px); }
4}
5
6.slide-element {
7  animation: slide 2s ease-in-out 0s infinite alternate;
8}

The element with the slide-element class will move left and right infinitely, with an ease-in-out effect.

Animation shorthand in CSS allows you to define multiple animation properties in a single line, making code management and reading easier. Using the animation property can significantly speed up the process of creating complex animations while maintaining code clarity.

Go to CodeWorlds