01. CSS Keyframe Animations (@keyframes, direction, fill-mode)

🟡 Intermediate

1. Definition

@keyframes rules define multi-step animation keyframes (from 0% to 100%) controlled by properties like animation-name, duration, iteration-count, direction, and fill-mode.

2. Why It Is Used

Keyframe animations run automatically or on triggers, producing continuous looping loaders, pulsing notifications, and entrance effects without JavaScript.

3. How It Works

The browser interpolates property states between keyframe percentages (e.g. 0%, 50%, 100%) across the specified duration.

4. Syntax

@keyframes pulse {
  0% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.05); opacity: 0.8; }
  100% { transform: scale(1); opacity: 1; }
}

.pulsing-badge {
  animation: pulse 2s infinite ease-in-out;
}

5. Animation Sub-Properties Reference Table

PropertyFunctionExample Values
animation-fill-modeControls element styling before start and after endforwards, backwards, both
animation-directionControls playback directionnormal, reverse, alternate
animation-iteration-countRepeat count1, 5, infinite

6. Beginner Example

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
.fade-box {
  animation: fadeIn 1s forwards;
}

7. Intermediate Example

@keyframes spinner {
  to { transform: rotate(360deg); }
}
.loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid #e2e8f0;
  border-top-color: #2563eb;
  border-radius: 50%;
  animation: spinner 0.8s linear infinite;
}

8. Advanced Staggered Entrance Example

@keyframes slideUpFade {
  0% { opacity: 0; transform: translateY(20px); }
  100% { opacity: 1; transform: translateY(0); }
}

.staggered-item {
  animation: slideUpFade 0.5s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

9. Line-by-Line Code Explanation

  • animation-fill-mode: forwards;: Retains the final 100% keyframe state styles after the animation completes.

10. Expected Terminal / Browser Output

A loading spinner rotating continuously at 60fps and entrance elements sliding up into view.

11. Common Mistakes & Pitfalls

  • Forgetting animation-fill-mode: forwards: Causing animated entrance elements to snap back to un-animated initial states upon completion.

12. Important Rules & Limitations

  • Respect prefers-reduced-motion media queries for users with motion sensitivity.

13. Multilingual Explanation: English

@keyframes creates multi-step animations. Use forwards to retain the end keyframe state.

14. Multilingual Explanation: Hindi (Roman Script)

@keyframes se multi-step animations bante hain. Final state ko hold karne ke liye animation-fill-mode: forwards use karein.

15. Multilingual Explanation: Marathi (Roman Script)

@keyframes mule multi-stage animations tayar karta येतात. End state maintain karnyasathi forwards vapara.

16. Practice Questions

  1. What is the function of animation-fill-mode: forwards?

17. Practical Coding Challenge

Create an @keyframes pulse-opacity animation that fades between 0.4 and 1.0 opacity infinitely.

18. Related Topics