AI CSS Animations

3D Carousel

Six cards stand in a ring and the ring turns one card at a time, pausing on each. Each card is rotateY(60deg times --i) then pushed out with translateZ; the ring itself is the only thing animated.

Also known as: css 3d carousel animation, rotating carousel css, 3d ring of cards.

HTML

<div class="kfa-3d-carousel"><div class="kfa-3d-carousel-ring"><span style="--i: 0">1</span><span style="--i: 1">2</span><span style="--i: 2">3</span><span style="--i: 3">4</span><span style="--i: 4">5</span><span style="--i: 5">6</span></div></div>

CSS

.kfa-3d-carousel {
  --c: var(--kfa-color, #3b82f6);
  font-size: var(--kfa-size, 16px);
  width: 2.8em;
  height: 3.8em;
  perspective: 26em;
}
.kfa-3d-carousel-ring {
  position: relative;
  height: 100%;
  transform-style: preserve-3d;
  animation: kfa-3d-carousel-spin 7.2s cubic-bezier(0.65, 0, 0.35, 1) infinite;
}
.kfa-3d-carousel-ring > span {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: 0.45em;
  background: var(--c);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--c) 55%, #fff);
  color: #fff;
  font-family: system-ui, sans-serif;
  font-weight: 700;
  font-size: 1.1em;
  transform: rotateY(calc(var(--i) * 60deg)) translateZ(4.2em);
  transform-style: preserve-3d;
  backface-visibility: hidden;
}
/* the back of each card: a pale slab that only shows while the card faces away */
.kfa-3d-carousel-ring > span::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: color-mix(in srgb, var(--c) 22%, transparent);
  transform: rotateY(180deg);
  backface-visibility: hidden;
}
@keyframes kfa-3d-carousel-spin {
  0%, 11% { transform: rotateX(-6deg) rotateY(0); }
  16.67%, 27.67% { transform: rotateX(-6deg) rotateY(-60deg); }
  33.33%, 44.33% { transform: rotateX(-6deg) rotateY(-120deg); }
  50%, 61% { transform: rotateX(-6deg) rotateY(-180deg); }
  66.67%, 77.67% { transform: rotateX(-6deg) rotateY(-240deg); }
  83.33%, 94.33% { transform: rotateX(-6deg) rotateY(-300deg); }
  100% { transform: rotateX(-6deg) rotateY(-360deg); }
}

@media (prefers-reduced-motion: reduce) {
  .kfa-3d-carousel, .kfa-3d-carousel *, .kfa-3d-carousel::before, .kfa-3d-carousel::after, .kfa-3d-carousel *::before, .kfa-3d-carousel *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

  • Color: set --kfa-color on the element or a parent. Currently previewing #3b82f6.
  • Base size: set --kfa-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 16px.
  • Speed: change the duration on the transition or animation shorthand. Staggered delays are relative, so they keep their rhythm.
  • Reduced motion: the rule at the end of the CSS turns the motion off for people who asked their OS for less of it. Keep it.

Similar keyframe animations