CSS animation property


The animation property in CSS is a shorthand property used to define animations for elements. It allows you to specify multiple aspects of an animation in one line, making it easier to manage and apply animations to elements.

Syntax

element { animation: [name] [duration] [timing-function] [delay] [iteration-count] [direction] [fill-mode] [play-state]; }

Here's a breakdown of each part:

  1. name: The name of the @keyframes animation to be applied. This defines the keyframes of the animation.

  2. duration: The length of time the animation takes to complete one cycle, specified in seconds (s) or milliseconds (ms).

  3. timing-function: Describes how the animation progresses over time. Common values include linear, ease, ease-in, ease-out, ease-in-out, and cubic-bezier functions.

  4. delay: The amount of time to wait before starting the animation, specified in seconds or milliseconds.

  5. iteration-count: The number of times the animation should repeat. It can be a specific number or infinite for an endless loop.

  6. direction: Specifies whether the animation should play forward, backward, or alternate. Values include normal, reverse, alternate, and alternate-reverse.

  7. fill-mode: Defines the styles applied to the element before and after the animation. Values include none, forwards, backwards, and both.

  8. play-state: Controls whether the animation is running or paused. Values are running and paused.

Example

@keyframes example { from { background-color: red; transform: translateX(0); } to { background-color: blue; transform: translateX(100px); } } .box { width: 100px; height: 100px; background-color: red; animation: example 2s ease-in-out 1s infinite alternate; }

Explanation:

  1. @keyframes example:

    • Defines the keyframes for the animation named example.
    • At the start (from), the background color is red, and the element is at its original position.
    • At the end (to), the background color changes to blue, and the element moves 100px to the right.
  2. .box:

    • Applies the example animation with the following settings:
      • 2s: The animation duration is 2 seconds.
      • ease-in-out: The animation timing function makes it start slow, speed up in the middle, and slow down at the end.
      • 1s: There is a 1-second delay before the animation starts.
      • infinite: The animation will repeat indefinitely.
      • alternate: The animation will alternate between forward and reverse directions on each iteration.

Key Properties Explained

  1. @keyframes:

    • Defines the animation states. You specify what happens at various points in the animation timeline (e.g., 0%, 50%, 100%).
  2. animation-name:

    • Refers to the name of the @keyframes animation.
    animation-name: example;
  3. animation-duration:

    • Sets how long the animation takes to complete.
    animation-duration: 2s;
  4. animation-timing-function:

    • Defines the speed curve of the animation.
    animation-timing-function: ease-in-out;
  5. animation-delay:

    • Specifies the delay before the animation starts.
    animation-delay: 1s;
  6. animation-iteration-count:

    • Sets how many times the animation should run.
    animation-iteration-count: infinite;
  7. animation-direction:

    • Specifies whether the animation should run forward, backward, or alternate.
    animation-direction: alternate;
  8. animation-fill-mode:

    • Determines how the element styles should be applied before and after the animation.
    animation-fill-mode: forwards;
  9. animation-play-state:

    • Controls whether the animation is running or paused.
    animation-play-state: paused;

Practical Example

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Animation Example</title> <style> @keyframes slide { from { transform: translateX(0); } to { transform: translateX(300px); } } .animated-box { width: 100px; height: 100px; background-color: orange; animation: slide 3s ease-in-out 1s infinite alternate; } </style> </head> <body> <div class="animated-box"></div> </body> </html>
  • Explanation:
    • The .animated-box element will move horizontally from its original position to 300px to the right, with a 3-second duration for each cycle.
    • The animation starts 1 second after the element appears and repeats indefinitely, alternating directions with each iteration.