3D carousel in CSS: a rotating ring of cards with perspective
Build a 3D carousel in CSS: cards on a ring with perspective and preserve-3d, the radius formula, buttons and arrow keys, a front highlight and a React version.
A 3D carousel places cards around a circle in space and turns the whole circle to bring the next card forward. The cards at the sides recede and shrink, which gives a sense of depth that a flat slider does not have. It works well for small sets: a handful of plans, featured projects, or product colours.
The effect needs no WebGL and no library. It is a parent with perspective, a ring with transform-style: preserve-3d, and one line of trigonometry to space the cards. This guide builds the ring, adds buttons and arrow keys, highlights the front card, handles back faces, and ends with a React version and performance notes.
Card 1 of 8
How the animation is put together
Only one element moves: the ring. The cards are placed once with a static transform and ride along with it. The highlight on the front card is a short transition on the card itself.
| Part | Property | Duration | Delay | Easing |
|---|---|---|---|---|
| Ring | transform: rotateY(), one step = 45deg | 700 ms | 0 | cubic-bezier(.22, 1, .36, 1) |
| New front card | opacity .55 → 1, border-color, box-shadow | 300–400 ms | 200 ms | ease |
| Previous front card | opacity 1 → .55, border and shadow off | 300–400 ms | 0 | ease |
| Back cards | hidden by backface-visibility | n/a | n/a | n/a |
| Reduced motion | ring and card transitions removed | instant | 0 | none |
The 200 ms delay on the new front card lets the ring do most of its turn before the highlight lands, so the accent arrives as the card settles instead of flashing on while it is still at the side. The outgoing card loses its highlight at once, because the delay is only set on the .is-active state and a transition uses the timing of the state it is moving to.
The geometry: a ring with no gaps
Picture the cards as the sides of a regular polygon seen from above. With n cards, each card is turned 360deg / n further than the one before. To stop neighbouring cards from overlapping or leaving holes, each card is pushed out from the centre by the polygon's inradius:
radius = (w / 2) / tan(π / n)
w = 120px, gap = 14px, n = 8
radius = (134 / 2) / tan(22.5deg) = 67 / 0.4142 ≈ 162px
Adding a small gap to the width before you divide keeps a sliver of space between cards. More cards push the ring out quickly, so eight or fewer suits a normal content column.
The ring is then moved back by the same radius with translateZ(-radius). That puts the front card at z = 0, where it renders at its real size with crisp text, and the others sit behind it.
The HTML
<div class="c3d">
<div class="c3d-stage" tabindex="0" role="group" aria-roledescription="carousel"
aria-label="Colour themes, use the left and right arrow keys">
<div class="c3d-ring">
<div class="c3d-card" style="--i:0; --g:linear-gradient(140deg,#fb9f3d,#e2496b)">Ember</div>
<div class="c3d-card" style="--i:1; --g:linear-gradient(140deg,#38bdf8,#6366f1)">Lagoon</div>
<!-- … up to --i:7 -->
</div>
</div>
<div class="c3d-controls">
<button type="button" class="c3d-btn" data-dir="-1" aria-label="Previous card">‹</button>
<p class="c3d-status" aria-live="polite">Card 1 of 8</p>
<button type="button" class="c3d-btn" data-dir="1" aria-label="Next card">›</button>
</div>
</div>
Each card only needs its index (--i) and its background. The stage is focusable so the arrow keys work once it has focus, and the status line is a live region that tells screen reader users which card is in front.
The CSS
.c3d-stage {
height: 200px;
display: grid; place-items: center;
perspective: 1000px; /* on the parent, not the ring */
}
.c3d-ring {
--n: 8; --w: 120px; --gap: 14px;
--angle: calc(360deg / var(--n));
--r: 170px; /* fallback radius */
position: relative; width: var(--w); height: 160px;
transform-style: preserve-3d;
transform: translateZ(calc(var(--r) * -1))
rotateY(calc(var(--step, 0) * var(--angle) * -1));
transition: transform 700ms cubic-bezier(.22, 1, .36, 1);
}
/* browsers with CSS trig functions compute the exact radius */
@supports (width: calc(1px * tan(45deg))) {
.c3d-ring { --r: calc((var(--w) + var(--gap)) / 2 / tan(180deg / var(--n))); }
}
.c3d-card {
position: absolute; inset: 0;
border-radius: 16px; border: 1px solid rgba(255, 255, 255, .1);
background: var(--g), #1c1f26; color: #fff;
backface-visibility: hidden;
transform: rotateY(calc(var(--i) * var(--angle))) translateZ(var(--r));
opacity: .55;
transition: opacity 400ms ease, border-color 300ms ease, box-shadow 300ms ease;
}
.c3d-card.is-active {
opacity: 1; border-color: #fb9f3d;
box-shadow: 0 0 0 3px rgba(251, 159, 61, .25), 0 18px 40px rgba(0, 0, 0, .45);
transition-delay: 200ms;
}
@media (prefers-reduced-motion: reduce) {
.c3d-ring, .c3d-card { transition: none; }
}
Card transforms read right to left: push the card out along its own z axis by the radius, after turning it to its slot on the circle. The ring's transform does the reverse for the viewer: move the whole ring back, then turn it by step × angle.
CSS trigonometric functions are supported in all current major browsers, but the fixed fallback costs nothing and keeps older engines from collapsing every card into the centre. If you would rather not depend on them at all, compute the radius in JavaScript, as the React version below does.
Back faces
backface-visibility: hidden hides any card that has turned more than 90 degrees away from the viewer, so you only see the front half of the ring. Without it, the back cards show through mirrored, which looks busy behind a semi-transparent front card. If you prefer to see the full ring, keep the back faces visible and dim them more strongly instead. Safari still wants the -webkit- prefixed property alongside the standard one in some versions, so include both.
The JavaScript
const root = document.querySelector('.c3d');
const stage = root.querySelector('.c3d-stage');
const ring = root.querySelector('.c3d-ring');
const cards = [...root.querySelectorAll('.c3d-card')];
const status = root.querySelector('.c3d-status');
const n = cards.length;
let step = 0;
function render() {
const active = ((step % n) + n) % n; // works for negative steps too
ring.style.setProperty('--step', step);
cards.forEach((card, i) => {
card.classList.toggle('is-active', i === active);
card.setAttribute('aria-hidden', String(i !== active));
});
status.textContent = `Card ${active + 1} of ${n}`;
}
const go = dir => { step += dir; render(); };
root.querySelectorAll('.c3d-btn').forEach(btn =>
btn.addEventListener('click', () => go(Number(btn.dataset.dir))));
stage.addEventListener('keydown', e => {
if (e.key === 'ArrowRight') { e.preventDefault(); go(1); }
if (e.key === 'ArrowLeft') { e.preventDefault(); go(-1); }
});
render();
The important detail is that step is never wrapped. Going from card 8 to card 1 takes the rotation from -315deg to -360deg, a single 45 degree turn. If you reset the rotation to 0 at the end, the ring spins all the way back through every card. Only the index of the active card uses the remainder.
The React version
import { useState } from 'react';
export function Carousel3D({ items, cardWidth = 120, gap = 14, label = 'Featured' }) {
const [step, setStep] = useState(0);
const n = items.length;
const angle = 360 / n;
const radius = Math.round((cardWidth + gap) / 2 / Math.tan(Math.PI / n));
const active = ((step % n) + n) % n;
const onKeyDown = e => {
if (e.key === 'ArrowRight') { e.preventDefault(); setStep(s => s + 1); }
if (e.key === 'ArrowLeft') { e.preventDefault(); setStep(s => s - 1); }
};
return (
<div className="c3d">
<div className="c3d-stage" tabIndex={0} role="group" aria-roledescription="carousel"
aria-label={label} onKeyDown={onKeyDown}>
<div className="c3d-ring" style={{
'--n': n, '--w': cardWidth + 'px', '--r': radius + 'px',
'--angle': angle + 'deg', '--step': step }}>
{items.map((item, i) => (
<div key={item.id} style={{ '--i': i, '--g': item.gradient }}
className={'c3d-card' + (i === active ? ' is-active' : '')}
aria-hidden={i !== active}>
{item.title}
</div>
))}
</div>
</div>
<div className="c3d-controls">
<button type="button" className="c3d-btn" aria-label="Previous card"
onClick={() => setStep(s => s - 1)}>‹</button>
<p className="c3d-status" aria-live="polite">Card {active + 1} of {n}</p>
<button type="button" className="c3d-btn" aria-label="Next card"
onClick={() => setStep(s => s + 1)}>›</button>
</div>
</div>
);
}
The CSS is the same file as above. Setting --r inline overrides the @supports value, so the component works whether or not the browser has trig functions. If you animate with Framer Motion instead, animate rotateY: -step * angle on the ring and keep the same unbounded step.
Timing that feels right
- 600 to 800 ms per step. A 45 degree turn in less than about 500 ms looks like a jump, because the side cards travel a long way across the screen. Much more than 900 ms feels sluggish when someone clicks several times.
- A strong ease-out.
cubic-bezier(.22, 1, .36, 1)starts fast and settles gently, so the ring responds immediately to the click. Avoid overshoot here: a ring that swings past and comes back makes the front card wobble at the moment people try to read it. The easing functions guide compares the common curves. - Highlight after the turn. Delay the front card's accent by roughly a third of the ring duration.
- Perspective around 800 to 1200 px. Lower values exaggerate the depth and distort the side cards; higher values flatten the ring.
Performance notes
- Animate one transform. The ring's
transformis the only animated property that moves anything, and transforms can be handled by the compositor without layout or repaint. Do not rotate each card separately. - Keep the ring a 3D context.
opacitybelow 1,filter,overflowother than visible,clip-pathandmaskon the ring all force it to flatten, and the carousel collapses into a flat row. Put those effects on the cards or on the stage instead. - Watch heavy shadows. Large blurred shadows on every card cost paint time.
- Size images to the card. If cards hold photos, serve them at the card size and lazy-load them.
- No autoplay by default. If you add autoplay, stop it when the carousel is off-screen or the tab is hidden, and add a pause button, the same way the orbit animation does, because motion that starts on its own and runs for more than five seconds needs one.
Accessibility and reduced motion
Under prefers-reduced-motion: reduce the transitions are removed, so a button press swaps the front card instantly. The carousel still works and still shows which card is active; it just does not turn. The aria-hidden attribute on the cards that are not in front keeps screen readers on the current card, and the live region announces the position after each change. If cards contain links or buttons, make the inactive ones unfocusable as well, for example with the inert attribute.
Copy one you already like
3D carousels differ in small ways that are hard to see at full speed: the perspective value, how far the side cards dim, whether the ring overshoots. To match one you have seen, open the page, start the Capture Motion extension and turn the carousel while it records. It reads the keyframes, duration, delay and easing of the rotating element, whether it runs on CSS, the Web Animations API or GSAP, and exports CSS, React, GSAP or JSON for the Figma plugin. For ready-made animated components you can customize, browse the gallery.
Questions
How do I work out the radius of a 3D carousel?
For n cards of width w, the radius is (w / 2) / tan(π / n), or tan(180deg / n) in CSS. Add a small gap to w first if you want space between neighbouring cards.
Why does my 3D carousel look flat?
Usually something on the ring flattens it: a missing transform-style: preserve-3d, or opacity, filter, overflow: hidden or clip-path on the ring element. Also check that perspective is set on the parent of the ring, not on the ring itself.
Why does the carousel spin all the way round when it wraps from the last card to the first?
The rotation was reset to 0 instead of continuing. Keep an unbounded step counter, rotate by step × angle, and only use the remainder (step mod n) to find which card is in front.
Can I copy a 3D carousel from another website?
Yes. The Capture Motion Chrome extension records the transform keyframes, duration and easing of the ring while it turns, including GSAP and Web Animations API carousels, and exports them as CSS, React, GSAP or JSON.
