β³ Mastering animation-timing-function in CSS
π What Is animation-timing-function?
The animation-timing-function property controls how an animation progresses over time. Instead of moving at a constant speed, elements can speed up, slow down, bounce, or curve smoothly during the animation. This creates natural, realistic motion. π¬β¨
>>βThe timing function defines the personality of your animation.β
π§© Basic Syntax
Syntax
animation-timing-function: ease;
animation-timing-function: linear;
animation-timing-function: ease-in-out;π¨ Common Timing Functions
| Value | Description | Motion Feel |
|---|---|---|
| linear | No acceleration | β‘ Constant speed |
| ease | Starts slow β speeds up β slows down | π― Default smooth motion |
| ease-in | Starts slow β speeds up | ποΈ Accelerating |
| ease-out | Starts fast β slows down | π¬ Decelerating |
| ease-in-out | Slow β fast β slow | π Smooth flowing |
| step-start | Jumps immediately | βΉοΈ Instant switch |
| step-end | Jumps at the end | βοΈ Snap at finish |
π§ Advanced: cubic-bezier()
For full control, CSS allows you to define a custom timing curve using cubic-bezier(). This enables cinematic, highly polished animations. ποΈ
Custom Bezier
animation-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);β¨ Tools like cubic-bezier.com help visualize and build curves.
π§± Using timing function with @keyframes
Example Animation
@keyframes slide {
from { transform: translateX(-50px); }
to { transform: translateX(0); }
}
.box {
animation-name: slide;
animation-duration: 1s;
animation-timing-function: ease-out;
}β The object starts fast and eases gently into place.
π― Multiple Timing Functions (Comma-separated)
Multiple Animations
animation-name: fade, move;
animation-duration: 1s, 2s;
animation-timing-function: ease-in, linear;β Each animation uses its corresponding timing function.
π¨ Real-World Examples
1οΈβ£ Bouncing Effect
Bounce
.ball {
animation: drop 0.7s cubic-bezier(.28,.84,.42,1.02);
}2οΈβ£ Smooth Fade
Smooth Fade-in
.fade {
animation: fadeIn 1.2s ease-in-out;
}3οΈβ£ Loading Pulse
Pulse
.pulse {
animation: pulse 0.5s linear infinite;
}π‘ animation Timing in Shorthand
Shorthand Example
animation: fadeIn 1.5s ease-in 0.2s forwards;| Shorthand Part | Meaning |
|---|---|
| fadeIn | animation-name |
| 1.5s | animation-duration |
| ease-in | animation-timing-function |
| 0.2s | animation-delay |
| forwards | animation-fill-mode |
π§ Tips for Choosing the Right Timing Function
- Use ease-out for UI elements entering the screen smoothly
- Use ease-in for exiting animations
- Use linear for consistent-motion elements (spinners, progress bars)
- Use ease-in-out for natural, soft transitions
- Use cubic-bezier() for custom feel β especially in production UI animations
π Debugging Tips
- Check if the animation is actually running (duration must be > 0)
- Use browser DevTools β Animations panel to visualize curves
- Compare different easing functions to see which feels best
π Helpful Resources
>>βGreat animations arenβt about movement β theyβre about timing.β β³π«