Same markup, third dimension
The sliders in this series keep the identical HTML and swap only the CSS: a circular one that rotates infinitely and one that flips through a stack of photos came first. This third variant extends the approach into 3D. It appears to be a rotating cube with four images, but there are actually six images in total.
<div class="gallery">
<img src="" alt="">
<img src="" alt="">
<img src="" alt="">
<img src="" alt="">
<img src="" alt="">
</div>
As before, CSS Grid stacks the images one on top of another:
.gallery {
display: grid;
}
.gallery > img {
grid-area: 1 / 1;
width: 160px;
aspect-ratio: 1;
object-fit: cover;
}
Arranging the images around the polygon
The core logic reuses the circular slider. Images sit around a polygon and a single full rotation brings back the first image. The first slider used transform-origin plus animation-delay to pass the same animation to each image with different delays; that does not work in 3D, so the placement here is done with transform, and the container rotates instead. A Sass loop walks through the images:
@for $i from 1 to ($n + 1) {
.gallery > img:nth-child(#{$i}) {
transform:
rotate(#{360*($i - 1) / $n}deg) /* 1 */
translateY(50% / math.tan(180deg / $n)) /* 2 */
rotateX(90deg); /* 3 */
}
}
The transform takes three values:

Each image is rotated by an increment of 360deg/N for N images, then translated by an equal amount so the center points meet on the sides:

The translation distance is 50%/tan(180deg/N) — the same kind of geometry as the circular slider's transform-origin: 50% 50%/sin(180deg/N). Finally the images are rotated 90deg around the x-axis, and the container does the rest:
.gallery {
transform-style: preserve-3d;
--_t: perspective(280px) rotateX(-90deg);
animation: r 12s cubic-bezier(.5, -0.2, .5, 1.2) infinite;
}
@keyframes r {
0%, 3% {transform: var(--_t) rotate(0deg); }
@for $i from 1 to $n {
#{($i/$n)*100 - 2}%,
#{($i/$n)*100 + 3}% {
transform: var(--_t) rotate(#{($i / $n) * -360}deg);
}
}
98%, 100% { transform: var(--_t) rotate(-360deg); }
}
The keyframes are effectively the circular slider's:
.gallery {
animation: m 12s cubic-bezier(.5, -0.2, .5, 1.2) infinite;
}
@keyframes m {
0%, 3% { transform: rotate(0); }
@for $i from 1 to $n {
#{($i / $n) * 100 - 2}%,
#{($i / $n) * 100 + 3}% {
transform: rotate(#{($i / $n) * -360}deg);
}
}
98%, 100% { transform: rotate(-360deg); }
}
Same percentage values, same loop, same rotation — because the logic is identical: images arranged in a circle, whole thing rotated. Two differences: the container is rotated -90deg on the x-axis (since the images are already at 90deg on that axis), and perspective supplies the 3D effect. Adding images beyond the initial count only means updating one variable.
Going vertical
A vertical version is a one-character change: rotate() becomes rotateX() inside the keyframes and the image transform. Since rotate() is equivalent to rotateZ(), swapping the axis from Z to X turns the horizontal slider into the vertical one.
A true cube
The cube version needs six faces, so it uses six images in plain CSS (no Sass). Six faces means five rotations — the last brings you back to the first face, much like turning a Rubik's Cube in your hand.
.gallery {
--s: 250px; /* the size */
transform-style: preserve-3d;
--_p: perspective(calc(2.5*var(--s)));
animation: r 9s infinite cubic-bezier(.5, -0.5, .5, 1.5);
}
@keyframes r {
0%, 3% { transform: var(--_p); }
14%, 19% { transform: var(--_p) rotateX(90deg); }
31%, 36% { transform: var(--_p) rotateX(90deg) rotateZ(90deg); }
47%, 52% { transform: var(--_p) rotateX(90deg) rotateZ(90deg) rotateY(-90deg); }
64%, 69% { transform: var(--_p) rotateX(90deg) rotateZ(90deg) rotateY(-90deg) rotateX(90deg); }
81%, 86% { transform: var(--_p) rotateX(90deg) rotateZ(90deg) rotateY(-90deg) rotateX(90deg) rotateZ(90deg); }
97%, 100%{ transform: var(--_p) rotateX(90deg) rotateZ(90deg) rotateY(-90deg) rotateX(90deg) rotateZ(90deg) rotateY(-90deg); }
}
Each image is attached to a face with transform:
.gallery img {
grid-area: 1 / 1;
width: var(--s);
aspect-ratio: 1;
object-fit: cover;
transform: var(--_t,) translateZ(calc(var(--s) / 2));
}
.gallery img:nth-child(2) { --_t: rotateX(-90deg); }
.gallery img:nth-child(3) { --_t: rotateY( 90deg) rotate(-90deg); }
.gallery img:nth-child(4) { --_t: rotateX(180deg) rotate( 90deg); }
.gallery img:nth-child(5) { --_t: rotateX( 90deg) rotate( 90deg); }
.gallery img:nth-child(6) { --_t: rotateY(-90deg); }
There is no formula behind those values: they were tuned in DevTools until they looked right, which is fine for a fixed image count with no need to support N. Doing the face placement yourself from a stacked start is a reasonable exercise, and different values can work.
The comma inside
var()— is it a typo?
No, and removing it does affect the first image's placement. --_t is defined for every image except the first, which needs only a translation. That comma makes the variable fall back to a null value; without it there is no fallback and the value is invalid. The spec puts it plainly: var(--a,) is valid and replaces the var() with nothing when --a is invalid or missing.
Random cube
Instead of rotating sequentially, the cube can roll as if the dice were thrown. The effect only looks random: each keyframe declares a transform that shows one face, in whatever order you choose.
@keyframes r {
0%, 3% { transform: var(--_p) rotate3d( 0, 0, 0, 0deg); }
14%,19% { transform: var(--_p) rotate3d(-1, 1, 0,180deg); }
31%,36% { transform: var(--_p) rotate3d( 0,-1, 0, 90deg); }
47%,52% { transform: var(--_p) rotate3d( 1, 0, 0, 90deg); }
64%,69% { transform: var(--_p) rotate3d( 1, 0, 0,-90deg); }
81%,86% { transform: var(--_p) rotate3d( 0, 1, 0, 90deg); }
97%,100% { transform: var(--_p) rotate3d( 0, 0, 0, 0deg); }
}
This version uses rotate3d(), with values found by feel in DevTools — there is no relationship between the keyframes, so do not hunt for one. Keep the first image as the first and last frames and show a different image on each of the others. Chained rotations like the previous example work equally well.
What carries across the series
Across all the sliders — grid placement and stacking order, animation delays, transforms, and a little Sass to loop over an array of elements — the HTML never changed. Everything here is CSS, no JavaScript.



