All effectsPlay & loop

Twinkling Stars

星点闪烁

Small points of light fade in and out of sync across a still background.

Play or pause the demo

Where to use it

Give a space-themed illustration or success moment a subtle background. Keep the points faint and infrequent near headings so the changing brightness reads as atmosphere.

Practical example

A planetarium’s late-night program card lets a few stars brighten and fade over the dark sky around its illustration. The show time and ticket button sit in a quiet part of the composition.

AI prompt

Add Twinkling Stars behind [target illustration]. Place a small fixed set of points, animate mild opacity changes with different delays, keep them outside the reading area, and show the points at a constant brightness for reduced motion.

Example code

Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.

View HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Twinkling Stars</title>
  <style>
    :root {
      color-scheme: dark;
      --demo-bg: #171629;
      --demo-ink: #f8f5ff;
      --demo-accent: #b9a6ff;
      --demo-muted: #bfbbd3;
      font-family: system-ui, sans-serif;
      color: var(--demo-ink);
      background: var(--demo-bg);
    }

    *, *::before, *::after { box-sizing: border-box; }
    body { margin: 0; min-width: 0; }
    button, input { font: inherit; }

    .demo {
      display: grid;
      place-items: center;
      align-content: center;
      min-height: 100vh;
      padding: 14px;
      overflow: hidden;
      background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
    }

    .demo-button {
      border: 1px solid #ffffff3d;
      border-radius: 7px;
      background: #ffffff12;
      color: var(--demo-ink);
      padding: 6px 11px;
      font-size: 12px;
      cursor: pointer;
    }

    :focus-visible {
      outline: 2px solid var(--demo-accent);
      outline-offset: 3px;
    }

    .star-field {
      position: relative; width: min(100%, 235px); height: 96px; border-radius: 14px;
      background: radial-gradient(ellipse at 75% 95%, #4b2e60, #1b1939 68%);
    }
    .star-field i {
      --x: 19%; --y: 30%; position: absolute; left: var(--x); top: var(--y); width: 9px; aspect-ratio: 1;
      background: #ffdca1; box-shadow: 0 0 9px #ffdca1; rotate: 45deg; opacity: .73;
      animation: twinkle 2.4s ease-in-out infinite alternate;
    }
    .star-field i:nth-child(2) { --x: 43%; --y: 17%; width: 5px; animation-delay: -.52s; }
    .star-field i:nth-child(3) { --x: 72%; --y: 47%; background: #dac8ff; animation-delay: -1.2s; }
    .star-field i:nth-child(4) { --x: 25%; --y: 71%; width: 4px; animation-delay: -.2s; }
    .star-field i:nth-child(5) { --x: 61%; --y: 74%; width: 4px; animation-delay: -1.1s; }
    .star-field i:nth-child(6) { --x: 86%; --y: 23%; width: 6px; animation-delay: -1.65s; }
    .star-field i:nth-child(7) { --x: 47%; --y: 49%; width: 5px; animation-delay: -1.83s; }
    @keyframes twinkle { from { opacity: .29; transform: scale(.7); } to { opacity: .94; transform: scale(1.16); } }

    .motion-example { display: grid; justify-items: center; width: 100%; gap: 10px; }
    .motion-specimen {
      display: grid;
      place-items: center;
      width: min(100%, 295px);
      height: 108px;
      position: relative;
      isolation: isolate;
    }
    .motion-example [data-motion] { animation-play-state: paused; }
    .motion-example[data-playing] [data-motion] { animation-play-state: running; }
    @media (prefers-reduced-motion: reduce) {
      .motion-example [data-motion] { animation: none; }
      .motion-example [data-loop-toggle] { display: none; }
    }
  </style>
</head>
<body>
  <main class="demo">
    <div class="motion-example">
      <div class="motion-specimen" aria-hidden="true"><div class="star-field"><i data-motion></i><i data-motion></i><i data-motion></i><i data-motion></i><i data-motion></i><i data-motion></i><i data-motion></i></div></div>
      <button class="demo-button" type="button" data-loop-toggle data-testid="effect-demo-loop-toggle">Play demo</button>
    </div>
  </main>
  <script>
    const example = document.querySelector('.motion-example');
    const toggle = example.querySelector('[data-loop-toggle]');

    toggle.addEventListener('click', () => {
      const playing = example.toggleAttribute('data-playing');
      toggle.textContent = playing ? "Pause demo" : "Play demo";
    });
  </script>
</body>
</html>

Original example by UI Skills