AI CSS Animations

3D Rotating Cube

A real six-faced cube turning in 3D space with translucent faces, so you can see the back edges through the front. Uses preserve-3d and a perspective on the parent.

Also known as: 3D cube loader, rotating box.

HTML

<div class="ldr-cube-3d" role="status" aria-label="Loading">
  <div><span></span><span></span><span></span><span></span><span></span><span></span></div>
</div>

CSS

.ldr-cube-3d {
  --s: var(--ldr-size, 48px);
  --c: var(--ldr-color, #3b82f6);
  --h: calc(var(--s) * 0.35);
  width: calc(var(--s) * 0.7);
  height: calc(var(--s) * 0.7);
  margin: calc(var(--s) * 0.15);
  perspective: calc(var(--s) * 4);
}
.ldr-cube-3d > div {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  animation: ldr-cube-3d-turn 3s linear infinite;
}
.ldr-cube-3d span {
  position: absolute;
  inset: 0;
  border: 1px solid var(--c);
  background: color-mix(in srgb, var(--ldr-color, #3b82f6) 55%, transparent);
}
.ldr-cube-3d span:nth-child(1) { transform: rotateY(0deg)   translateZ(var(--h)); }
.ldr-cube-3d span:nth-child(2) { transform: rotateY(90deg)  translateZ(var(--h)); }
.ldr-cube-3d span:nth-child(3) { transform: rotateY(180deg) translateZ(var(--h)); }
.ldr-cube-3d span:nth-child(4) { transform: rotateY(-90deg) translateZ(var(--h)); }
.ldr-cube-3d span:nth-child(5) { transform: rotateX(90deg)  translateZ(var(--h)); }
.ldr-cube-3d span:nth-child(6) { transform: rotateX(-90deg) translateZ(var(--h)); }
@keyframes ldr-cube-3d-turn {
  from { transform: rotateX(-25deg) rotateY(0deg); }
  to   { transform: rotateX(-25deg) rotateY(360deg); }
}

@media (prefers-reduced-motion: reduce) {
  .ldr-cube-3d, .ldr-cube-3d *, .ldr-cube-3d::before, .ldr-cube-3d::after {
    animation-duration: 3s;
  }
}

Customizing this loader

  • Color: set --ldr-color on the element or a parent. Currently previewing #3b82f6.
  • Size: set --ldr-size. Every dimension in the snippet is derived from it. Currently previewing 64px.
  • Speed: change the duration on the animation shorthand. Delays on the children are relative, so they keep their rhythm.
  • Accessibility: the root carries role="status" and an aria-label so screen readers announce the wait. The reduced-motion rule at the end slows the animation for users who asked for less motion.

Similar loaders