TetraKits UI Animated Starfield ← Library

Animated Starfield

Animated starfield with 80 twinkling stars and subtle emerald nebula tint on deep black. Lightweight React background using CSS keyframes. Ideal for space-themed SaaS, portfolio, and AI product pages.

starfield backgroundtwinkling stars reactspace background cssanimated starsdark hero bg
Live preview

Dependencies

react
npm install react react-dom
const STARS = Array.from({ length: 80 }, (_, i) => ({
  id: i,
  left: `${(i * 17 + 3) % 100}%`,
  top: `${(i * 23 + 7) % 100}%`,
  size: i % 3 === 0 ? 2 : 1,
  delay: `${(i % 10) * 0.4}s`,
}));

export function StarfieldBackground({ children }: { children?: React.ReactNode }) {
  return (
    <div className="relative min-h-screen overflow-hidden bg-[#030712]">
      <div className="pointer-events-none absolute inset-0 bg-[radial-gradient(ellipse_at_50%_0%,rgba(16,185,129,0.12),transparent_60%)]" aria-hidden />
      {STARS.map((s) => (
        <span
          key={s.id}
          className="pointer-events-none absolute rounded-full bg-white"
          style={{
            left: s.left, top: s.top, width: s.size, height: s.size,
            animation: `twinkle 2.5s ease-in-out ${s.delay} infinite`,
            boxShadow: s.size > 1 ? "0 0 6px rgba(255,255,255,0.8)" : undefined,
          }}
          aria-hidden
        />
      ))}
      <style>{`@keyframes twinkle{0%,100%{opacity:.15}50%{opacity:1}}`}</style>
      {children && <div className="relative z-10">{children}</div>}
    </div>
  );
}