Color Change
A 180 px square pinned inside a 280 px frame: its plain colour slides from indigo to magenta, to cyan and then to green over 520 px of scrolling, and the text of its caption takes the same hue at the same instant. animation-timeline: scroll(), four colours in CSS variables, no JavaScript.
Updated
This effect is part of Effect.Labs — 846 vanilla effects, some free, some premium. Scroll has 74 effects, including 12 free. Explore the category →
3 usage examples



How it works
Under the .fx-0554-root root, the .sdc-scroll-container area (280 px, overflow-y: auto) holds a .sb-colorchange-content tray 800 px tall: its height sets the travel, 800 − 280 = 520 px. The tray has only two children, both position: sticky: the .sb-colorchange-box square (top: 37px) and the .sb-colorchange-label caption (top: 229px). They therefore sit at the same spot in the frame from the first scrolled pixel to the last — it is the empty tray that passes behind them.
Two keyframes do the work. fx0554Fond animates background-color on the square, fx0554Texte animates color on the caption, with the same four stops (0, 33, 66 and 100%) read from --fx0554-c1 to --fx0554-c4. animation-timeline: scroll() swaps the clock for how far the area has been scrolled. Because a plain colour interpolates, the hue slides without a jolt. Read in Chromium: rgb(99, 102, 241) at the start, rgb(133, 93, 240) at 50 px, rgb(168, 83, 240) at 100 px, exact magenta at 172 px, a lavender blue rgb(108, 128, 225) at 260 px, cyan at 343 px, green at 520 px.
Nothing is remembered: colour is a function of position. Scrolling back up retraces the path, stopping midway leaves the square in its in-between hue. The variables sit on the root, so two copies on a page can carry two palettes; measured with two copies, the second pushed to 520 px (green) while the first stayed at 0 (indigo). Outside @supports (animation-timeline: scroll()), an @supports not rule puts the fourth colour on the square and the caption: the object is coloured, it just no longer changes.
Accessibility
- Reduced motion: a colour change is not a movement, so the code keeps it as it is under
prefers-reduced-motion: reduce(both colour animations still run, checked). Only two details switch off: the pulsing of the “Scroll ↓” hint and thescroll-behavior: smoothof the area. - Contrast, worth a close look: the coloured caption sits at 4.4:1 on the
#0a0a0fbackground at the start (indigo), then 5.7:1, 8.1:1 and 7.8:1. The white text inside the square reaches 4.47:1 on indigo, just under the 4.5:1 threshold, and drops to 3.5:1 on magenta, 2.4:1 on cyan and 2.5:1 on green. If that text carries information, write it in#0a0a0f(4.4:1 at worst across the four colours) or darken the palette. - Keyboard: the area has
tabindex="0",role="region"and anaria-label. Tab lands on it in Chromium, Firefox and WebKit with a 2 px violet outline; four presses of the down arrow move it 160 px in Chromium and 204 px in Firefox. WebKit skipped it before thetabindexwas added. - What colour does not say: a screen reader announces no hue. The area's
aria-labeldescribes the principle and the hint isaria-hidden. If the colour encodes a state (level, step, temperature), back it with visible text or a figure. - Touch: a 150 px finger drag moved the area by 147 px in Chromium touch emulation (390 px wide), the colour following the finger. At the end of the travel the gesture goes back to the page.
Browser compatibility
CSS only. animation-timeline: scroll(): Chrome and Edge 115, Safari 26, Firefox 159 according to MDN data. Identical readings in Chromium 149 and WebKit 26.5 (rgb(182, 80, 240) at 120 px in both). Firefox 151 answers no to @supports: the square and the caption are green (#10b981) from the start and stay so, the area scrolls with no visible effect. Variables inside keyframes and position: sticky are no trouble for any of these engines.
The fallback written in the code is a static colour: without animation-timeline, @supports not applies --fx0554-c4. If the colour has to follow scrolling everywhere, you need a scroll listener that writes a CSS variable — this code does not include one, it is yours to add.
The code
HTML structure to paste into your page (the CSS + JS come with your purchase):
<div class="fx-0554-root">
<div class="sdc-scroll-container" tabindex="0" role="region" aria-label="…">
<div class="sb-colorchange-content">
<div class="sb-colorchange-box">…</div>
<span class="sb-colorchange-label">…</span>
</div>
</div>
<span class="sdc-scroll-hint" aria-hidden="true">…</span>
</div>
One-time payment, no subscription — the code is yours forever
Customize
Options passed to the API or data-* attributes:
| Option / property | Default | Effect |
|---|---|---|
Colours (CSS — --fx0554-c1 to --fx0554-c4, on the root) |
#6366f1, #d946ef, #06b6d4, #10b981 | Four hues, from the top of the travel to the bottom. Checked with red as c1 and blue as c4: the square starts red and ends blue. Colours of similar lightness keep text laid over them readable. |
Stops (CSS — @keyframes fx0554Fond and fx0554Texte) |
0%, 33%, 66%, 100% | Move a percentage to make a hue arrive sooner; add a line and a fifth variable for one more colour. Edit both keyframes together, otherwise the caption drifts out of tune with the square. |
Travel (CSS — .sb-colorchange-content { height: 800px }) |
800 px, i.e. 520 px of scrolling | 580 px gives 300 px of travel, so the colour changes almost twice as fast under the wheel. With real content in the tray, write height: auto: the travel becomes the length of the text. |
Pinned position (CSS — top: 37px on the square, 229px on the caption) |
square + caption centred in the 280 px | 37 px = (280 − 180 − 12 − 14) ÷ 2, and 229 = 37 + 180 + 12. If you resize the square, redo that sum and copy the first value into the tray's padding-top as well. |
Shape (CSS — width, height, border-radius of .sb-colorchange-box) |
180 × 180 px, 20 px radius | border-radius: 50% makes it a disc; 14 px across, a status dot; a border or a clip-path turns it into a pictogram. Only the fill is animated, everything else can be redrawn freely. |
Area (CSS — .sdc-scroll-container { height: 280px; overflow-y: auto }) |
280 px box | Without those two declarations, scroll() reads the scrolling of the whole page. Tried: the colour then runs from first to last over the full height of the document, and the square is only pinned while its tray goes by. |
Hint (HTML — span.sdc-scroll-hint) |
“Scroll ↓” | A demo marker placed at the bottom of the root. Delete it once the effect lives in a page where scrolling is obvious. |
FAQ
background with five gradients on the backdrop of a whole section: a gradient does not interpolate, so the palette flips at once at each stop. Color Change animates background-color, a plain colour, on a pinned object: the hue slides continuously, and the caption text follows it through a second keyframe. Color Shift changes the mood of a section; Color Change tints an object — dot, illustration, gauge — and its label.0%, 30% for the first, 36%, 63% for the second, 69%, 94% for the third and to for the last: the square stays indigo up to 150 px, turns magenta between 156 and 187 px, holds until 328 px, and so on. The blend then lasts only 6% of the travel, i.e. 31 px.position: sticky. The tray must be taller than the area (800 px against 280 here), otherwise there is nothing to scroll. No element between the square and the area may carry overflow: hidden, auto or scroll: the square would latch onto it. And top must stay below the frame height minus the square's height. In the original version of this effect the square was not pinned at all: centred in the tray, it only showed up after 17 px of scrolling.