Building a slinky that walks down stairs in pure CSS
Braydon Coyer's monthly CSS art challenge opened with the theme Spring, and a slinky is the natural subject: that classic toy that tumbles down a flight of stairs under its own momentum. Making one in 3D CSS, one that keeps slinking downward indefinitely, is the puzzle worth documenting.
Markup and scene fundamentals
The structure should let CSS custom properties drive everything, so values can be swapped without touching the markup. Written in Pug:
- const RING_COUNT = 10;
.container
.scene
.plane(style=`--ring-count: ${RING_COUNT}`)
- let rings = 0;
while rings < RING_COUNT
.ring(style=`--index: ${rings};`)
- rings++;
Compiled, that yields 10 rings — see the compiled version — with --ring-count inlined for easy configuration. The extra .scene wrapper exists to keep room for later requirements.
First, the properties for the "infini-slinky" scene:
:root {
--border-width: 1.2vmin;
--depth: 20vmin;
--stack-height: 6vmin;
--scene-size: 20vmin;
--ring-size: calc(var(--scene-size) * 0.6);
--plane: radial-gradient(rgb(0 0 0 / 0.1) 50%, transparent 65%);
--ring-shadow: rgb(0 0 0 / 0.5);
--hue-one: 320;
--hue-two: 210;
--blur: 10px;
--speed: 1.2s;
--bg: #fafafa;
--ring-filter: brightness(1) drop-shadow(0 0 0 var(--accent));
}
As in most 3D CSS scenes, transform-style is set across the board:
* {
box-sizing: border-box;
transform-style: preserve-3d;
}
The scene depends on translating the .plane so the slinky appears to descend endlessly. Several transforms land on the .container, including a few magic numbers that get explained below:
.container {
/* Define the scene's dimensions */
height: var(--scene-size);
width: var(--scene-size);
/* Add depth to the scene */
transform:
translate3d(0, 0, 100vmin)
rotateX(-24deg) rotateY(32deg)
rotateX(90deg)
translateZ(calc((var(--depth) + var(--stack-height)) * -1))
rotate(0deg);
}
.scene,
.plane {
/* Ensure our container take up the full .container */
height: 100%;
width: 100%;
position: relative;
}
.scene {
/* Color is arbitrary */
background: rgb(162 25 230 / 0.25);
}
.plane {
/* Color is arbitrary */
background: rgb(25 161 230 / 0.25);
/* Overrides the previous selector */
transform: translateZ(var(--depth));
}
Component by component:
translate3d(0, 0, 100vmin)pulls the.containerforward so the 3D work isn't clipped by the body — safe here because perspective isn't applied at this level.rotateX(-24deg) rotateY(32deg)gives the scene the intended viewing angle.rotateX(90deg)flattens.sceneand.plane; without it they would read as the top and bottom faces of a cube.translate3d(0, 0, calc((var(--depth) + var(--stack-height)) * -1))moves the scene into place on the z-axis, centering it using--depthand--stack-height.rotate(0deg)is unused for now but reserved for rotating or animating scene rotation later.
Rings: placement, color, geometry
Ring positioning draws on the inlined --index and --ring-count together with the properties declared on :root:
.ring {
--origin-z:
calc(
var(--stack-height) - (var(--stack-height) / var(--ring-count))
* var(--index)
);
--hue: var(--hue-one);
--accent: hsl(var(--hue) 100% 55%);
height: var(--ring-size);
width: var(--ring-size);
border-radius: 50%;
border: var(--border-width) solid var(--accent);
position: absolute;
top: 50%;
left: 50%;
transform-origin: calc(100% + (var(--scene-size) * 0.2)) 50%;
transform:
translate3d(-50%, -50%, var(--origin-z))
translateZ(0)
rotateY(0deg);
}
.ring:nth-of-type(odd) {
--hue: var(--hue-two);
}
Each ring is absolutely positioned; note how --origin-z is computed and then applied through transform. The last ruleset alternates ring colors. A rainbow variant that walks the hues was the first attempt, but it adds complexity to the effect.
The rings also carry a transform-origin based on the .scene size:
.ring {
transform-origin: calc(100% + (var(--scene-size) * 0.2)) 50%;
}
The 0.2 represents half the space left in .scene once the ring has been positioned — a value worth tidying up:
:root {
--ring-percentage: 0.6;
--ring-size: calc(var(--scene-size) * var(--ring-percentage));
--ring-transform:
calc(
100%
+ (var(--scene-size) * ((1 - var(--ring-percentage)) * 0.5))
) 50%;
}
.ring {
transform-origin: var(--ring-transform);
}
That origin makes the ring's motion look off-center. Toggling an individual ring's transform with a slider in the demo shows the flip clearly; restoring every ring flips the whole stack. But the rings don't yet land on the next stair. Since --origin-z is already calculated, the fix is a parallel --destination-z: a ring at the top of the stack should end up at the bottom after falling, so each ring scopes its own destination.
ring {
--destination-z: calc(
(
(var(--depth) + var(--origin-z))
- (var(--stack-height) - var(--origin-z))
) * -1
);
transform-origin: var(--ring-transform);
transform:
translate3d(-50%, -50%, var(--origin-z))
translateZ(calc(var(--destination-z) * var(--flipped, 0)))
rotateY(calc(var(--flipped, 0) * 180deg));
}
Getting the timing right (and where CSS falls short)
A naïve flip-plus-fall animation runs every ring in unison and looks wrong, because animation-delay isn't being used:
.ring {
animation-name: slink;
animation-duration: 2s;
animation-fill-mode: both;
animation-iteration-count: infinite;
}
@keyframes slink {
0%, 5% {
transform:
translate3d(-50%, -50%, var(--origin-z))
translateZ(0)
rotateY(0deg);
}
25% {
transform:
translate3d(-50%, -50%, var(--origin-z))
translateZ(0)
rotateY(180deg);
}
45%, 100% {
transform:
translate3d(-50%, -50%, var(--origin-z))
translateZ(var(--destination-z))
rotateY(180deg);
}
}
Delaying each ring by its --index produces successive slinking:
.ring {
animation-delay: calc(var(--index) * 0.1s);
}
It's closer, but there's a deeper problem: animation-delay applies only to the first iteration. Coloring the rings across the hue wheel makes the issue obvious:
.ring {
--hue: calc((360 / var(--ring-count)) * var(--index));
}
What's needed is a delay on every iteration — the equivalent of GreenSock's delay alongside its repeatDelay, which JavaScript animation libraries can express. A reduced case: two boxes meant to spin alternately. A plain delay fails, because the red box keeps spinning and the blue one resumes spinning after its single delay.
.box {
animation: spin 1s var(--delay, 0s) infinite;
}
.box:nth-of-type(2) {
--delay: 1s;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
GreenSock handles it with ease:
import gsap from 'https://cdn.skypack.dev/gsap'
gsap.to('.box', {
rotate: 360,
/**
* A function based value, means that the first box has a delay of 0 and
* the second has a delay of 1
*/
delay: (index) > index,
repeatDelay: 1,
repeat: -1,
ease: 'power1.inOut',
})
In CSS without JavaScript, the only route is to pad @keyframes with empty space and ditch animation-delay entirely. A keyframe that fully rotates the element twice, then animation-direction: reverse on the second box:
@keyframes spin {
50%, 100% {
transform: rotate(360deg);
}
}
.box {
animation: spin 2s infinite;
}
.box:nth-of-type(2) {
animation-direction: reverse;
}
Almost — the second box rotates the wrong way. Wrapping the element and rotating the wrapper adds balancing work, so the cleaner answer is two separate keyframes:
@keyframes box-one {
50%, 100% {
transform: rotate(360deg);
}
}
@keyframes box-two {
0%, 50% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
A native declaration would have been far simpler:
/* Hypothetical! */
animation: spin 1s 0s 1s infinite;
Or, if the repeated delay matched the initial one, a combinator:
/* Hypothetical! */
animation: spin 1s 1s+ infinite;
Generating per-ring keyframes with a preprocessor
Consistent delays mean one keyframe set per ring, all sized so every ring has slinked and settled before the set repeats. Hand-writing that is impractical, so this is a job for a preprocessor — Stylus here — writing it only once:
// STYLUS GENERATED KEYFRAMES BE HERE...
$ring-count = 10
$animation-window = 50
$animation-step = $animation-window / $ring-count
for $ring in (0..$ring-count)
// Generate a set of keyframes based on the ring index
// index is the ring
$start = $animation-step * ($ring + 1)
@keyframes slink-{$ring} {
// In here is where we need to generate the keyframe steps based on ring count and window.
0%, {$start * 1%} {
transform
translate3d(-50%, -50%, var(--origin-z))
translateZ(0)
rotateY(0deg)
}
// Flip without falling
{($start + ($animation-window * 0.75)) * 1%} {
transform
translate3d(-50%, -50%, var(--origin-z))
translateZ(0)
rotateY(180deg)
}
// Fall until the cut-off point
{($start + $animation-window) * 1%}, 100% {
transform
translate3d(-50%, -50%, var(--origin-z))
translateZ(var(--destination-z))
rotateY(180deg)
}
}
The variables: $ring-count is the number of rings; $animation-window is the percentage of the keyframe available for slinking (here 50%, with the other 50% serving as delay); $animation-step is the computed stagger used to derive each ring's unique keyframe percentages. The compiled start:
View full code
@keyframes slink-0 {
0%, 4.5% {
transform:
translate3d(-50%, -50%, var(--origin-z))
translateZ(0)
rotateY(0deg);
}
38.25% {
transform:
translate3d(-50%, -50%, var(--origin-z))
translateZ(0)
rotateY(180deg);
}
49.5%, 100% {
transform:
translate3d(-50%, -50%, var(--origin-z))
translateZ(var(--destination-z))
rotateY(180deg);
}
}
@keyframes slink-1 {
0%, 9% {
transform:
translate3d(-50%, -50%, var(--origin-z))
translateZ(0)
rotateY(0deg);
}
42.75% {
transform:
translate3d(-50%, -50%, var(--origin-z))
translateZ(0)
rotateY(180deg);
}
54%, 100% {
transform:
translate3d(-50%, -50%, var(--origin-z))
translateZ(var(--destination-z))
rotateY(180deg);
}
}
Each ring then needs its own --name in addition to --index:
- const RING_COUNT = 10;
.container
.scene
.plane(style=`--ring-count: ${RING_COUNT}`)
- let rings = 0;
while rings < RING_COUNT
.ring(style=`--index: ${rings}; --name: slink-${rings};`)
- rings++;
Which compiles to:
<div class="container">
<div class="scene">
<div class="plane" style="--ring-count: 10">
<div class="ring" style="--index: 0; --name: slink-0;"></div>
<div class="ring" style="--index: 1; --name: slink-1;"></div>
<div class="ring" style="--index: 2; --name: slink-2;"></div>
<div class="ring" style="--index: 3; --name: slink-3;"></div>
<div class="ring" style="--index: 4; --name: slink-4;"></div>
<div class="ring" style="--index: 5; --name: slink-5;"></div>
<div class="ring" style="--index: 6; --name: slink-6;"></div>
<div class="ring" style="--index: 7; --name: slink-7;"></div>
<div class="ring" style="--index: 8; --name: slink-8;"></div>
<div class="ring" style="--index: 9; --name: slink-9;"></div>
</div>
</div>
</div>
And the styling follows suit:
.ring {
animation: var(--name) var(--speed) both infinite cubic-bezier(0.25, 0, 1, 1);
}
Timing carries the effect, so the default animation-timing-function is replaced with a cubic-bezier, and the --speed property declared at the start is used here.
Looping forever
The heavy lifting is done. To repeat endlessly, translate the scene while the slinky slinks, so it appears to slide back into its starting position:
.scene {
animation: step-up var(--speed) infinite linear both;
}
@keyframes step-up {
to {
transform: translate3d(-100%, 0, var(--depth));
}
}
Dropping the platform colors from .scene and .plane keeps that transition from looking jarring. One last detail: the stack flips before it slinks again — precisely where ring color helps. Set the ring count to an odd number such as 11 and return to alternating colors.
Variations
For a "flip flop" — a slinky that alternates direction — add a wrapper around the scene and rotate it 180deg on every slink:
- const RING_COUNT = 11;
.container
.flipper
.scene
.plane(style=`--ring-count: ${RING_COUNT}`)
- let rings = 0;
while rings < RING_COUNT
.ring(style=`--index: ${rings}; --name: slink-${rings};`)
- rings++;
Use the steps() timing function at twice the --speed:
.flipper {
animation: flip-flop calc(var(--speed) * 2) infinite steps(1);
height: 100%;
width: 100%;
}
@keyframes flip-flop {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(180deg);
}
100% {
transform: rotate(360deg);
}
}
Finally, the .scene element's step-up animation no longer needs x-axis movement; steps(1) is what makes this work:
@keyframes step-up {
0% {
transform: translate3d(-50%, 0, 0);
}
100% {
transform: translate3d(-50%, 0, var(--depth));
}
}
A slow rotation of the whole scene can round it out:
.container {
animation: rotate calc(var(--speed) * 40) infinite linear;
}
@keyframes rotate {
to {
transform:
translate3d(0, 0, 100vmin)
rotateX(-24deg)
rotateY(-32deg)
rotateX(90deg)
translateZ(calc((var(--depth) + var(--stack-height)) * -1))
rotate(360deg);
}
}


