Sub topic 11
Progressive enhancement, @supports and prefers-reduced-motion
Scroll-driven animations are not available in every browser, and some users ask their system to limit motion. This chapter shows how to ship the animation so that both groups still get a complete, readable page. It assumes you can already write @keyframes, attach a timeline with animation-timeline, and use scroll() and view().
The goal: content first, motion second
Progressive enhancement means the page works without the effect, and the effect is added only where it is safe. For scroll-driven animation, this has two conditions:
- The browser understands
animation-timeline. - The user has not asked for reduced motion.
If either condition fails, the elements should simply be visible in their final state, with no animation.
The common mistake
Suppose the base rule hides the card and relies on the animation to reveal it:
.card {
opacity: 0;
animation: reveal linear both;
animation-timeline: view();
}In a browser that does not support animation-timeline, the animation runs on the default time-based timeline, or the card stays at opacity: 0. Either way, the content can be invisible or flash unexpectedly. Hiding content in the base styles and counting on an enhancement to show it is the main pitfall.
The safer rule is to write the base styles so that content is in its normal, visible state. Put every animation declaration inside the conditional blocks.
Feature detection with @supports
@supports tests whether the browser accepts a declaration. For scroll timelines, test the animation-timeline property with a scroll() value:
@supports (animation-timeline: scroll()) {
/* only runs where scroll-driven animations exist */
}Testing the property name alone is not enough in every case. Using a real timeline function as the value makes the test match what you intend to use.
Respecting prefers-reduced-motion
prefers-reduced-motion is a media feature that reflects the user's operating system setting. It has two values: reduce and no-preference. Scroll-linked motion can be a trigger for people with vestibular disorders, so the animation should run only when the value is no-preference.
Nest the two conditions together:
@keyframes reveal {
from { opacity: 0; transform: translateY(40px); }
to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: no-preference) {
@supports (animation-timeline: scroll()) {
.card {
animation: reveal linear both;
animation-timeline: view();
animation-range: entry 0% cover 30%;
}
}
}Three things to notice:
- Outside the blocks,
.cardhas no animation and no hidden starting state, so it is visible everywhere. animation-timelinecomes after theanimationshorthand, because the shorthand resets it.- Choosing
no-preferenceas the condition is deliberate. Users whose browser does not report the preference also get the safe, static page only if you treat motion as opt-in. Where the feature is unsupported, the media query does not matchreduce, so motion is still gated by your own@supportsblock.
Reduced motion is not always "no animation"
Some users accept gentle changes but not large movement. A reasonable option is to keep a fade and drop the slide when motion is reduced. Define a second set of keyframes that animate only opacity, and attach it inside a prefers-reduced-motion: reduce block. Whether to do this depends on your design. The minimum is to remove the movement.
Testing both paths
- In browser developer tools, emulate
prefers-reduced-motion: reduceand confirm the cards are visible and still. - Test in a browser without scroll timeline support, or temporarily rename the
@supportstest to something false, and confirm the page is complete.
Exercise
Why should the opacity: 0 starting state live inside the @media (prefers-reduced-motion: no-preference) and @supports blocks instead of in the base .card rule?
Show answer
Because if the animation does not run (unsupported browser or reduced motion), nothing would bring the opacity back to 1 and the card would stay hidden. Keeping the starting state inside the keyframes and the conditional blocks means the base card is always visible.
Exercise
In the nested example, why is animation-timeline: view() written after animation: reveal linear both?
Show answer
The animation shorthand resets animation-timeline to its initial value, so declaring the timeline afterward keeps it from being overwritten.