Sub topic 9
Named timelines with scroll-timeline and view-timeline
This chapter assumes you can write @keyframes and the animation properties. It also assumes you know what makes an element a scroll container, and that you have used animation-timeline: scroll() and animation-timeline: view() in their anonymous forms. Here you will give a timeline a name and attach animations to it by that name.
Why anonymous timelines are not always enough
The functions scroll() and view() create a timeline on the spot. scroll() picks a scroller relative to the animated element, usually the nearest ancestor scroller. view() always tracks the animated element itself inside its nearest scroller.
That covers many cases, but three situations need more control:
- The scroller you want is not the nearest one. A nested panel sits between your element and the page scroller.
- You want one element's visibility to drive a different element's animation, such as a card's title that animates while the card enters.
- Several elements should share a single timeline, so their animations stay in sync.
A named timeline solves these. You declare a timeline on one element, give it a dashed identifier such as --page, and refer to that name from animation-timeline on any element that can see it.
Declaring a named scroll timeline
Put these properties on the scroll container:
| Property | Purpose |
|---|---|
| `scroll-timeline-name` | The dashed identifier, for example `--page`. |
| `scroll-timeline-axis` | `block`, `inline`, `x`, or `y`. Defaults to `block`. |
| `scroll-timeline` | Shorthand for name and axis, for example `--page block`. |
The shorthand is the usual form. A named scroll timeline progresses from 0% to 100% as the container scrolls from start to end along the chosen axis, exactly like scroll() would.
Names must start with two hyphens and are case-sensitive. --Page and --page are different names.
Consider a page where a table-of-contents panel scrolls on its own. You want a small indicator inside the panel to show how far the whole page has scrolled.
<div class="page">
<aside class="toc">
<div class="meter"></div>
<ul>
<li>Intro</li><li>Setup</li><li>Usage</li><li>Gotchas</li>
<li>Reference</li><li>Changelog</li><li>Credits</li>
</ul>
</aside>
<main>
<p>Long article content goes here.</p>
</main>
</div>.page {
height: 100vh;
overflow-y: auto;
scroll-timeline: --page block;
}
.toc {
position: sticky;
top: 0;
max-height: 8rem;
overflow-y: auto;
}
@keyframes grow {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
.meter {
position: sticky;
top: 0;
height: 4px;
background: rebeccapurple;
transform-origin: left;
animation: grow linear both;
animation-timeline: --page;
}With scroll() and no arguments, .meter would follow .toc, because .toc is its nearest scrolling ancestor. Naming the page's timeline and writing animation-timeline: --page selects the outer scroller explicitly.
Declare animation-timeline after the animation shorthand. The shorthand resets animation-timeline to its initial value, so writing it first would discard your timeline.
Declaring a named view timeline
A view timeline tracks how much of one element, the subject, is visible inside its scrollport. The named version is declared on the subject:
| Property | Purpose |
|---|---|
| `view-timeline-name` | The dashed identifier. |
| `view-timeline-axis` | `block`, `inline`, `x`, or `y`. |
| `view-timeline-inset` | Shrinks or grows the area used to measure visibility, for example `20px` or `auto`. |
| `view-timeline` | Shorthand for name and axis. |
The scroller is still the subject's nearest ancestor scroll container. You are naming the timeline, not choosing a different scroller.
The real benefit appears when other elements use that name. A descendant can animate according to its parent's visibility:
@keyframes slide-in {
from { opacity: 0; transform: translateX(-30px); }
to { opacity: 1; transform: none; }
}
@keyframes zoom {
from { transform: scale(1); }
to { transform: scale(1.15); }
}
.card {
view-timeline: --card block;
}
.card .title {
animation: slide-in linear both;
animation-timeline: --card;
animation-range: entry 0% entry 100%;
}
.card .image {
animation: zoom linear both;
animation-timeline: --card;
animation-range: cover;
}Both children follow the same .card timeline, but each uses a different range. The title finishes sliding in once the card has fully entered the scrollport. The image zooms gradually across the whole time the card crosses the view. If you had written animation-timeline: view() on .title, the title would track its own visibility, which differs from the card's and would make the two pieces drift out of sync.
How name lookup works
An animation-timeline name is resolved by looking for a timeline with that name on the element itself or its ancestors. A name declared on an element is visible to that element's descendants and nowhere else, unless timeline-scope is applied. The next chapter covers timeline-scope.
Two consequences matter in practice:
- If two ancestors declare the same name, the nearest one wins. This is how a component can reuse
--cardfor every card in a list. Each card's descendants see their own card's timeline. - A sibling or a cousin of the declaring element cannot see the name. Without
timeline-scope, an indicator outside the scroller will not find--listand will not animate.
Axis and shorthand details
The axis values behave as they do in scroll() and view():
blockis the direction blocks stack in the writing mode, usually vertical.inlineis the direction text flows, usually horizontal.yandxare physical directions that ignore writing mode.
For a horizontal carousel, declare scroll-timeline: --gallery inline. Use x instead if your design must stay horizontal even in a vertical writing mode.
The shorthands are order-sensitive in one way: the name comes first and the axis second. Omitting the axis gives block. Setting the shorthand also resets any longhand you set earlier in the cascade, so avoid mixing scroll-timeline with scroll-timeline-axis on the same rule.
A view timeline can also be tuned with view-timeline-inset. A positive inset makes the measured area smaller, so the subject counts as "entering" later. A common use is a fixed header that covers the top 80px of the scrollport:
.card {
view-timeline: --card block;
view-timeline-inset: 80px 0;
}The first value applies to the start edge and the second to the end edge.
Reusing one timeline for several animations
Because the timeline is a named object, one element can run multiple animations on it, and each can have its own range and keyframes. Separate several names in animation-timeline with commas, in the same order as the animation-name list:
.hero {
view-timeline: --hero block;
}
@keyframes fade-out {
from { opacity: 1; }
to { opacity: 0; }
}
@keyframes rise {
from { transform: translateY(0); }
to { transform: translateY(-60px); }
}
.hero .caption {
animation: fade-out linear both, rise linear both;
animation-timeline: --hero, --hero;
animation-range: exit 0% exit 100%, cover;
}If the lists have different lengths, the shorter one repeats to match the longer. A single animation-timeline: --hero would therefore apply to both animations.
Common mistakes
- Name not visible. The animated element is not a descendant of the element that declares the name. The animation silently does not run. Move the declaration up to a common ancestor, or use
timeline-scope. - Missing dashes.
scroll-timeline: pageis invalid. Use--page. - Declaration before the shorthand. Writing
animation-timelinebeforeanimationloses the timeline. - Inactive scroller. If the container does not overflow, the scroll timeline is inactive and the animation does not progress. Check that content is taller than the box and that
overflowisautoorscroll. - Range keywords on a scroll timeline. Names such as
entryandcoverapply only to view timelines. A scroll timeline accepts percentages and lengths. - Wrong subject. A named view timeline measures the element that declares it. Declaring
view-timelineon the wrapper and expecting it to track a child's visibility gives the wrapper's progress.
Pair named timelines with the reduced-motion guard from the earlier chapters so users who ask for less motion see static content:
@media (prefers-reduced-motion: no-preference) {
@supports (animation-timeline: scroll()) {
.card .title {
animation: slide-in linear both;
animation-timeline: --card;
animation-range: entry 0% entry 100%;
}
}
}The @supports test uses scroll() only to detect support for the property. Browsers that fail it ignore the rule, and the title simply appears in place.
Exercise
A page has an outer scroller .app and, inside it, an inner scrollable .sidebar. An element .badge inside .sidebar should scale from 0 to 1 as .app scrolls. Why does animation-timeline: scroll() on .badge give the wrong result, and how do you fix it with a named timeline?
Show answer
scroll() defaults to the nearest ancestor scroller, which is .sidebar, so the badge follows the sidebar's scrolling. Declare scroll-timeline: --app block; on .app, then set animation-timeline: --app; on .badge after the animation shorthand. .badge is a descendant of .app, so it can see the name.
Exercise
Two cards each declare view-timeline: --card block. Each card contains a .title that uses animation-timeline: --card. Does every title follow the first card's timeline? Explain.
Show answer
No. Name lookup finds the nearest declaration among the element and its ancestors. Each .title is inside its own card, so it resolves --card to that card's timeline, and each title animates according to its own card's visibility.