The Complete Guide to CSS Keyframe Animations & Easing Physics
To create performant CSS animations, declare keyframe stops with '@keyframes' and apply them using 'animation: name duration timing-function iteration-count'. Always animate GPU-composited properties like 'transform' and 'opacity' rather than layout dimensions ('width', 'top') to guarantee smooth 60fps motion without CPU layout recalculations.
Step 1: The GPU Compositor: Why Some CSS Properties Lag
Browsers process CSS changes across three distinct rendering stages: Layout, Paint, and Composite. Animating layout properties like 'width', 'height', 'top', or 'margin' forces the browser to recalculate element geometry across the entire document on every single frame. In contrast, 'transform' and 'opacity' bypass Layout and Paint completely, executing directly on the GPU compositor at full refresh rate.
/* ❌ CPU-heavy: triggers layout and paint thrashing */
.laggy-motion {
animation: slideLag 1s infinite;
}
@keyframes slideLag {
0% { left: 0px; top: 0px; }
100% { left: 200px; top: 50px; }
}
/* GPU-accelerated: zero layout reflows */
.smooth-motion {
animation: slideSmooth 1s cubic-bezier(0.16, 1, 0.3, 1) infinite;
will-change: transform;
}
@keyframes slideSmooth {
0% { transform: translate3d(0, 0, 0); }
100% { transform: translate3d(200px, 50px, 0); }
}
Step 2: Mastering cubic-bezier() for Natural Spring and Bounce Physics
Linear animations feel mechanical because objects in nature possess momentum, friction, and inertia. A cubic-bezier curve takes four control points: 'cubic-bezier(x1, y1, x2, y2)'. By pushing the y-values above 1.0 (such as 'cubic-bezier(0.34, 1.56, 0.64, 1)'), you create realistic elastic overshoot and bounce physics without heavy JavaScript physics engines.
/* Elastic Pop & Overshoot */
.bounce-in {
animation: popIn 0.5s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
@keyframes popIn {
0% { transform: scale(0.6); opacity: 0; }
100% { transform: scale(1); opacity: 1; }
}
Step 3: How animation-fill-mode Solves the Abrupt Snapping Bug
A common beginner pitfall is watching an animation snap abruptly back to its initial state when it completes. Setting 'animation-fill-mode: forwards' instructs the browser to retain the computed CSS styles declared in the 100% (or 'to') keyframe stop indefinitely.
.slide-in-card {
/* Keeps final position instead of reverting */
animation: enterFromBottom 0.6s ease-out forwards;
}
@keyframes enterFromBottom {
from { transform: translateY(40px); opacity: 0; }
to { transform: translateY(0); opacity: 1; }
}
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
When building high-performance UI animations or particle systems, my golden rule is: never calculate layout triggers (like getBoundingClientRect() or offsetHeight) inside animation loops. Read measurements once upfront and use CSS transforms or Canvas for buttery 60fps.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
What is the difference between CSS transitions and CSS keyframe animations?
CSS transitions require a trigger (such as :hover, :focus, or a class toggle via JS) to interpolate between an initial state and a target state. CSS @keyframes animations can run automatically on page load, loop infinitely, and define multiple intermediate waypoint states (0%, 25%, 50%, 100%).
Why does will-change improve animation performance?
Declaring 'will-change: transform' hints to the browser engine ahead of time that the element will move, prompting the browser to promote it to its own isolated GPU compositor layer. This prevents repainting adjacent sibling elements during motion.
How do you pause a CSS keyframe animation?
You can pause any running keyframe animation in CSS using 'animation-play-state: paused'. This is commonly toggled on :hover, on user click, or when the user scrolls the element out of the active viewport.
How do you stop animations from triggering motion sickness in users?
Always respect accessibility preferences by wrapping movement in the '@media (prefers-reduced-motion: reduce)' media query, setting 'animation: none !important;' or reducing rapid scaling and displacement effects.
Can you animate CSS custom properties with keyframes?
Yes! When custom properties are registered using '@property' with a defined 'syntax' (such as '