Sub topic 5
Choosing the scroller and the axis
This chapter assumes you know how @keyframes and the animation shorthand work. It also assumes you know what makes an element a scroll container (any overflow value other than visible), and that you have seen animation-timeline: scroll() once. The previous chapter built a reading progress bar with scroll(root block). Here you learn what each part of that function does and how to pick the right values.
What scroll() takes
The scroll() function takes two optional arguments in either order:
scroll( <scroller> || <axis> )The scroller says which scroll container supplies the progress. The axis says which direction of scrolling counts. If you leave out an argument, the browser uses its default: nearest for the scroller and block for the axis. So scroll() is the same as scroll(nearest block).
The animation then runs from 0% progress (scrolled to the start) to 100% progress (scrolled to the end) along that axis.
The three scroller values
| Value | Which scroll container is used |
|---|---|
| `nearest` | The closest ancestor of the animated element that is a scroll container. This is the default. |
| `root` | The document viewport. |
| `self` | The animated element itself, which must be a scroll container. |
root
root is the right choice for page-level effects such as progress bars and a header that changes as the page scrolls. It works from any element on the page, however deeply nested.
@keyframes grow {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
.page-progress {
position: fixed;
inset: 0 0 auto 0;
height: 4px;
background: rebeccapurple;
transform-origin: left;
animation: grow linear both;
animation-timeline: scroll(root block);
}nearest
nearest walks up the ancestor chain and stops at the first scroll container. It lets you write a component once and have it follow whichever scroller it ends up inside. This example tints a sticky header as its panel scrolls:
@keyframes tint {
from { background-color: transparent; }
to { background-color: white; }
}
.panel {
height: 300px;
overflow-y: auto;
}
.panel-header {
position: sticky;
top: 0;
animation: tint linear both;
animation-timeline: scroll(nearest block);
}The header sits inside .panel, and .panel is its nearest scroll container. Moving the same header into a different scrolling box changes which scroll position drives it, with no CSS change.
self
self uses the animated element's own scroll position. This suits a scrollable box that reacts to its own scrolling:
@keyframes warm {
from { border-color: #9aa; }
to { border-color: crimson; }
}
.log {
height: 200px;
overflow-y: auto;
border: 3px solid #9aa;
animation: warm linear both;
animation-timeline: scroll(self block);
}As you scroll .log toward the bottom, its border changes color. If .log did not have overflow set, self would have nothing to read.
How nearest picks its scroller
The nearest scroll container is decided by CSS, not by whether the content actually overflows. An ancestor with overflow: hidden, auto, scroll or clip-like behavior that creates a scroll container counts, even when its content is short. Two consequences follow:
- A wrapper with
overflow: hiddenthat you added to clip something becomes the nearest scroller. Your animation then follows that wrapper, which never scrolls, and nothing happens. - If an element has no scroll container ancestor,
nearestfalls back to the viewport, so it behaves likeroot.
When the chosen scroller has no overflow in the chosen axis, the timeline is inactive. The animation has no scroll range to progress through, so it does not run. A page that is shorter than the window gets no progress bar movement. This is correct behavior, but it looks like a bug the first time.
Choosing the axis
The axis argument has four values:
| Value | Meaning |
|---|---|
| `block` | The scroller's block direction. This is the default. |
| `inline` | The scroller's inline direction. |
| `y` | Vertical, regardless of writing mode. |
| `x` | Horizontal, regardless of writing mode. |
block and inline are logical values. They follow the writing mode of the scroll container. In a horizontal language such as English, block is vertical and inline is horizontal. In a vertical writing mode such as writing-mode: vertical-rl, they swap. x and y are physical and never change.
Use logical values for components that should adapt to the document's language and writing mode. Use x and y when the layout is fixed to a physical direction, for example a carousel whose scrolling must always be horizontal.
This example shows a horizontal strip with a sticky label that fades as the strip scrolls sideways:
@keyframes fade-out {
from { opacity: 1; }
to { opacity: 0; }
}
.strip {
display: flex;
gap: 1rem;
overflow-x: auto;
}
.strip-label {
position: sticky;
left: 0;
animation: fade-out linear both;
animation-timeline: scroll(nearest x);
}Writing scroll(nearest inline) would give the same result here, because the strip is in a horizontal writing mode.
Two axes, one scroller
A scroller can overflow in both directions, for example a large map or table. Each scroll() timeline tracks only one axis, so one scroller can drive two independent animations:
.map-x-marker {
animation: slide-x linear both;
animation-timeline: scroll(nearest x);
}
.map-y-marker {
animation: slide-y linear both;
animation-timeline: scroll(nearest y);
}If you ask for an axis that does not overflow, that timeline is inactive, as described above.
A decision path
The last branch, a named timeline, is for an element that is not inside the scroller. A progress indicator placed in a toolbar next to a scrolling list is the typical case. nearest cannot reach a scroller that is not an ancestor, so that case needs scroll-timeline and timeline-scope, which later chapters cover.
Common mistakes
- Putting
animation-timelinebefore theanimationshorthand. The shorthand resetsanimation-timelinetoauto. Declareanimation-timelineafter it. - Leaving the default easing. The default
easetiming function distorts scroll progress. Uselinearso the animation matches the scroll position, unless you want the distortion on purpose. - Using
selfon an element that does not scroll. Addoverflowand make sure the content is taller or wider than the box. - Assuming
nearestincludes the element itself. It starts at the parent. Useselffor the element's own scrolling. - Using
xorywhereblockorinlineis needed, or the reverse, in content that supports vertical writing modes.
Exercise
A .badge element sits inside .wrapper { overflow: hidden; }, which sits inside the normal page. You give .badge animation-timeline: scroll() expecting it to follow page scrolling, but it never animates. Why does this happen, and what is the smallest change that fixes it?
Show answer
scroll() defaults to nearest, and .wrapper is a scroll container because overflow: hidden creates one. .wrapper never scrolls, so its timeline is inactive and the animation does not run. Changing the value to scroll(root block) makes the badge follow the viewport's scroll position.
Exercise
You build a horizontally scrolling gallery, and a progress marker inside it should always track horizontal scrolling, even if a page translation switches the document to a vertical writing mode. Which axis value should you use, inline or x, and why?
Show answer
Use x. It is a physical direction and stays horizontal in any writing mode. inline would turn vertical if the writing mode became vertical.