How it works

Mirrors from clip-path and trigonometry

A kaleidoscope is two or three mirrors in a tube: whatever lies at the far end is seen once directly and then reflected round and round into a symmetric star. CSS can't reflect light, but it can draw the same picture several times, cut each copy to a slice and turn it into place.

1. One wedge: clip-path: polygon()

With 8 segments every wedge spans 360° / 8 = 45°. Each copy of the source sits in a square layer centered on the kaleidoscope and is cut to the same upward-pointing triangle: the apex at the center, the other two corners on the top edge, half an angle to either side of the vertical. The top edge is half a side away from the center, so the corners are at 50% ± 50% × tan(half angle). CSS has had tan() since the trig functions landed, so the polygon can be written once and works for any segment count:

clip-path: polygon(
  50% calc(50% + 2px),
  calc(50% - 50% * tan(var(--half))) 0%,
  calc(50% + 50% * tan(var(--half))) 0%
);

/* for 8 segments that resolves to
   polygon(50% calc(50% + 2px), 29.0333% 0%, 70.9667% 0%) */

A triangle, not a pie slice, because its base lies on the layer's edge, which is tangent to the largest circle that fits in the square. That circle is bigger than the box's half diagonal, so the triangles together cover every corner. Each wedge is also widened by 0.25° on both sides: antialiased edges of two clips that meet exactly let a hairline of the background shine through, and the small overlap hides it. Close to the center an angle of a quarter degree is far thinner than a pixel, so the apex also moves 2px past the center.

2. Turned into place: rotate with calc()

Copy i gets rotate: calc(var(--i) * 360deg / var(--n)). The rotation turns both the clip and the content, since clip-path is applied in the element's own coordinates before its transform. Without mirroring this is plain rotational symmetry, and every edge is a hard seam where the source doesn't continue.

3. Mirrors: flip every other copy

A real mirror at the wedge's edge shows the wedge reflected across that edge. With the edge at half a wedge angle θ/2 from the vertical, that reflection is the same as flipping the wedge horizontally (scale: -1 1, which maps it onto itself, mirrored) and then rotating it by θ. So the odd copies get scale: -1 1 on top of their rotation. A point on the shared edge lands on the same spot in both neighbors, and the pattern runs continuously across every edge. That only closes up all the way round with an even number of wedges, which is why Segments moves in steps of two.

<div class="kaleidoscope">
  <div class="ring" aria-hidden="true">
    <div class="segment" style="--i: 0"><div class="source"></div></div>
    <div class="segment" style="--i: 1"><div class="source"></div></div>
    <!-- … up to --i: 7 -->
  </div>
</div>
.kaleidoscope {
  --n: 8;
  container-type: size;
  position: relative;
  overflow: hidden;
}

/* a square that covers the box at any angle */
.ring {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 142cqmax;
  aspect-ratio: 1;
  translate: -50% -50%;
}

/* every copy: the same wedge, pointing up, turned into place */
.segment {
  --half: calc(180deg / var(--n) + 0.25deg);
  position: absolute;
  inset: 0;
  overflow: hidden;
  clip-path: polygon(
    50% calc(50% + 2px),
    calc(50% - 50% * tan(var(--half))) 0%,
    calc(50% + 50% * tan(var(--half))) 0%
  );
  rotate: calc(var(--i) * 360deg / var(--n));
}

/* the odd copies are mirror images */
.segment:nth-child(even) {
  scale: -1 1;
}

/* the source: big enough to pan and spin without showing its edges */
.source {
  position: absolute;
  inset: -25%;
  background: url(tile.jpg) center / cover;
  translate: var(--px, 0%) var(--py, 0%);
  scale: 1.5;
  transition: translate 600ms ease-out;
  animation: spin 60s linear infinite;
}

@keyframes spin { to { rotate: 360deg; } }

@media (prefers-reduced-motion: reduce) {
  .source { animation: none; transition: none; }
}

4. Turning the tube

In a real kaleidoscope the mirrors stay put and the beads tumble. Here the source inside every wedge rotates with one CSS animation, the same in all copies, and the flipped copies turn the other way automatically. The visible part of each wedge is a disc of half the layer's side, and a square that spins about its center always covers the circle inscribed in it, so without panning a source of the layer's size is enough. The pointer pans the source by up to Pan range × half a side; the source grows by that range so its inscribed circle still covers the disc at the largest offset, and its edges never come into view. A transition on translate makes the pattern glide after the pointer.

Motion and accessibility

  • Under prefers-reduced-motion the source neither spins nor follows the pointer. Large, symmetric patterns in motion are a typical trigger for vestibular discomfort. The Pause animations button stops the spin (WCAG 2.2.2).
  • All copies are aria-hidden. Text goes in children, which render once on top; put it on its own surface, since contrast over a moving pattern can't be guaranteed.

Browser support and performance

clip-path with basic shapes is Baseline widely available, and the trig functions (sin(), cos(), tan()) are Baseline since 2023 and widely available by now. A browser without trig functions treats the polygon as invalid; an @supports rule then shows one unclipped copy of the source instead of a pile of overlapping ones. Checked in Chromium only; Firefox and Safari are untested.

  • There are n live copies of the source, so n times the paint. An image (src) is decoded once and only drawn n times; inline SVG (try the landscape) is laid out n times. Keep interactive DOM out of it.
  • The spin animates rotate, which the compositor can run without repainting the content, but every frame still redraws n clipped layers. At 24 segments on a large, high DPI screen that adds up (judgment, not measured).

Back to the demo ↑