Sub topic 7
Limiting the active part of the timeline with animation-range
This chapter assumes you can build a scroll progress timeline with scroll() and a view progress timeline with view(). It also assumes you know that animation-timeline replaces elapsed time with scroll progress, and you should be comfortable with @keyframes, animation-fill-mode, and the idea of a scrollport (the visible area of a scroll container).
What animation-range does
By default, an animation attached to a timeline uses the whole timeline. With scroll(), the keyframes run from the scroll container's start to its end. With view(), they run from the moment the element first touches the scrollport to the moment it has fully left.
That default is rarely what you want. A card that fades in only after it has fully left the bottom edge, or a bar that completes after 400px of scrolling, needs a smaller window. animation-range selects that window. Your from keyframe maps to the start of the window, and your to keyframe maps to the end.
Outside the window, the animation is not running. Whether the element shows its first keyframe, its last keyframe, or its normal style there depends on the fill mode. This is why most examples in this tutorial use both.
Syntax
animation-range is a shorthand for two longhands, animation-range-start and animation-range-end. Each accepts one of these:
normal, the default, which means the start or end of the whole timeline.- A length or percentage, such as
100pxor20%. - A named range, optionally followed by an offset, such as
entry 25%.
The shorthand expands in predictable ways:
| You write | It means |
|---|---|
| `animation-range: entry` | `entry 0% entry 100%` |
| `animation-range: entry exit` | `entry 0% exit 100%` |
| `animation-range: entry 0% cover 30%` | start at 0% of entry, end at 30% of cover |
| `animation-range: 20% 80%` | from 20% to 80% of the timeline |
| `animation-range: normal` | the full timeline |
When you give a name without an offset, the start defaults to 0% and the end to 100%. A bare percentage with no name refers to the whole timeline. On a view timeline, that whole timeline is the cover range.
Named ranges on a view timeline
A view timeline divides an element's trip across the scrollport into named phases. Each name is a range you can use as a start or end.
| Name | Active from | Active until |
|---|---|---|
| `cover` | the element's leading edge touches the far edge of the scrollport | the element's trailing edge leaves the near edge |
| `entry` | the element starts entering the scrollport | it is fully inside, or fills the scrollport |
| `contain` | the element is fully inside, or fills the scrollport | it starts to leave |
| `exit` | the element starts leaving | it has fully left |
| `entry-crossing` | the leading edge crosses into the scrollport | the trailing edge has crossed in |
| `exit-crossing` | the leading edge starts crossing out | the trailing edge has crossed out |
Percentages are measured within the named range. So entry 100% is the moment entry ends, which for an element that fits in the scrollport is the same position as contain 0%. cover 0% is the same as entry 0%, and cover 100% is the same as exit 100%.
The -crossing names follow the element's edges regardless of its size. Use them when an element can be taller than the scrollport. For such elements, entry and exit end earlier because the element covers the scrollport.
Worked example: where does cover 30% land?
Here is the card example from the plan.
@keyframes reveal {
from { opacity: 0; transform: translateY(40px); }
to { opacity: 1; transform: none; }
}
.card {
animation: reveal linear both;
animation-timeline: view();
animation-range: entry 0% cover 30%;
}Suppose the card is 300px tall and the scrollport is 900px tall. The cover range lasts as long as the card needs to travel from touching the bottom edge to clearing the top edge. That is the scrollport height plus the card height: 1200px of scrolling.
entry 0%is at 0px of scrolling. The card's top edge just touches the bottom of the scrollport.entry 100%is at 300px. The card is fully inside.cover 30%is at 0.3 × 1200 = 360px.
So the animation runs over 360px of scroll. It finishes 60px after the card is fully visible. Opacity reaches 1 slightly late, which can feel gentler than ending exactly at entry 100%. If you want the animation to end exactly when the card is fully visible, write animation-range: entry. This stays correct if the card height changes, because the end of entry is computed from the real size.
Because the start is entry 0%, the card remains at opacity: 0 while it is off-screen. The both fill mode keeps the from keyframe applied before the range, and the to keyframe applied after it.
Ranges on scroll progress timelines
Named ranges such as entry belong to view timelines. A scroll() timeline has no such names, so you use lengths and percentages. They measure scroll distance, from 0 at the start of the scroller to its maximum scroll offset.
@keyframes grow {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
.progress {
position: fixed;
inset: 0 0 auto 0;
height: 4px;
background: rebeccapurple;
transform-origin: left;
animation: grow linear both;
animation-timeline: scroll(root block);
animation-range: 0px 400px;
}This bar finishes growing after 400px of scrolling and stays full for the rest of the page, because both holds the to state. Percentages work the same way. animation-range: 10% 90% ignores the first and last tenth of the scroll distance.
A length is an absolute scroll distance. A percentage stays proportional if the page grows. Pick the one that matches your intent.
Combining entry and exit animations
You can attach several animations to one element and give each its own range. Both animation and animation-range accept comma-separated lists, and the items line up in order.
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes fade-out {
from { opacity: 1; }
to { opacity: 0; }
}
.card {
animation: fade-in linear backwards, fade-out linear forwards;
animation-timeline: view();
animation-range: entry, exit;
}The fill modes are chosen on purpose. Suppose both animations used both. Before the card enters, fade-in would hold opacity 0, and fade-out would hold its from value of 1. When two animations affect the same property, the later one in the list wins. The card would be visible before it ever animated in. Here, fade-in fills only backwards, so it applies its first keyframe before entry and does nothing afterward. fade-out fills only forwards, so it does nothing until exit begins and then holds its end state.
An alternative is one animation across cover with keyframe percentages that place the fade-in and fade-out where you want them. It uses a single animation and avoids fill-mode conflicts. Choose the two-animation form when the entry and exit effects use different properties or different easing.
Common errors
- Using
entryon ascroll()timeline. Named ranges are meaningless there, so the declaration is invalid and the browser discards it. The animation then spans the full timeline. Use lengths or percentages instead. - Forgetting the fill mode. Without
both(or the right one-sided mode), the element snaps back to its normal style outside the range. Cards that should stay hidden until they enter will appear fully visible, then flash into the animation. - Expecting
animation-durationto matter. With a scroll-driven timeline, the range sets the length of the animation. Adurationof1shas no effect on timing. Leave the duration unset, or useauto. - Ending the range before it starts.
animation-range: exit entryputs the end before the start, which gives you nothing useful. Keep the start earlier on the timeline than the end. - Measuring percentages against the wrong thing.
30%alone is 30% ofcoveron a view timeline.entry 30%is 30% of just the entry phase. These can be very different distances. - Relying on pixel offsets for responsive layouts. A
300pxoffset behaves differently on a phone than on a desktop. Prefer named ranges and percentages when the layout changes.
Debugging a range
If a range does not behave as expected, check three things in order. First, confirm the element has the timeline you think it has, with animation-timeline declared after the shorthand. Second, check whether the element is bigger than the scrollport, which changes where entry and contain end. Third, scroll slowly and watch when the style changes. The interactive range visualizer at scroll-driven-animations.style shows each named range for a real element and lets you drag the start and end handles. You can copy the resulting animation-range value into your CSS.
Exercise
A 200px tall panel scrolls through a 800px tall scrollport. It uses animation-timeline: view() and animation-range: entry 0% cover 25%. How many pixels of scrolling does the animation run over, and does it end before or after the panel is fully visible?
Show answer
The cover range lasts 800 + 200 = 1000px. Cover 25% is 250px. The entry range lasts 200px (the panel's height), so the panel is fully visible at 200px. The animation runs for 250px of scrolling and finishes 50px after the panel is fully visible.
Exercise
You write .card { animation-range: entry; animation: reveal linear both; animation-timeline: view(); }. The reveal animation seems to run across the whole view timeline instead of just entry. Why, and how do you fix it?
Show answer
The animation shorthand comes after animation-range, and it resets animation-range to its default (normal). Move animation-range after the animation shorthand, along with animation-timeline.