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.
Dependencies
react
npm install react react-domconst 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>
);
}
Please implement this code in the current project and add it to (Your Desired Section, e.g. homepage hero, pricing page, dashboard). Change the primary accent color to (YOUR HEX CODE COLOR, e.g. #6366F1).
Requirements:
- Use React + Tailwind CSS (match the project's existing setup)
- Install dependencies if needed: npm install react react-dom
- Keep the layout responsive on mobile and desktop
- Replace placeholder copy with content that fits the project
The UI code:
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>
);
}
Replace (Your Desired Section) and (YOUR HEX CODE COLOR) before pasting into Cursor, Copilot, or ChatGPT.