CSS animation-timeline Property
The animation-timeline property specifies the timeline used to control the progress of a CSS animation.
By default, CSS animations progress according to elapsed time. The animation-timeline property can instead connect an animation to scrolling or to an element's movement through a viewing area. This makes it possible to create scroll progress indicators, content reveals, and other scroll-driven effects without JavaScript.
animation-timeline Syntax
selector {
animation-timeline: view();
}
The property accepts a default timeline, a named timeline, or an anonymous scroll or view timeline.
.default-animation {
animation-timeline: auto;
}
.no-timeline {
animation-timeline: none;
}
.scroll-animation {
animation-timeline: scroll();
}
.view-animation {
animation-timeline: view();
}
.named-animation {
animation-timeline: --page-scroll;
}
Anonymous timelines can also specify a scroll direction or axis.
.vertical-scroll {
animation-timeline: scroll(nearest block);
}
.horizontal-scroll {
animation-timeline: scroll(nearest inline);
}
.vertical-view {
animation-timeline: view(block);
}
.horizontal-view {
animation-timeline: view(inline);
}
For multiple animations, use a comma-separated list of timelines.
.multiple {
animation-timeline: scroll(), view();
}
The property also accepts the global CSS values inherit, initial, revert, revert-layer, and unset.
Property Values
| Value | Description |
|---|---|
auto |
Uses the document's default timeline, which normally progresses with elapsed time. This is the initial value. |
none |
Does not associate the animation with a timeline. |
scroll() |
Creates an anonymous scroll progress timeline based on the nearest ancestor scroll container's block-axis scrolling. |
view() |
Creates an anonymous view progress timeline based on the animated element's position in its nearest ancestor scroll container. |
--page-scroll |
References a named timeline declared using scroll-timeline-name or view-timeline-name. |
scroll(root) |
Uses the root document scroll container as the source of scroll progress. |
scroll(self inline) |
Uses the element's own scroll container and its inline axis. |
view(inline) |
Tracks the animated element's visibility progress along the inline axis. |
The property applies to all elements, including ::before and ::after pseudo-elements. It is not inherited, its initial value is auto, and it is not itself animatable.
Its computed value is the specified timeline list, with the appropriate computed representation of any timeline functions.
How animation-timeline Works
A CSS animation requires keyframes and an associated timeline. The timeline determines how animation progress advances.
Time-Based Animations
With the default auto value, an animation normally progresses according to elapsed time and its duration.
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.box {
animation-name: fade-in;
animation-duration: 2s;
animation-timeline: auto;
}
The animation progresses over two seconds without requiring the user to scroll.
Scroll-Driven Animations
When a scroll or view timeline is selected, the animation's progress is associated with scrolling instead of ordinary elapsed time.
.box {
animation-name: fade-in;
animation-duration: auto;
animation-timing-function: linear;
animation-fill-mode: both;
animation-timeline: view();
}
The animation progresses as the element moves through its scroll container's viewing area. Scrolling backward can also move the animation backward through its keyframes.
For scroll-driven animations, animation-duration: auto is commonly used so the animation's duration is determined by the timeline and its attachment range.
If the selected timeline is inactive or cannot be resolved, the animation does not progress normally.
Using the scroll() Function
The scroll() function creates an anonymous scroll progress timeline. Animation progress is tied to the scroll position of a scroll container.
.progress-bar {
animation-name: fill-progress;
animation-duration: auto;
animation-timing-function: linear;
animation-fill-mode: both;
animation-timeline: scroll();
}
@keyframes fill-progress {
from {
transform: scaleX(0);
}
to {
transform: scaleX(1);
}
}
As the associated scroll container moves from its starting scroll position to its ending scroll position, the animation progresses from its first keyframe to its last.
Scroll Container Selection
The scroll() function can select a scroll container using one of the following values:
| Value | Description |
|---|---|
nearest |
Uses the nearest ancestor scroll container. This is the default. |
root |
Uses the root document scroll container. |
self |
Uses the element itself as the scroll container. |
Scroll Axis Selection
You can also select the axis used to measure scrolling.
| Value | Description |
|---|---|
block |
Uses the block axis, which is usually vertical in horizontal writing modes. This is the default. |
inline |
Uses the inline axis, which is usually horizontal in horizontal writing modes. |
x |
Uses the physical horizontal axis. |
y |
Uses the physical vertical axis. |
.horizontal-progress {
animation-timeline: scroll(nearest x);
}
.page-progress {
animation-timeline: scroll(root block);
}
A scroll progress timeline generally needs a scrollable range along the selected axis to provide meaningful progress.
Using the view() Function
The view() function creates an anonymous view progress timeline. It tracks the animated element's position as it moves through the viewing area of its nearest ancestor scroll container.
@keyframes reveal {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.reveal-box {
animation-name: reveal;
animation-duration: auto;
animation-timing-function: linear;
animation-fill-mode: both;
animation-timeline: view();
animation-range: entry;
}
In this example, the animation progresses while the element enters the viewing area.
View Timeline Axes
By default, view() uses the block axis. You can specify block, inline, x, or y.
.vertical-reveal {
animation-timeline: view(block);
}
.horizontal-reveal {
animation-timeline: view(inline);
}
Adjusting the Viewing Area
The view() function also accepts inset values that adjust the boundaries used to calculate view progress.
.reveal-box {
animation-timeline: view(block 20%);
}
This applies a 20% inset at both ends of the selected axis, changing when the element reaches the timeline's entry and exit boundaries.
Two inset values can be supplied to control the start and end sides independently.
.reveal-box {
animation-timeline: view(block 10% 25%);
}
Use view() for effects that depend on an element's visibility or movement through a scroll container rather than the scroll position alone.
Named Animation Timelines
Instead of creating an anonymous timeline directly in animation-timeline, you can define a named scroll or view timeline and reference it from an animation.
Named Scroll Timeline
.scroll-container {
height: 300px;
overflow-y: auto;
scroll-timeline-name: --content-scroll;
scroll-timeline-axis: block;
}
.progress-bar {
animation: fill-progress linear both;
animation-timeline: --content-scroll;
}
The scroll-timeline-name property creates a named timeline from the scroll container. The animated element references that timeline using --content-scroll.
The timeline must be available to the animated element through the timeline lookup and scoping rules. For example, a descendant of the scroll container can reference its named timeline.
Named View Timeline
.article-section {
view-timeline-name: --section-view;
view-timeline-axis: block;
}
.article-section .reveal-content {
animation: reveal linear both;
animation-timeline: --section-view;
animation-range: entry;
}
The view-timeline-name property creates a named view timeline for the section. The descendant animation uses that timeline rather than tracking its own visibility.
Named timelines are useful when the element supplying the timeline differs from the element being animated.
Timeline names are custom identifiers beginning with two hyphens, such as --content-scroll or --section-view. They are not CSS custom properties and do not require a var() function.
Controlling Animation Ranges
The animation-timeline property selects the timeline, while the animation-range shorthand controls which portion of that timeline is used for the animation.
.reveal-box {
animation: reveal linear both;
animation-timeline: view();
animation-range: entry 0% entry 100%;
}
Here, the animation begins at the start of the element's entry range and reaches its final keyframe at the end of that range.
You can also specify the start and end boundaries separately.
.reveal-box {
animation-range-start: entry 25%;
animation-range-end: exit 75%;
}
This extends the attachment range from 25% of the entry interval to 75% of the exit interval.
Common named view timeline ranges include cover, contain, entry, exit, entry-crossing, and exit-crossing.
Scroll progress timelines commonly use percentage attachment points, such as animation-range: 20% 80%;.
Multiple Animation Timelines
An element can use multiple animations, with each animation associated with a different timeline.
.animated-element {
animation-name: fade-in, slide-in;
animation-duration: auto;
animation-timing-function: linear;
animation-fill-mode: both;
animation-timeline: view(), scroll(root);
}
The fade-in animation follows the element's view progress timeline. The slide-in animation follows the root scroll progress timeline.
Timeline values are matched to animations in the order specified by animation-name.
If fewer timeline values are provided than animations, the list repeats as needed.
.three-animations {
animation-name: fade, move, rotate;
animation-timeline: view(), scroll(root);
}
In this example, fade uses view(), move uses scroll(root), and rotate uses view().
Using none for one entry leaves that animation without an associated timeline.
.mixed-animations {
animation-name: fade, move;
animation-timeline: none, view();
}
Here, the fade animation has no timeline, while move uses a view progress timeline.
animation-timeline Example
The following example demonstrates a scroll progress timeline and a view progress timeline inside the same scrollable container.
The progress bar fills as the container scrolls. The blue square moves and fades into view as its section enters the viewing area.
<div class="timeline-demo" tabindex="0" aria-label="Scrollable animation timeline demonstration">
<div class="timeline-progress"></div>
<div class="timeline-spacer">Scroll down to see the animation</div>
<div class="timeline-item">
<strong>view() animation</strong>
<div class="timeline-box"></div>
</div>
<div class="timeline-spacer">Continue scrolling</div>
<div class="timeline-spacer">End of example</div>
</div>
.timeline-demo {
position: relative;
height: 250px;
overflow-y: auto;
border: 1px solid #ccc;
background-color: #f8f9fa;
}
.timeline-progress {
position: sticky;
top: 0;
z-index: 1;
height: 6px;
background-color: #1769aa;
transform: scaleX(0);
transform-origin: left;
}
.timeline-spacer {
min-height: 220px;
display: flex;
align-items: center;
justify-content: center;
padding: 15px;
box-sizing: border-box;
color: #555;
text-align: center;
}
.timeline-item {
min-height: 150px;
padding: 15px;
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
background-color: #fff;
}
.timeline-item strong {
display: block;
margin-bottom: 15px;
}
.timeline-box {
width: 50px;
height: 50px;
background-color: #1769aa;
}
@keyframes timeline-fill {
from {
transform: scaleX(0);
}
to {
transform: scaleX(1);
}
}
@keyframes timeline-reveal {
from {
opacity: 0.2;
transform: translateX(0);
}
to {
opacity: 1;
transform: translateX(100px);
}
}
@supports (animation-timeline: scroll()) {
.timeline-progress {
animation: timeline-fill linear both;
animation-timeline: scroll(nearest block);
}
}
@supports (animation-timeline: view()) and (animation-range: entry) {
.timeline-box {
animation: timeline-reveal linear both;
animation-timeline: view();
animation-range: entry;
}
}
@media (prefers-reduced-motion: reduce) {
.timeline-progress,
.timeline-box {
animation: none;
}
}
The two elements use different timelines. The progress bar follows the scroll container's overall progress, while the square follows its own entry into the viewing area.
As you scroll, the progress bar advances toward its full width. The square reveals itself during its entry range, reaching its final keyframe when that range finishes.
Scrolling backward reverses the corresponding animation progress.
In browsers without the required scroll-driven animation features, the square remains visible and stationary. The progress indicator remains in its initial state. Reduced-motion preferences disable both animations.
Using the animation Shorthand
The animation-timeline property is not included in the general animation shorthand syntax.
When you use the animation shorthand, it resets animation-timeline to auto. Therefore, declare the timeline after the shorthand.
Correct Declaration Order
.box {
animation: reveal linear both;
animation-timeline: view();
animation-range: entry;
}
The animation-timeline declaration overrides the default time-based timeline established by the shorthand.
Incorrect Declaration Order
.box {
animation-timeline: view();
animation: reveal linear both;
}
In this example, the shorthand resets animation-timeline to auto, so the animation no longer uses the intended view timeline.
The animation-range shorthand is separate from the general animation shorthand and can be used to define the start and end attachment points.
Important Considerations
Keep the following points in mind when using animation-timeline:
- Default value: The initial value is
auto, which normally uses the document's time-based timeline. - Scroll progress: Use
scroll()when an animation should follow the scroll position of a container. - View progress: Use
view()when an animation should follow an element's movement through a viewing area. - Named timelines: Use
scroll-timeline-nameorview-timeline-namewhen an animation needs to reference a timeline established by another element. - Animation duration: Use
animation-duration: autowhen appropriate for scroll-driven animations. - Declaration order: Place
animation-timelineafter theanimationshorthand because the shorthand resets the timeline. - Attachment ranges: Use
animation-rangeto limit the portion of the timeline used for animation progress. - Scroll containers: Ensure the selected timeline has meaningful scroll or view progress. A timeline may be inactive when its source cannot provide usable progress.
- Multiple animations: Comma-separated timeline values are matched to animation names in their listed order.
- Browser compatibility: Scroll-driven timelines have more limited browser support than traditional time-based animations.
- Accessibility: Respect
prefers-reduced-motionand ensure essential information remains available without animation.
Use animation-timeline to connect animations to the scrolling experience while keeping the animation behavior defined in CSS.
Browser Support
The animation-timeline property has Limited availability and is not supported consistently across all major browsers.
Support varies for anonymous scroll timelines, view timelines, and named timelines. Traditional CSS keyframe animation support does not guarantee support for scroll-driven animation timelines.
Use @supports to apply scroll-driven behavior only when the necessary features are available.
@supports (animation-timeline: view()) and (animation-range: entry) {
.box {
animation: reveal linear both;
animation-timeline: view();
animation-range: entry;
}
}
Provide a readable fallback for browsers that do not support these features. Avoid hiding essential content unless a supported animation will reveal it.
Visit Can I Use? to check current browser compatibility.
Specifications
The animation-timeline property is defined in the CSS Scroll-Driven Animations Level 1 specification.
