Sub topic 13
Common patterns and pitfalls
This chapter collects the patterns you will reach for most often and the mistakes that cause most "my animation does nothing" bug reports. It assumes you can write @keyframes, know the animation-timeline and animation-range properties, and know the difference between scroll() and view() timelines. It also assumes you know that a scroll container is any element with an overflow value other than visible or clip.
Patterns worth keeping
Pattern 1: a header that gains a shadow after a short scroll
A sticky header often needs a visual cue once the page has moved. A scroll timeline bounded by a length range does this without JavaScript. The animation covers only the first 120px of scrolling, then holds its end state.
@keyframes header-shadow {
from { opacity: 0; }
to { opacity: 1; }
}
.site-header {
position: sticky;
top: 0;
}
.site-header::after {
content: "";
position: absolute;
inset: auto 0 -8px 0;
height: 8px;
background: linear-gradient(rgb(0 0 0 / 0.25), transparent);
opacity: 0;
animation: header-shadow linear both;
animation-timeline: scroll(root);
animation-range: 0px 120px;
}The animation animates opacity on a pseudo-element. Animating box-shadow on the header itself would repaint on every scroll frame. The range uses explicit lengths (0px 120px) instead of named ranges because the animation is tied to scroll distance, not to an element's visibility.
Pattern 2: parallax inside a clipped wrapper
Parallax moves an image more slowly than the page. Give the wrapper a named view timeline, and let the image use it.
@keyframes drift {
from { transform: translateY(-10%); }
to { transform: translateY(10%); }
}
.hero {
position: relative;
height: 60vh;
overflow: clip;
view-timeline: --hero block;
}
.hero img {
position: absolute;
top: -20%;
left: 0;
width: 100%;
height: 140%;
object-fit: cover;
animation: drift linear both;
animation-timeline: --hero;
}Two details matter here. The image is a descendant of .hero, so the name --hero is visible without timeline-scope. The wrapper uses overflow: clip instead of overflow: hidden, for a reason covered under pitfall 2.
Pattern 3: a staggered reveal with no delays
If every list item has its own view() timeline, items lower on the page start later because they enter the scrollport later. The stagger comes from layout.
@keyframes reveal {
from { opacity: 0; transform: translateY(24px); }
to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: no-preference) {
@supports (animation-timeline: view()) {
.list > li {
animation: reveal linear both;
animation-timeline: view();
animation-range: entry 10% entry 90%;
}
}
}The range entry 10% entry 90% finishes the effect while the item is still entering. The item is fully visible and settled before the reader reaches it. A range that runs to cover makes the content look unfinished while the reader is trying to read it.
Pitfalls
Pitfall 1: the shorthand resets the timeline
The animation shorthand sets animation-timeline back to auto, and it also resets animation-range. Declare both after the shorthand.
/* Broken: the shorthand comes last and wins */
.bad {
animation-timeline: view();
animation-range: entry 0% cover 30%;
animation: reveal linear both;
}
/* Correct */
.good {
animation: reveal linear both;
animation-timeline: view();
animation-range: entry 0% cover 30%;
}The broken version runs as an ordinary time-based animation. It plays once on page load, which can look almost right, so the mistake is easy to miss.
Pitfall 2: the scroller does not scroll
scroll() and view() attach to the nearest ancestor scroll container. That has three consequences.
- An ancestor with
overflow: hiddencounts as a scroll container even though the user cannot scroll it. The timeline attaches to it and never moves. This is why Pattern 2 usesoverflow: clip, which clips without creating a scroll container. - A container with
overflow: autobut not enough content to overflow has an inactive timeline. The animation does not progress. - If you meant the page scroller, say so with
scroll(root)orview()placed where no intermediate scroll container sits in between.
When an animation seems dead, open DevTools and walk up the ancestors of the element, checking each overflow value.
Pitfall 3: a named timeline that cannot be found
A name such as --list is visible only to the element that declares it and to that element's descendants. A sibling or a cousin cannot see it. The fix is timeline-scope on a common ancestor, as in the earlier chapter. Two related slips are common. One is a missing double dash in the name, which makes the declaration invalid. The other is declaring timeline-scope on an element that is not an ancestor of both the scroller and the animated element.
Pitfall 4: hiding content in the base styles
Never write the hidden state as a normal declaration.
/* Risky: if the animation never runs, the card stays invisible */
.card {
opacity: 0;
}Put the hidden state only inside the from keyframe. In browsers without scroll-driven animation support, the card then shows in its normal state. Combine this with the @media and @supports wrapper from Pattern 3. This keeps the content readable when animation is unsupported or the reader has asked for less motion.
Pitfall 5: fill mode and elements already in view
An element near the top of the page is already partway through its entry range when the page loads. Without both, it takes its un-animated style outside the range, so it can jump when the range boundary is crossed. With animation-fill-mode: both (the both in the shorthand), the element holds the from state before the range and the to state after it. At load, the elements in the first viewport then appear already settled instead of snapping. For a reveal, the practical advice is to always use both and linear easing.
Pitfall 6: time-based settings used as scroll controls
animation-delay, animation-duration and animation-iteration-count describe time. They do not map onto scroll distance in a way you can reason about. Shape scroll-driven motion with animation-range, and leave the duration as auto. Easing is the exception. An easing function other than linear still applies, but it bends the relationship between scroll position and motion. A reveal with ease-out can feel as if it slows down under the reader's finger. Use linear for progress indicators, and use a different easing only for short, bounded effects.
Pitfall 7: animating properties that force layout
Scroll-driven animations run as often as the browser produces scroll frames. Animating width, height, top or margin makes the browser recompute layout on each frame. If you animate a property on the subject of a view() timeline that changes the subject's own box, the progress calculation depends on its own output, and the result can jitter. Stick to transform, opacity and, where needed, filter. Replace width: 0 → 100% with scaleX(0) → scaleX(1) and set transform-origin.
A debugging flow
The checks below are ordered from cheapest to most involved. Stop at the first one that fails.
Quick symptom table
| Symptom | Likely cause | Fix |
|---|---|---|
| Animation plays once at load, ignoring scroll | Shorthand declared after `animation-timeline` | Declare the timeline after the shorthand |
| Nothing moves, no errors | Nearest scroller is `overflow: hidden` or does not overflow | Use `overflow: clip`, or target `scroll(root)` |
| Indicator outside the scroller does not move | Named timeline not in scope | Add `timeline-scope` on a common ancestor |
| Content invisible in older browsers | Hidden state in base styles | Move hidden state into `from` keyframe, add `@supports` |
| Elements jump at the edges of the range | Missing fill mode | Use `both` |
| Choppy motion on low-end devices | Layout-affecting properties animated | Animate `transform` and `opacity` |
Choosing between CSS and JavaScript
Use CSS when the effect is a pure function of scroll position, which covers everything in this chapter. Reach for ScrollTimeline and ViewTimeline only when you must create, pause or reuse animations from script, or when you need to read the animation's current time. A JavaScript IntersectionObserver that toggles a class is still the right tool when you want a one-shot, time-driven transition triggered by visibility, because a scroll-driven animation runs backwards when the reader scrolls back up.
Exercise
A developer wraps an image in <div class="frame" style="overflow: hidden"> and sets animation-timeline: view() on the image. The image never moves, even though the page scrolls. Name the cause and give the smallest fix.
Show answer
view() attaches to the nearest ancestor scroll container, and overflow: hidden makes .frame a scroll container that never scrolls, so the timeline stays inactive. Change .frame to overflow: clip, which clips without creating a scroll container. Then the image's view() timeline attaches to the page scroller. Alternatively, give .frame a named view-timeline and use that name on the image.
Exercise
This rule does not scroll-link the animation. Why, and how do you fix it? .box { animation-timeline: scroll(root); animation: grow linear both; }
Show answer
The animation shorthand comes after animation-timeline and resets it to auto, so the animation runs on the document timeline (time). Move animation-timeline: scroll(root); below the shorthand.
Exercise
Why should a reveal effect put opacity: 0 in the from keyframe instead of on the .card rule?
Show answer
If the browser does not support scroll-driven animations, or the @media or @supports wrapper excludes the rule, the animation never runs. With opacity: 0 on .card, the card would stay invisible. With the hidden state only in from, the card shows normally when the animation is not applied.