Sub topic 11
Progressive enhancement, @supports and prefers-reduced-motion
Scroll-driven animations are a good fit for progressive enhancement. The page works without them, and browsers that support them get extra motion. This chapter shows how to build that safety net with @supports, how to honor the prefers-reduced-motion setting, and how to check both from JavaScript.
This chapter assumes you know the animation-timeline property, scroll() and view(), and animation-range from earlier chapters. You should also be comfortable with @media queries and the cascade.
Why scroll-driven animation needs a fallback
Two things can stop an animation from running:
- The browser does not implement
animation-timeline. Support has been uneven across browsers and versions, so check a current compatibility table before you ship. - The user has asked their operating system to reduce motion.
A scroll-linked animation is more than decoration. Its first keyframe usually hides something, for example opacity: 0. If the animation never runs, the content must still be visible.
Progressive enhancement means the baseline page is complete, and the animation is layered on top only when it can run. For scroll-driven animations, that gives two rules:
- Write the normal, finished appearance as the default styles.
- Put every animation declaration inside conditions that confirm the animation can run.
Feature detection with @supports
@supports applies a block of CSS only if the browser understands a given declaration. You can test the property and value you depend on:
@supports (animation-timeline: scroll()) {
.progress {
animation: grow linear both;
animation-timeline: scroll(root block);
}
}A browser that does not know animation-timeline treats the condition as false and skips the whole block. The .progress element then has no animation at all.
What happens without the guard
Suppose you write the declarations at the top level instead. In a browser that ignores animation-timeline, the animation shorthand still works. It runs as an ordinary time-based animation, with grow playing once, on page load, in the default duration of 0s. With both as the fill mode, the element jumps to the to keyframe. That is often harmless, but it is not guaranteed. If you set a duration, or your to keyframe is not the state you want, users see a motion you never designed. The guard removes that uncertainty.
Ordering inside the block
The animation shorthand resets animation-timeline. Declare the timeline after the shorthand, as the earlier examples do. The shorthand also resets animation-range, so the same rule applies to it.
Testing the right feature
animation-timeline: scroll() and animation-timeline: view() are the same property with different values. A single check on scroll() is a reasonable proxy for both. If your design depends on animation-range or timeline-scope, test those separately when you need to be strict:
@supports (animation-range: entry 0% cover 30%) {
/* styles that depend on animation-range */
}Keep these checks small. A long chain of @supports blocks is hard to maintain, and in practice the support differences between these properties are small.
The not form
@supports not (...) lets you write fallback styles for browsers without support. You can use it to give those browsers a different treatment, such as a static progress indicator:
@supports not (animation-timeline: scroll()) {
.progress {
display: none;
}
}Prefer the positive form when you can. A page that works with no extra CSS is simpler than one that needs a fallback block.
Respecting prefers-reduced-motion
Some people get dizzy, nauseous or distracted by large movement on screen. Operating systems let them request less of it, and browsers expose that through the prefers-reduced-motion media feature. It has two values:
| Value | Meaning |
|---|---|
| `no-preference` | The user has not asked for reduced motion. |
| `reduce` | The user wants less motion. |
Scroll-driven animation is a special case. The user controls the speed of the motion because they are scrolling. That does not make it safe. Parallax, large translations and elements sliding across the screen can still trigger symptoms, because the movement happens at the same time as the scroll movement and competes with it.
The opt-in pattern
The pattern from this chapter's examples wraps the animation in both conditions:
@media (prefers-reduced-motion: no-preference) {
@supports (animation-timeline: scroll()) {
.card {
animation: reveal linear both;
animation-timeline: view();
animation-range: entry 0% cover 30%;
}
}
}The animation applies only when the browser can run it and the user has not asked for reduced motion. Everyone else sees the cards in their final state. This is an opt-in approach: motion is the exception. It is safer than writing the animation first and removing it in a reduce block, because a forgotten override leaves motion on for the people who asked you to remove it.
Reduce, do not always remove
The guideline is to reduce motion, not to remove every change. Fades and color changes are usually acceptable. Large translations, scaling, spinning and parallax are the risky effects. A progress bar that only scales along one axis in a thin strip is low risk. You can keep that and drop the sliding.
Here is a card reveal that keeps a fade for everyone and adds the slide only when motion is allowed:
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes slide-in {
from { transform: translateY(40px); }
to { transform: none; }
}
@supports (animation-timeline: scroll()) {
.card {
animation: fade-in linear both;
animation-timeline: view();
animation-range: entry 0% cover 30%;
}
@media (prefers-reduced-motion: no-preference) {
.card {
animation: fade-in linear both, slide-in linear both;
animation-timeline: view(), view();
animation-range: entry 0% cover 30%;
}
}
}Two details matter here. First, animation-timeline takes a comma-separated list, and the list lines up with the animations in animation. With two animations you provide two timelines, or the single value is repeated across the list. Writing the list out makes the intent clear. Second, animation-range also repeats across animations when you give one value, so one declaration covers both.
Checking support from JavaScript
You sometimes need to know in script whether the feature is available. For example, you may only want to attach a JavaScript fallback or skip loading a helper library. CSS.supports runs the same check as @supports:
const supportsScrollTimeline = CSS.supports('animation-timeline', 'scroll()');
const reducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
function updateMotionClass() {
const enhance = supportsScrollTimeline && !reducedMotion.matches;
document.documentElement.classList.toggle('scroll-motion', enhance);
}
updateMotionClass();
reducedMotion.addEventListener('change', updateMotionClass);The script toggles a class on the root element and listens for changes. A user can switch the operating system setting while the page is open, and a change listener lets the page react without a reload. The class gives you a hook for any behavior that CSS alone cannot express.
If you use the Web Animations API from the earlier JavaScript chapter, check for the constructor before you call it:
if ('ScrollTimeline' in window && !window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
const timeline = new ScrollTimeline({
source: document.documentElement,
axis: 'block'
});
document.querySelector('.progress').animate(
{ transform: ['scaleX(0)', 'scaleX(1)'] },
{ timeline, fill: 'both', easing: 'linear' }
);
}Calling new ScrollTimeline where the class does not exist throws a ReferenceError. That would stop the rest of your script, so the guard matters.
Common mistakes
- Hiding content in the base styles. Setting
opacity: 0on.cardand relying on the animation to show it leaves the content invisible when the animation does not run. Put the hidden state only inside the keyframes. - Testing only in a supporting browser. Turn on the reduced-motion setting in your operating system or browser developer tools, and test in a browser that lacks the feature, or temporarily rename the property to simulate it.
- Forgetting the property order. The
animationshorthand resetsanimation-timelineandanimation-range. Declare them afterward. - Treating the media query as optional for scroll effects. Scroll control does not remove the need to reduce motion.
- Using
reduceonly to turn things off. Offer a calmer version where it helps, such as an opacity-only reveal.
Summary
- Write the finished appearance as the default, and add animation on top.
- Wrap the animation in
@supports (animation-timeline: scroll())so unsupported browsers skip it. - Wrap it in
@media (prefers-reduced-motion: no-preference)so users who asked for less motion do not get large movement. - Use
CSS.supports,matchMediaand the'ScrollTimeline' in windowcheck when script is involved.
Exercise
A page sets .card { opacity: 0; } in its base styles, then animates opacity to 1 with animation-timeline: view() inside an @supports (animation-timeline: view()) block. Users in an older browser report that the cards never appear. What is the bug, and how do you fix it?
Show answer
The base style hides the cards, and the @supports block that would reveal them is skipped in the older browser, so the cards stay at opacity 0. Remove opacity: 0 from the base styles. The hidden state should exist only in the from keyframe, which applies only when the animation runs. Then the cards are visible by default in browsers without support.
Exercise
Write the CSS wrapper so that a .hero element uses animation: parallax linear both with animation-timeline: scroll() only when the browser supports the property and the user has not asked for reduced motion.
Show answer
@media (prefers-reduced-motion: no-preference) {
@supports (animation-timeline: scroll()) {
.hero {
animation: parallax linear both;
animation-timeline: scroll();
}
}
}
The two conditions can be nested in either order. animation-timeline comes after the animation shorthand so the shorthand does not reset it.
Exercise
Why does the JavaScript example add a change listener to the matchMedia result?
Show answer
The user can change the operating system's reduced-motion setting while the page is open. The listener runs the update function again, so the page switches the motion class on or off without a reload.