CSS propertyAnimations
animation-duration
Sets how long one animation cycle takes.
- Initial value
0s- Inherited
- No
- Animatable
- no
- Applies to
- all elements and the ::before and ::after pseudo-elements
- JavaScript
element.style.animationDuration = "2s"
Example
#CSS
.progress {
animation: fill 2.5s ease-out forwards;
}
@keyframes fill {
from { width: 0; }
to { width: 72%; }
}Example HTML
<style>@keyframes fill-bar { from { width: 0; } to { width: 72%; } }</style>
<div style="height: 10px; background: #262626; border-radius: 99px; overflow: hidden;">
<div style="height: 100%; background: linear-gradient(90deg, #7c3aed, #0ea5e9); animation: fill-bar 2.5s ease-out forwards;"></div>
</div>Preview
Definition and usage
#The animation-duration property sets the time of a single run of the animation, from the first to the last keyframe. You provide it in seconds or milliseconds.
The default is zero, so an animation with only a name will not be visible. This is the most common reason an animation βdoes not workβ.
With multiple iterations, the total animation time is the duration multiplied by the iteration count. The delay counts only once, before the first cycle.
Syntax
#Syntax
animation-duration: <time> [, <time>]*;Values
#Values in practice
#0.4s
Fast movement back and forth.
.demo { animation-duration: 0.4s; }1.5s
A moderate pace.
.demo { animation-duration: 1.5s; }4s
Slow, calm movement.
.demo { animation-duration: 4s; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- If an animation does not start, first check that it has a duration greater than zero.
- Entrance effects usually last 300-600 ms. Longer ones delay access to the content.
Related entries
#Practise it in lessons
#Spotted a mistake or missing an example? Write to us.