#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
5 min read
Question 15 of 49medium

How Do CSS Animations Work?

Understanding keyframe animations in CSS.

What You'll Learn

  • Keyframe syntax
  • Animation properties
  • Multiple animations

Keyframe Animation

styles.cssCSS
@keyframes slideIn {
  from {
    transform: translateX(-100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

/* Or use percentages */
@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-20px); }
}

Animation Properties

styles.cssCSS
.element {
  animation-name: slideIn;
  animation-duration: 0.5s;
  animation-timing-function: ease-out;
  animation-delay: 0s;
  animation-iteration-count: 1; /* or infinite */
  animation-direction: normal; /* reverse, alternate */
  animation-fill-mode: forwards; /* none, backwards, both */

  /* Shorthand */
  animation: slideIn 0.5s ease-out forwards;
}

Multiple Animations

styles.cssCSS
.element {
  animation:
    slideIn 0.5s ease-out,
    fadeIn 0.3s ease;
}