The Mathematics Behind CSS Animations

CSS animations might seem like magic, but underneath they're driven by elegant mathematical principles. Understanding these foundations will help you create more sophisticated and performant animations.
The Geometry of Easing Functions#
Every CSS animation relies on easing functions to control the rate of change over time. The most common are cubic Bézier curves.
Cubic Bézier Curves#
A cubic Bézier curve is defined by four control points. For CSS, the curve is constrained to start at and end at , so we only specify the two middle control points:
Where ranges from 0 to 1, and , .
In CSS, we write this as:
.element {
transition: transform 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
}
The values (0.4, 0.0, 0.2, 1) represent the coordinates of and .
Common Easing Functions#
The built-in CSS easing functions are actually specific Bézier curves:
| Name | Bézier Values | Equation Behavior |
|---|---|---|
ease | (0.25, 0.1, 0.25, 1.0) | Slow start, fast middle, slow end |
ease-in | (0.42, 0, 1.0, 1.0) | Slow start: |
ease-out | (0, 0, 0.58, 1.0) | Slow end: |
ease-in-out | (0.42, 0, 0.58, 1.0) | Slow start and end |
Trigonometry in Animation#
Trigonometric functions create natural, oscillating motion that's perfect for breathing effects, waves, and pendulum-like animations.
Sine Waves#
The sine function produces smooth oscillation:
Where:
- is the amplitude (how far it moves)
- is the angular frequency ()
- is the phase offset
In CSS, we can approximate this with keyframes:
@keyframes breathe {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.1); }
}
For more precise control, use JavaScript:
function animate(element, amplitude, frequency) {
const omega = 2 * Math.PI * frequency;
function update(time) {
const t = time / 1000;
const y = amplitude * Math.sin(omega * t);
element.style.transform = `translateY(${y}px)`;
requestAnimationFrame(update);
}
requestAnimationFrame(update);
}
Circular Motion#
For circular paths, we use parametric equations:
This creates perfect circular motion with radius and angular velocity .
Physics-Based Animation#
The most realistic animations simulate physical forces.
Spring Dynamics#
A spring follows Hooke's Law combined with damping:
Where:
- is the spring constant (stiffness)
- is displacement from equilibrium
- is the damping coefficient
- is velocity
The solution for an underdamped spring is:
Where is the damping ratio and is the damped frequency.
function springAnimation(stiffness, damping, mass) {
const omega0 = Math.sqrt(stiffness / mass);
const gamma = damping / (2 * mass);
const omegaD = Math.sqrt(omega0 ** 2 - gamma ** 2);
return function(t, initialDisplacement) {
return initialDisplacement *
Math.exp(-gamma * t) *
Math.cos(omegaD * t);
};
}
Gravity and Projectile Motion#
For falling or bouncing objects:
Where is gravitational acceleration.
Interpolation Mathematics#
Smooth transitions between values use interpolation functions.
Linear Interpolation (Lerp)#
The simplest interpolation:
const lerp = (a, b, t) => a + (b - a) * t;
Smoothstep#
For smoother transitions, we use polynomial interpolation:
This has zero derivatives at and , creating smooth acceleration and deceleration.
An even smoother variant is smootherstep:
const smoothstep = t => t * t * (3 - 2 * t);
const smootherstep = t => t * t * t * (t * (6 * t - 15) + 10);
Practical Application: Custom Easing#
Let's create a bounce easing function. The math involves a decaying sinusoid:
function bounceEasing(t, bounces = 4, decay = 6) {
const n = bounces;
const k = decay;
return 1 - Math.abs(Math.cos(n * Math.PI * t)) * Math.exp(-k * t);
}
Conclusion#
Understanding the mathematics behind animations transforms you from someone who copies easing values to someone who crafts intentional motion. Whether you're using Bézier curves for subtle UI feedback or physics simulations for game-like interactions, these mathematical foundations are your toolkit for creating exceptional user experiences.
The beauty of animation math is that it mirrors the natural world—springs, pendulums, and waves all follow these same equations, which is why mathematically-driven animations feel so satisfying and natural.



