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.
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%",
}}
/>
);
}@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.
- 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.
- Adapt quality. Measure frame time and drop render resolution a step when frames run long. Soft backgrounds hide the difference.
- Pause off screen. Stop the loop with an IntersectionObserver when the canvas scrolls away, and on
visibilitychangewhen the tab is hidden. - Respect reduced motion. Under
prefers-reduced-motion: reduce, draw one frame and stop. - 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
uibrik's backgrounds follow every rule above: capped pixel ratio, adaptive quality, pause off screen, a still frame for reduced motion and a server-rendered CSS fallback in both themes. Install any with the shadcn CLI.
npx shadcn@latest add https://uibrik.com/r/mesh-gradient.json- Mesh GradientProSix drifting points of colour melting into each other along soft, curling seams.
- Grain GradientColour that folds slowly like silk, finished with film grain.
- AuroraSlow curtains of light with fine rays that lean toward the cursor.
- Silk ClothA sheet of satin rippling in a light breeze, with a long sheen along every fold.
- Dot GridA quiet grid of dots lit by a drifting wave and by the cursor.
- CausticsSunlight on a pool floor, from light rays bent by moving water; click to drop a ripple.
- Light RainThin lines of blue light falling through the dark, with bright glints sliding along them.
- Halftone HorizonProAn arched horizon printed in halftone in the dark, where the dots grow with the light until they fuse into a smooth glow.
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.