185 Pro components and page blocks

Get Pro
All guides

Animated backgrounds in React

CSS for soft colour, WebGL for light and texture. How to pick, and how to keep either one fast.

Updated

CSS or WebGL

Pick by what the background has to show.

  • CSS for a slow gradient shift or two or three soft blobs. It costs almost nothing and needs no JavaScript.
  • A WebGL shader for light, grain, folds, ripples or anything with many moving parts. It draws every pixel on the GPU in one pass.

The trap is the middle: stacking large blurred CSS layers to fake light. Big filter: blur() layers repaint every frame, get slow on phones, and show colour banding on wide gradients. Once you reach for a third blur, a shader is usually faster and looks smoother.

A CSS background

Animate the background position, not the layers: it's cheap and respects reduced motion.

gradient-background.tsx
export function GradientBackground() {
  return (
    <div
      aria-hidden
      className="absolute inset-0 -z-10 animate-[drift_18s_ease-in-out_infinite_alternate] motion-reduce:animate-none"
      style={{
        backgroundImage: [
          "radial-gradient(60% 50% at 20% 30%, color-mix(in oklch, var(--primary) 25%, transparent), transparent)",
          "radial-gradient(50% 40% at 80% 70%, color-mix(in oklch, var(--primary) 15%, transparent), transparent)",
        ].join(", "),
        backgroundSize: "140% 140%",
      }}
    />
  );
}
globals.css
@keyframes drift {
  from { background-position: 0% 0%; }
  to { background-position: 100% 100%; }
}

Keeping WebGL fast

A shader background runs every frame, so it needs a budget. Five rules cover most of it.

  1. Cap the pixel ratio. A background rarely needs more than 1.5× device pixels. On a 3× phone that cuts the work by three quarters.
  2. Adapt quality. Measure frame time and drop render resolution a step when frames run long. Soft backgrounds hide the difference.
  3. Pause off screen. Stop the loop with an IntersectionObserver when the canvas scrolls away, and on visibilitychange when the tab is hidden.
  4. Respect reduced motion. Under prefers-reduced-motion: reduce, draw one frame and stop.
  5. Load it late. Import the shader component lazily so it never delays the content above it.

Before it loads

Render a CSS gradient in the shader's colours on the server. The first paint then matches, the shader fades in over it, and the page still looks right where WebGL is off or fails. Give it a version for each theme so dark mode doesn't flash light.

Text on top

Contrast

Check contrast against the brightest point the animation reaches, not the average. Keep colours in one hue family behind text: shades of one colour read calmer than a rainbow and keep contrast predictable.

Focus

Mark the canvas aria-hidden and keep it out of the tab order. Pointer effects are fine, but nothing on the page should depend on them.

Ready-made backgrounds

Questions

Should an animated background use CSS or WebGL?
Use CSS for a few soft colour blobs or a slow gradient shift. Use a WebGL shader for anything with light, grain, folds or many moving parts: it draws on the GPU in one pass, where stacked CSS blurs get slow and band.
How do I keep a WebGL background fast?
Cap the pixel ratio, drop render resolution when frames run long, pause when the background is off screen or the tab is hidden, and draw one still frame when the user prefers reduced motion.
What should show before the shader loads?
A CSS gradient in the same colours, rendered on the server, so the first paint matches and the page still looks right if WebGL is unavailable.

More guides