CSS Keyframe Animations & Performance
Master multi-stage `@keyframes`, `animation-fill-mode`, compositor-driven performance, and modern scroll-driven animations.
1. Real-World Analogy: The Animation Flipbook Studio
Think of CSS keyframe animations like directing a professional stop-motion movie:
🎬 Keyframe Storyboards (`@keyframes`)
You draw the critical milestone frames (e.g. 0% start, 50% apex, 100% landing). The browser's animation engine automatically calculates and draws all 60 in-between frames (tweening)!
⏳ Fill Mode (Curtain Call States)
animation-fill-mode: forwards tells the actor to freeze in their final heroic pose when the play finishes, rather than snapping back to the dressing room instantly.
2. `@keyframes` Syntax & Animation Shorthand
The animation property binds a set of keyframes to an element and configures its playback mechanics:
/* 1. Define Keyframe Storyboard */
@keyframes pulseRadar {
0% {
transform: scale(0.95);
opacity: 0.8;
}
50% {
transform: scale(1.15);
opacity: 0.4;
}
100% {
transform: scale(0.95);
opacity: 0.8;
}
}
/* 2. Bind Animation Shorthand */
.radar-beacon {
/* animation: [name] [duration] [timing-func] [delay] [iteration-count] [direction] [fill-mode] */
animation: pulseRadar 2s ease-in-out 0s infinite normal both;
}
3. `animation-fill-mode` Demystified
Controls what styles apply to the element before the animation begins (during delay) and after it ends:
| Value | Before Animation (During Delay) | After Animation Finishes | Common Use Case |
|---|---|---|---|
none (Default) |
Original default CSS styles | Snaps back to original default styles | Transient visual blinks |
forwards |
Original default CSS styles | Retains styles from the final keyframe (100%) | Modal entry animations, slide-ins |
backwards |
Applies styles from the first keyframe (0%) immediately | Snaps back to original styles | Staggered list item entrances |
both |
Applies 0% keyframe during delay | Retains 100% keyframe upon finish | Production UI entry components |
4. Interactive Workbench: Live Pulse & Spinner Animations
Observe multi-step keyframe animations running smoothly on GPU Compositor threads:
/* 1. Loading Spinner Ring */
@keyframes spinLoop {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.spinner-ring {
width: 36px;
height: 36px;
border: 3px solid rgba(108, 76, 232, 0.2);
border-top-color: #6C4CE8;
border-radius: 50%;
animation: spinLoop 0.8s linear infinite;
}
/* 2. Pause on Hover */
.spinner-ring:hover {
animation-play-state: paused;
}
5. Modern Scroll-Driven Animations (CSS Animation Timeline)
Modern CSS allows keyframes to be linked directly to viewport scroll progress without writing JavaScript scroll listeners:
/* Progress Reading Bar linked to page scroll */
@keyframes growProgressBar {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
.scroll-progress-indicator {
position: fixed;
top: 0;
left: 0;
height: 4px;
width: 100%;
background: #6C4CE8;
transform-origin: left;
animation: growProgressBar auto linear;
animation-timeline: scroll(); /* Native browser scroll driver */
}
6. CSS Keyframe Animations Mastery Quiz Deck
Test your knowledge with 5 placement-ready interactive multiple choice questions:
🎯 Placement Interview Key Takeaways
- Q: What is the negative delay trick in CSS animations (e.g. `animation-delay: -2s`)?
Ans: A negative animation delay causes the animation to start immediately as if it had already been running for 2 seconds, allowing developers to offset staggered spinners and pulsing indicators in phase without initial pauses. - Q: Why should you avoid animating `box-shadow` or `border-radius` in infinite loops?
Ans: Modifyingbox-shadowforces the CPU to calculate Gaussian blurs and repaint pixels on every single frame, draining battery and causing severe mobile frame drops. Instead, animateopacityon a pre-rendered pseudo-element with the shadow! - Q: How do you respect user vestibular motion preferences with animations?
Ans: Always wrap infinite animations in@media (prefers-reduced-motion: no-preference) { ... }, or disable/substitute with instant fades for users with motion sensitivity.