CSS Tutorial

Animate Keyframes

Define Keyframe Rules

  • Identify the property you want to animate (e.g., opacity, transform).
  • Create keyframes to specify the property's values at specific points in time.

Create Animation Rule

  • Use the @keyframes rule to define the animation name and its keyframes.
  • Set the animation-name property on the element you want to animate.

Optional Steps: Customize Animation

  • Animation duration: Set the duration of the animation (e.g., 1s).
  • Animation timing function: Control the speed and acceleration of the animation (e.g., linear, ease-in).
  • Animation delay: Delay the start of the animation (e.g., 0.5s).
  • Animation iteration count: Set the number of times the animation should repeat (e.g., infinite).
  • Animation direction: Control the direction of the animation (e.g., normal, reverse).

Example:

@keyframes fade-in {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

.fade-in {
  animation-name: fade-in;
  animation-duration: 1s;
  animation-timing-function: ease-in;
  animation-direction: normal;
}

Steps to Animate:

  • Choose an animation: Decide what you want the element to do.
  • Set the animation name: Give your animation a unique name.
  • Define the animation steps: Specify where the element will be at different times.
  • Apply the animation to the element: Use the animation name on the element.

Customization:

  • Duration: How long the animation takes.
  • Timing: How the animation flows.
  • Delay: When the animation starts.
  • Repeat: How many times the animation plays.
  • Direction: Which way the animation runs.
Example:
@keyframes fade-in {
  0% { opacity: 0; }
  100% { opacity: 1; }
}

#my-element {
  animation: fade-in 1s ease-in;
}

This animation fades in the element with the ID "my-element" over 1 second.