β±οΈ Mastering animation-duration in CSS
π What Is animation-duration?
The animation-duration property defines how long an animation takes to complete one full cycle. It works together with animation-name to control the timing of your CSS animations. Without animation-duration, an animation will not run! β
>>βDuration determines the rhythm of your animation β too fast or too slow changes everything.β
π§© Basic Syntax
Syntax
animation-duration: 2s; /* seconds */
animation-duration: 500ms; /* milliseconds */CSS supports both seconds (s) and milliseconds (ms).
π¬ Example with Keyframes
Keyframes + Duration
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.box {
animation-name: fadeIn;
animation-duration: 1.5s;
}β The animation will last 1.5 seconds from start to end.
β³ Using Different Durations
Slow Animation
animation-duration: 5s;Quick Animation
animation-duration: 200ms;Use longer durations for dramatic effects and shorter durations for micro-interactions.
π― Multiple Animations (Comma Separated)
Multiple Durations
animation-name: fadeIn, slideUp;
animation-duration: 2s, 400ms;β The first duration applies to the first animation
β The second duration applies to the second animation
π‘ animation-duration in Shorthand
Shorthand Format
animation: fadeIn 2s ease-in-out;| Shorthand Part | Meaning |
|---|---|
| fadeIn | animation-name |
| 2s | animation-duration |
| ease-in-out | animation-timing-function |
π¨ Real-World Examples
1οΈβ£ Fade-in Animation
Fade-In
@keyframes fade {
from { opacity: 0; }
to { opacity: 1; }
}
.element {
animation: fade 1s ease-out;
}2οΈβ£ Button Hover Pop
Hover Pop
@keyframes pop {
0% { transform: scale(1); }
50% { transform: scale(1.1); }
100% { transform: scale(1); }
}
button:hover {
animation-duration: 300ms;
animation-name: pop;
}3οΈβ£ Smooth Slide Effect
Slide Animation
@keyframes slide {
from { transform: translateX(-20px); }
to { transform: translateX(0); }
}
.card {
animation: slide 0.8s ease-out;
}π§ Tips for Choosing the Right Duration
- Use 150β300ms for small UI micro-interactions β‘
- Use 0.5β1.5s for smooth visual transitions β¨
- Use 2β5s for dramatic, storytelling animations π
- Match animation speed with user expectations
- Avoid overly long repetitive animations
π Debugging Tips
- Ensure animation-name is declared correctly
- Make sure animation-duration is not missing (default = 0)
- Use DevTools β "Animations" panel to inspect timeline
- Check vendor prefixes only for very old browsers
π Helpful Resources
>>βAnimations feel great when the timing feels natural β duration is the key to that natural flow.β β±οΈβ¨