Chapter 3.4โ˜• 16 min read

Animations & Keyframes

Create complex animations with @keyframes and animation properties. From loading spinners to page transitions.

01Understanding Animations

CSS animations let you create complex, multi-step animations using @keyframes and animation properties. Unlike transitions (which only animate between two states), animations can have multiple keyframes.

โฐ
Analogy: Just like the Nizam clock ticks with precision, CSS animations let you control every frame of an element's movement.
@keyframes bounce {
  0%   { transform: translateY(0); }
  50%  { transform: translateY(-20px); }
  100% { transform: translateY(0); }
}
.box { animation: bounce 2s infinite; }
02Animation Properties

Eight animation properties give you full control:

PropertyDescription
animation-nameName of the @keyframes animation.
animation-durationTime for one cycle (e.g., 2s, 300ms).
animation-timing-functionSpeed curve (ease, linear, ease-in-out, etc.).
animation-delayDelay before animation starts.
animation-iteration-countHow many times to repeat. Use infinite for endless.
animation-directionnormal, reverse, alternate, alternate-reverse.
animation-fill-modeStyles before/after: none, forwards, backwards, both.
animation-play-statePause/resume: running, paused.
/* Shorthand: name duration timing delay iteration direction fill-mode */
.box { animation: bounce 2s ease 0s infinite alternate both; }

/* Pause on hover */
.box:hover { animation-play-state: paused; }

/* Multiple animations */
.box { animation: bounce 2s infinite, fadeIn 1s ease-out; }
03Keyframes Syntax

Keyframes define the animation sequence. Use from/to or percentage steps:

/* Simple two-step */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Multi-step */
@keyframes pulse {
  0%   { transform: scale(1); }
  25%  { transform: scale(1.05); }
  50%  { transform: scale(1.1); }
  75%  { transform: scale(1.05); }
  100% { transform: scale(1); }
}

/* Loading spinner */
@keyframes spin { to { transform: rotate(360deg); } }

/* Float back and forth */
@keyframes float {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  33%      { transform: translateY(-10px) rotate(3deg); }
  66%      { transform: translateY(5px) rotate(-2deg); }
}
04Practical Examples

Real-world animation patterns:

/* Loading spinner */
.spinner { animation: spin 1s linear infinite; }

/* Pulse button */
.btn:hover { animation: pulse 1s infinite; }

/* Fade in on page load */
.text { animation: fadeIn 1.5s ease-out; }

/* Slide in from left */
@keyframes slideIn {
  from { transform: translateX(-100%); opacity: 0; }
  to   { transform: translateX(0); opacity: 1; }
}
.hero { animation: slideIn 0.8s ease-out; }

/* Staggered entrance โ€” delay per child */
.child:nth-child(1) { animation: fadeIn 0.5s ease-out both; }
.child:nth-child(2) { animation: fadeIn 0.5s ease-out 0.1s both; }
.child:nth-child(3) { animation: fadeIn 0.5s ease-out 0.2s both; }
05Exercise โ€” Animations

Practice animations below.

Try these experiments:

Done โ€” Key Points:

  • โœ… @keyframes define animation steps. Use % or from/to.
  • โœ… 8 animation properties: name, duration, timing, delay, iteration, direction, fill-mode, play-state.
  • โœ… infinite repeats forever. alternate goes back and forth.
  • โœ… animation-fill-mode: both keeps start/end styles before/after animation.
  • โœ… animation-play-state: paused on hover gives user control.
Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase