agyanadda@gmail.com

CSS Animations

Advanced CSS 1 min read Updated Dec 1, 2026 Topic 36 of 39

Animations run by themselves, without hover, and can have many steps.

1. Define keyframes

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-20px); }
}

2. Apply it

animation: bounce 1s ease-in-out infinite;

Animation properties

  • animation-name, animation-duration
  • animation-iteration-count — a number or infinite
  • animation-direction — normal, reverse, alternate
  • animation-delay, animation-timing-function
  • animation-fill-mode: forwards; — keep the last frame

Respect users who prefer less motion with @media (prefers-reduced-motion: reduce).

Example

Example — Bounce and spinner HTML
<style>
  @keyframes bounce { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-25px); } }
  @keyframes spin { to { transform: rotate(360deg); } }
  .ball { width: 50px; height: 50px; border-radius: 50%; background: tomato; margin: 30px; animation: bounce 1s ease-in-out infinite; }
  .spinner { width: 40px; height: 40px; border: 5px solid #ddd; border-top-color: #1f386b; border-radius: 50%; animation: spin .8s linear infinite; margin: 20px; }
</style>

<div class="ball"></div>
<div class="spinner"></div>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which rule defines the steps of an animation?

Exercise 2 Fill in the Blank

animation-iteration-count: ___; repeats forever.

Exercise 3 True / False

CSS animations need JavaScript to start.

Frequently Asked Questions

Use a transition for simple A-to-B changes on hover or class change. Use an animation for loops or multi-step motion.