Sub topic 10
Sharing a Timeline with timeline-scope
This chapter assumes you can already build a scroll progress timeline and a view progress timeline. It also assumes you know how to name one with scroll-timeline or view-timeline. You should be comfortable with animation-timeline, animation-range, and the rule that the animation shorthand resets animation-timeline. Here you will learn why a named timeline sometimes can't be found, and how timeline-scope fixes that.
The problem: names are only visible downward
A named timeline has a limited reach. When you write scroll-timeline: --list block on an element, you create a timeline called --list. By default, only that element's descendants can use the name in animation-timeline. A view-timeline name follows the same rule.
This works well when the animated element sits inside the element that defines the timeline. Real pages often don't look like that. A common case is a scrolling panel with a progress indicator beside it, or a list of sections with a navigation bar that lives somewhere else in the document. The indicator is not a descendant of the scroller, so this fails:
.scroller {
height: 300px;
overflow-y: auto;
scroll-timeline: --list block;
}
.indicator {
animation: fill linear both;
animation-timeline: --list; /* --list is not visible here */
}The browser can't resolve --list for .indicator, so the animation has no usable timeline. The indicator then typically shows its ordinary, unanimated styles. Nothing in the console reports the mistake. If a named timeline seems to do nothing, check scope first.
What timeline-scope does
timeline-scope is set on an ancestor. It says that the listed timeline names are defined somewhere inside this element, and that every element in this element's subtree may use them. The ancestor does not create a timeline. It only widens the area where the names can be looked up.
The syntax is:
timeline-scope: none; /* default: no names hoisted */
timeline-scope: --list; /* one name */
timeline-scope: --list, --section-a; /* several names, comma separated */Some newer browsers also accept timeline-scope: all. It hoists every named timeline below the element. Check current browser support before relying on it. Listing the names explicitly works everywhere scroll-driven animations work, and it documents your intent.
A complete example: a scroller with a separate indicator
Here is the markup. The indicator and the scroller are siblings inside body:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>timeline-scope demo</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="indicator"></div>
<div class="scroller">
<p>Item 1</p><p>Item 2</p><p>Item 3</p><p>Item 4</p>
<p>Item 5</p><p>Item 6</p><p>Item 7</p><p>Item 8</p>
<p>Item 9</p><p>Item 10</p><p>Item 11</p><p>Item 12</p>
</div>
</body>
</html>Here is the CSS, style.css:
body {
timeline-scope: --list;
display: flex;
gap: 1rem;
}
.scroller {
height: 300px;
width: 240px;
overflow-y: auto;
scroll-timeline: --list block;
}
.scroller p {
margin: 0;
padding: 2rem 1rem;
}
@keyframes fill {
from { transform: scaleY(0); }
to { transform: scaleY(1); }
}
.indicator {
width: 6px;
height: 300px;
background: rebeccapurple;
transform-origin: top;
animation: fill linear both;
animation-timeline: --list;
}Three things make this work together:
.scrollerdeclares the timeline withscroll-timeline: --list block. The scroll container is the.scrollerelement, and the progress runs along its block axis.bodydeclarestimeline-scope: --list. Both.scrollerand.indicatorare insidebody, so the name is now visible to both of them..indicatorusesanimation-timeline: --list. It is declared after theanimationshorthand, so the shorthand does not reset it.
The bar fills as you scroll the list, even though the two elements are siblings.
Choosing where to put timeline-scope
The scope element must be an ancestor of the element that defines the timeline and of every element that consumes it. Beyond that, you choose how far up to place it.
- Nearest common ancestor. Put it on the closest element that contains both the definer and the consumers. The name stays local and is easy to reason about.
- A high-level wrapper. On a large page, a
mainor app-shell element can hold several names at once. bodyor:root. This is convenient for page-wide timelines. It also makes the names visible to the whole page, which can lead to clashes as the stylesheet grows.
Prefer the nearest common ancestor. It limits the blast radius of the name, which is the same reason you avoid global variables.
A second example: navigation driven by section visibility
timeline-scope is especially useful with view-timeline, because the element that should react is often in a navigation bar rather than in the section itself. In this example each section defines a view timeline. Its matching dot in the header animates while the section crosses the viewport.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Section dots</title>
<link rel="stylesheet" href="dots.css">
</head>
<body>
<header class="dots">
<span class="dot dot-a"></span>
<span class="dot dot-b"></span>
</header>
<section class="panel panel-a">Section A</section>
<section class="panel panel-b">Section B</section>
</body>
</html>body {
margin: 0;
timeline-scope: --panel-a, --panel-b;
}
.dots {
position: fixed;
top: 0;
left: 0;
right: 0;
display: flex;
gap: 8px;
padding: 12px;
background: white;
}
.dot {
width: 12px;
height: 12px;
border-radius: 50%;
background: #ccc;
}
.panel {
height: 100vh;
display: grid;
place-items: center;
font-size: 2rem;
}
.panel-a { view-timeline: --panel-a block; }
.panel-b { view-timeline: --panel-b block; }
@keyframes highlight {
0%, 100% { background: #ccc; }
50% { background: rebeccapurple; }
}
.dot-a {
animation: highlight linear both;
animation-timeline: --panel-a;
}
.dot-b {
animation: highlight linear both;
animation-timeline: --panel-b;
}The .dots header is a sibling of the sections, so without timeline-scope on body the dots could not see --panel-a or --panel-b. Here animation-range is left at its default, which is the full cover range of each section. Each dot lights up at the midpoint of its section's pass through the viewport and fades back out afterward.
How the pieces fit together
| Property | Set on | Purpose |
|---|---|---|
| `scroll-timeline` or `view-timeline` | The scroller or the tracked element | Creates and names the timeline |
| `timeline-scope` | A common ancestor | Makes the name visible beyond the definer's own subtree |
| `animation-timeline` | The element being animated | Looks up the name and drives its animation |
Common mistakes
Putting timeline-scope on the wrong element
Setting timeline-scope on the animated element or on the scroller does not help. It must be on an ancestor of both. If the scope element is only an ancestor of the consumer, the definer is outside the scope and the name still can't be resolved.
Forgetting to list the name
timeline-scope only hoists the names you list, unless you use all where supported. A typo such as --lst in one place produces no error. The animation simply doesn't run. Copy the dashed identifier exactly, including both leading dashes.
Reusing one name for several elements
If several elements inside the scope define the same name, the name is ambiguous. In that situation the timeline may be inactive or may resolve to a different element than you intended. This often happens with repeated list items, such as each card declaring view-timeline: --card. Give each timeline you need to share its own unique name. If you only need each card to animate itself, don't use a name at all. Use the anonymous view() function instead.
Resetting animation-timeline with the shorthand
As in the earlier chapters, animation-timeline must come after the animation shorthand. If you write it first, the shorthand resets it to the default time-based timeline. Your animation then runs once on load instead of following the scroll.
Skipping the fallback
Hoisting a timeline doesn't change browser support. Wrap scoped timelines in the same @supports and prefers-reduced-motion conditions you used for other scroll-driven animations. Where scroll timelines are unsupported, a decorative indicator should be hidden or left in a sensible static state.
@media (prefers-reduced-motion: no-preference) {
@supports (animation-timeline: scroll()) {
.indicator {
animation: fill linear both;
animation-timeline: --list;
}
}
}The timeline-scope declaration itself can stay outside the conditions. It has no visible effect when no animation uses the name.
Exercises
Exercise
A page has a main element containing a .gallery scroller and, as a separate child of main, a .counter badge that should scale up as the gallery is scrolled. You wrote scroll-timeline: --gallery block on .gallery and animation-timeline: --gallery on .counter, but the badge never animates. What single declaration fixes it, and on which element does it go?
Show answer
Add timeline-scope: --gallery; to main. It is the common ancestor of .gallery, which defines the timeline, and .counter, which uses it. Without it, the name --gallery is only visible inside .gallery's own subtree.
Exercise
A stylesheet gives every .card the declaration view-timeline: --card, and body has timeline-scope: --card. A separate sidebar element uses animation-timeline: --card. Why is the result unreliable, and what are two ways to fix it?
Show answer
Many elements define the same name inside one scope, so the name is ambiguous and the sidebar can't tell which card's timeline it is meant to follow. One fix is to give each card a unique name, such as --card-1 and --card-2, and list each one in timeline-scope. The other is to drop the shared name if each card only needs to animate itself, and use animation-timeline: view() on the card instead.