An animation timeline can solely be of service to the aspect that defines it, and to that aspect’s descendants. Nonetheless, with the timeline-scope CSS property, a named animation timeline is inside attain of any aspect sharing an ancestor with the aspect that defines it, as soon as that ancestor is fitted with a timeline scope for a similar.
Put one other manner: When a component tracks a timeline, like a scroll timeline or view timeline, that timeline can drive an animation in any of the mother or father aspect’s youngsters or descendants as a result of they’re all in scope.
That is sometimes helpful in creating consumer interfaces — like progress indicators for scrollable sections of a web page — to offer visible results as parts come and exit of view. One other instance: highlighting place markers as completely different elements of a web page are scrolled to.
On this article, we’re going to have a look at an instance the place scrolling via one carousel triggers an animation on one other aspect with timeline-scope doing all of the lifting. As you’ll see, It’s a pleasant animation impact. But it surely can be helpful for shopping via a number of visible focal factors with a single scrolling by the consumer.
Some examples the place you may need seen comparable results: In a racing sport, when you choose a automobile, its specs roll via as you flick through the vehicles. In a TV match, participant profiles are rotated via, together with their statistics. Primarily, a number of carousels are displayed in sync with a major one.

Notice: Since timeline-scope will not be but Baseline, I like to recommend its use for elements of a web page which are ornamental moderately than essential performance.
The Carousels
First, we lay down a typical horizontal carousel, and a secondary one the place scrolling will not be wanted. They’re in reverse order within the markup due to how we would like them positioned.
Discover what we’re doing subsequent within the CSS: permitting the first carousel’s content material to overflow and scroll within the horizontal path. We’ll sprinkle in a bit of scroll snapping to ensure every carousel merchandise doesn’t get skipped. In the meantime, we’re clipping the secondary carousel’s overflow to stop it from scrolling in any respect.
/* Major carousel */
.cp {
/* Permits horizontal scrolling */
overflow-x: auto;
/* Snaps slides into place throughout scrolling */
scroll-snap-type: x obligatory;
}
.cp-slide {
/* Units the snap place to the middle of every slide */
scroll-snap-align: heart;
}
/* Secondary carousel */
.cs {
/* Prevents content material from overflowing.
Disables all scrolling/scrollbars */
overflow: clip;
}
To date, now we have two carousels: one which scrolls and one that doesn’t.
The Animation and Its Timeline
Subsequent up, we have to outline the keyframe animation that we’re going to use to the secondary carousel. That is the timeline that’ll be in its scope, permitting the aspect to maneuver from proper to left in a manner the place one carousel merchandise is displayed at a time as scrolling occurs on the first carousel.
@keyframes cs-scroll {
to {
/* Strikes to the left primarily based on the variety of siblings */
rework: translateX(calc(-100% * (sibling-count() - 1)));
}
}
Notice: We’re utilizing the very new CSS sibling-count() perform. It does what it says and counts the variety of direct youngsters inside a component. Its help is nearing Baseline, however I need to name it out as a result of it’s nonetheless rolling out and never prime time for manufacturing. One other technique to go about it’s to make use of CSS variables to index gadgets straight within the markup — which is certainly extra cumbersome and why we actually want sibling-count() (to not point out sibling-index()).
OK, let’s apply that animation to the secondary carousel:
The animation is then utilized to every slide within the secondary carousel. Since this animation must mirror the scrolling within the major carousel, a scroll timeline, --carousel-scroll, is outlined over the horizontal axis within the major carousel, and the identical timeline is assigned because the animation timeline within the secondary carousel.
/* ... */
/* Secondary carousel */
.cs-slide {
/* Applies the 'cs-scroll' animation to the slides */
animation: cs-scroll linear forwards;
}
You is likely to be used to setting a length or delay on the animation to regulate its pace. However that’s pointless after we’re coping with scroll animations as a result of the animation’s progress is predicated on the scroll place, not an ordinary timeline. So, let’s register that scroll timeline on the first carousel. All it takes is the scroll-timeline property with a customized ident as the worth that we’ll reference on the secondary carousel with the animation-timeline property.
/* Major carousel */
.cp {
overflow-x: auto;
scroll-snap-type: x obligatory;
/* Defines a scroll timeline primarily based on the x-axis */
scroll-timeline: --carousel-scroll x;
}
/* Secondary carousel */
.cs-slide {
/* Applies the 'cs-scroll' animation to the slides */
animation: cs-scroll linear forwards;
/* Hyperlinks the animation progress to the scroll place of #cp */
animation-timeline: --carousel-scroll;
}
Scoping the Timeline to Each Carousels
Now we’re monitoring scrolling on the first carousel. And simply as I promised earlier, we will use the first carousel’s scrolling progress to set off the animation we outlined on the secondary carousel.
All now we have to do is hoist up the first carousel’s scroll timeline to the aspect. Why the ? As a result of that’s the mother or father the 2 carousels have in frequent. If we scope the timeline to the physique, then we’re primarily passing it to each carousels since they’re each direct youngsters.
physique {
/* Lifts the scope of '--carousel-scroll' timeline to the physique aspect
so it may be shared between #cp and #cs */
timeline-scope: --carousel-scroll;
}
However, hey, we may actually do the identical factor if the carousels had been youngsters of one other container if that’s how the markup is structured, say .carousels:
/* Father or mother container */
.carousels {
/* Shares the scroll time with any descendant */
timeline-scope: --carousel-scroll;
}
/* Major carousel */
#cp {
/* ... */
scroll-timeline: --carousel-scroll x;
}
/* Secondary carousel */
.cs-slide {
animation: cs-scroll linear forwards;
animation-timeline: --carousel-scroll;
}
And we may technically do that identical factor for as many youngsters as we would like! They usually don’t should all use the identical animation both. It’s a wild concept that we will set off one or many animations from a consumer’s scroll place in a single or many parts. All in vanilla CSS.








