All effectsPlay & loop

Meteor Shower

流星雨

Short trails streak diagonally across a dark sky at irregular intervals and fade as they travel.

Play the demo to see meteors cross the sky

Where to use it

Give a dark hero backdrop occasional movement around the headline. Limit how many meteors overlap and leave enough time between crossings for the text to be read.

Practical example

A planetarium introduces its meteor calendar with a dark cover. A brief diagonal trail crosses above the date every few seconds.

Also called

  • Meteors
  • Shooting Stars

AI prompt

Add a Meteor Shower to [hero background]. Thin, slightly angled streaks appear at random intervals, travel quickly, and fade. Keep density low so the headline stays readable. Show a still sky 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>Meteor Shower</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;
    }

    .demo { position: relative; padding: 0; }
    .showcase { position: absolute; inset: 0; isolation: isolate; overflow: hidden; }
    .showcase .demo-button {
      position: absolute;
      z-index: 3;
      bottom: 10px;
      left: 50%;
      transform: translateX(-50%);
      border-radius: 999px;
      background: #101026d9;
      color: #f1efff;
      white-space: nowrap;
    }
    .showcase :is(h1, p) { margin: 0; }
    .scene-copy {
      position: absolute;
      z-index: 1;
      inset: 0;
      display: grid;
      place-content: center;
      padding: 14px;
      text-align: center;
      pointer-events: none;
    }
    .scene-copy h1 {
      font-size: clamp(25px, 8vw, 62px);
      line-height: 1;
      letter-spacing: -.045em;
    }
    .showcase [data-motion] { animation-play-state: paused; }
    .showcase[data-playing] [data-motion] { animation-play-state: running; }
    @media (prefers-reduced-motion: reduce) {
      .showcase [data-motion] { animation: none; }
      .showcase [data-loop-toggle] { display: none; }
    }

    .showcase {
      background:
        radial-gradient(ellipse at 76% 72%, #352458a0, transparent 63%),
        radial-gradient(ellipse at 12% 97%, #394c77, transparent 47%),
        #0c1124;
    }
    .night-sky, .moving-meteors { position: absolute; inset: 0; }
    .night-sky {
      background:
        radial-gradient(circle at 13% 29%, #fff8 0 1px, transparent 1.5px),
        radial-gradient(circle at 61% 21%, #b9c5ffa8 0 1px, transparent 1.5px),
        radial-gradient(circle at 69% 74%, #fff7 0 1px, transparent 1.5px),
        radial-gradient(circle at 93% 45%, #b9c5ffaa 0 1px, transparent 1.5px),
        radial-gradient(circle at 29% 83%, #b9c5ffaa 0 1px, transparent 1.5px);
    }
    .meteor {
      position: absolute;
      display: block;
      width: clamp(34px, 20vw, 106px);
      height: 1.5px;
      border-radius: 99px;
      background: linear-gradient(90deg, transparent, #c2b5ff85 65%, #ece6ff);
      transform: rotate(146deg);
      filter: drop-shadow(0 0 4px #8b8bf1);
    }
    .still-meteor { left: 17%; top: 26%; }
    .still-meteor:nth-child(2) { left: 65%; top: 23%; opacity: .68; width: clamp(29px, 13vw, 83px); }
    .still-meteor:nth-child(3) { left: 72%; top: 69%; opacity: .79; }
    .showcase[data-playing] .still-meteor { visibility: hidden; }
    .scene-copy h1 {
      color: #f2efff;
      font-family: Georgia, serif;
      font-weight: 400;
      text-shadow: 0 1px 18px #090d22, 0 2px 10px #090d22;
    }
  </style>
</head>
<body>
  <main class="demo">
    <div class="showcase">
      <div class="night-sky" aria-hidden="true">
      <i class="meteor still-meteor"></i><i class="meteor still-meteor"></i><i class="meteor still-meteor"></i>
      <div class="moving-meteors"></div>
    </div>
    <div class="scene-copy"><h1>Meteor Night</h1></div>
      <button class="demo-button" type="button" data-loop-toggle data-testid="effect-demo-loop-toggle">Play demo</button>
    </div>
  </main>
  <script>
    const showcase = document.querySelector('.showcase');
    const toggle = showcase.querySelector('[data-loop-toggle]');
    const preference = matchMedia('(prefers-reduced-motion: reduce)');
    let playing = false;

    const meteors = showcase.querySelector('.moving-meteors');
    let nextMeteor = null;

    function shootMeteor() {
      if (meteors.childElementCount < 3) {
        const trail = document.createElement('i');
        trail.className = 'meteor';
        const height = meteors.clientHeight;
        const width = meteors.clientWidth;
        const distance = Math.min(width * (.36 + Math.random() * .26), height * 1.3);
        trail.style.left = (24 + Math.random() * 80) + '%';
        trail.style.top = (Math.random() * 69 - 15) + '%';
        meteors.append(trail);
        const flight = trail.animate([
          { transform: 'translate(0, 0) rotate(146deg)', opacity: 0, offset: 0 },
          { opacity: .92, offset: .21 },
          { transform: 'translate(' + -distance + 'px, ' + distance * .675 + 'px) rotate(146deg)', opacity: 0, offset: 1 },
        ], { duration: 640 + Math.random() * 470, easing: 'linear' });
        flight.onfinish = () => trail.remove();
      }
      nextMeteor = setTimeout(shootMeteor, 510 + Math.random() * 1000);
    }

    function setScenePlayback(active) {
      clearTimeout(nextMeteor);
      for (const trail of meteors.children) {
        trail.getAnimations().forEach((animation) => animation.cancel());
      }
      meteors.replaceChildren();
      if (active) shootMeteor();
    }

    function syncPlayback() {
      if (preference.matches) playing = false;
      const active = playing && !document.hidden;
      showcase.toggleAttribute('data-playing', active);
      toggle.textContent = playing ? "Pause demo" : "Play demo";
      setScenePlayback(active);
    }

    toggle.addEventListener('click', () => {
      playing = !playing;
      syncPlayback();
    });
    preference.addEventListener('change', syncPlayback);
    document.addEventListener('visibilitychange', syncPlayback);
  </script>
</body>
</html>

Original example by UI Skills