CSS animation-duration Property
The animation-duration property specifies how long one iteration of a CSS keyframe animation takes to complete.
For ordinary time-based animations, durations are expressed in seconds or milliseconds. Longer durations produce slower animations, while shorter durations produce faster ones. The property also supports auto, which is particularly useful with scroll-driven animation timelines.
animation-duration Syntax
selector {
animation-duration: 2s;
}
Use a non-negative time value in seconds (s) or milliseconds (ms), or specify the keyword auto.
.fast {
animation-duration: 500ms;
}
.slow {
animation-duration: 5s;
}
.automatic {
animation-duration: auto;
}
For multiple animations, specify a comma-separated list of durations.
.multiple {
animation-duration: 1s, 2s, 3s;
}
The property also accepts the global CSS values inherit, initial, revert, revert-layer, and unset.
Property Values
| Value | Description |
|---|---|
0s |
Specifies a zero-duration animation. This is the initial value. |
2s |
Sets the duration of one animation iteration to two seconds. |
500ms |
Sets the duration to 500 milliseconds, equivalent to half a second. |
auto |
Uses the timeline's duration. For ordinary time-based animations, it resolves to zero seconds. For scroll-driven animations, it fills the relevant timeline range. |
1s, 3s |
Assigns separate durations to multiple animations in their listed order. |
Time values use the CSS <time> data type. Negative durations are invalid. Time units are required, including for zero.
The property applies to all elements, is not inherited, and has an initial value of auto in the current specification. Its computed value is a list of time values or auto keywords.
The animation-duration property itself is not animatable.
How animation-duration Works
The @keyframes at-rule defines the changes that occur during an animation, while animation-duration determines how long each iteration takes.
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.box {
animation-name: fade-in;
animation-duration: 2s;
animation-timing-function: linear;
}
In this example, the element changes from transparent to fully visible over two seconds.
Increasing the duration to 4s makes the same animation take twice as long. Reducing it to 1s makes the animation complete twice as quickly.
The duration does not determine how many times an animation repeats. That behavior is controlled by animation-iteration-count.
Seconds and Milliseconds
CSS animation durations can be written in seconds or milliseconds. Both units describe the same type of time value.
| Seconds | Milliseconds |
|---|---|
0.25s |
250ms |
0.5s |
500ms |
1s |
1000ms |
2s |
2000ms |
5s |
5000ms |
One second equals 1000 milliseconds. You can use either unit based on which makes the declaration easier to read.
.button-effect {
animation-duration: 250ms;
}
.loading-effect {
animation-duration: 1.5s;
}
Short durations are often appropriate for small interface effects, while longer durations may be useful for more noticeable visual changes.
Zero-Duration Animations
A duration of 0s means the animation has no elapsed time between its starting and ending keyframes.
.box {
animation: fade-in 0s forwards;
}
The animation does not visibly transition between keyframe values. However, a zero-duration animation can still apply keyframe styles according to its fill mode and can generate animation events.
For example, forwards can preserve the animation's ending keyframe styles after the zero-duration animation completes.
If an animation has a positive delay and a zero duration, it waits for the delay and then completes without a visible transition.
The auto Value
The auto keyword allows the animation duration to be determined by its timeline.
For ordinary time-based animations, auto behaves like 0s. For scroll-driven animations, it allows the animation to span the relevant scroll or view timeline range.
.scroll-progress {
animation-name: reveal;
animation-duration: auto;
animation-timing-function: linear;
animation-timeline: view();
}
@keyframes reveal {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
In supporting browsers, this animation progresses according to the element's view timeline instead of a fixed number of seconds.
The animation-timeline property determines the timeline, while animation-duration: auto allows the animation to use the timeline's available duration.
Scroll-driven animation support varies by browser version. Use ordinary time-based durations when broad compatibility is required.
Multiple Animation Durations
When an element has several animations, you can assign a separate duration to each one.
.animated-element {
animation-name: fade-in, slide-in;
animation-duration: 1s, 3s;
}
The fade-in animation lasts one second, while slide-in lasts three seconds.
Durations are matched to animations in the order specified by animation-name.
If fewer duration values are provided than animations, the browser repeats the duration list as needed.
.three-animations {
animation-name: fade, move, rotate;
animation-duration: 1s, 2s;
}
Here, fade uses 1s, move uses 2s, and rotate uses 1s.
This allows multiple animation effects to run at different speeds on the same element.
animation-duration Example
The following example compares three animations with different durations. Each square moves the same distance, but the duration determines how quickly it travels.
<div class="duration-demo">
<div class="duration-row">
<strong>1s</strong>
<div class="duration-track"><div class="duration-box fast"></div></div>
</div>
<div class="duration-row">
<strong>2s</strong>
<div class="duration-track"><div class="duration-box medium"></div></div>
</div>
<div class="duration-row">
<strong>4s</strong>
<div class="duration-track"><div class="duration-box slow"></div></div>
</div>
</div>
.duration-demo {
display: grid;
gap: 12px;
}
.duration-row {
display: flex;
align-items: center;
gap: 12px;
}
.duration-row strong {
flex: 0 0 35px;
}
.duration-track {
flex: 1;
min-width: 0;
height: 40px;
padding: 5px;
background-color: #f1f3f5;
border: 1px solid #ccc;
box-sizing: border-box;
}
.duration-box {
width: 30px;
height: 28px;
background-color: #1769aa;
animation-name: duration-move;
animation-timing-function: linear;
animation-iteration-count: infinite;
animation-direction: alternate;
}
.duration-box.fast {
animation-duration: 1s;
}
.duration-box.medium {
animation-duration: 2s;
}
.duration-box.slow {
animation-duration: 4s;
}
@keyframes duration-move {
from {
transform: translateX(0);
}
to {
transform: translateX(100px);
}
}
@media (prefers-reduced-motion: reduce) {
.duration-box {
animation: none;
}
}
All three squares use the same keyframes and linear timing function. The first completes each iteration in one second, the second in two seconds, and the third in four seconds.
The one-second animation moves fastest, while the four-second animation moves slowest. Because all three use alternate, they reverse direction at the end of each iteration.
If reduced motion is enabled, the squares remain stationary.
animation-duration vs. animation-delay
These two properties both use time values, but they control different parts of an animation.
| Property | Purpose |
|---|---|
animation-duration |
Specifies how long one iteration of the animation takes. |
animation-delay |
Specifies how long the animation waits before starting or how far into its timeline it begins. |
.box {
animation-name: fade-in;
animation-duration: 2s;
animation-delay: 1s;
}
For this ordinary time-based animation, the element waits one second and then plays the animation over two seconds.
When using the animation shorthand, the first time value represents the duration and the second represents the delay.
.box {
animation: fade-in 2s ease 1s;
}
Important Considerations
Keep the following points in mind when using animation-duration:
- Time units: Use seconds or milliseconds for explicit durations. Unitless values, including
0, are invalid. - Negative values: Negative durations are invalid and cause the declaration to be ignored.
- Zero duration: A zero-duration animation does not visibly interpolate between keyframes, but it can still affect styles and generate animation events.
- Iteration count: The duration applies to each iteration, not automatically to the entire repeating animation.
- Timing functions: The
animation-timing-functionproperty controls the pacing of an animation within its duration. - Shorthand declarations: The
animationshorthand can specify the duration directly, as inanimation: fade-in 2s ease;. - Accessibility: Use
prefers-reduced-motionto reduce or disable significant movement when requested.
Choose durations that make changes understandable without making users wait unnecessarily. Short interface effects generally need less time than larger visual sequences.
Browser Support
The animation-duration property has Baseline Widely available status and has been supported across major browser engines since September 2015, including Chrome, Edge, Firefox, and Safari.
Time-based durations are broadly supported. The auto value and scroll-driven animation behavior may require newer browser versions.
Visit Can I Use? to check current browser compatibility.
Specifications
The animation-duration property is defined in the CSS Animations specifications. CSS Animations Level 2 extends its behavior to support newer animation timelines.
