December 4, 2026
You can build an animated gradient background in three ways: with CSS, with a 2D canvas, or with a WebGL shader. Use CSS for a slow shift between a few colours. Use WebGL for the flowing, liquid look. Use a 2D canvas when you want that look without writing a shader. Each has a different cost, and the most copied CSS version is the most wasteful.
How do the three compare?
| Method | Look | Cost | Code |
|---|---|---|---|
| CSS | Colours or blobs drifting | Low, if you avoid repaints | A few lines |
| 2D canvas | Waves, plasma, soft noise | Medium; runs on the processor | 50 to 150 lines |
| WebGL shader | Smooth flowing noise, any complexity | Low to medium; runs on the graphics card | 100 lines plus GLSL |
What is the right way in CSS?
@property --a { syntax: "<angle>"; inherits: false; initial-value: 0deg; }
@property --c1 { syntax: "<color>"; inherits: false; initial-value: #0b3d2e; }
.bg {
background: linear-gradient(var(--a), var(--c1), #c6f432);
animation: drift 12s ease-in-out infinite alternate;
}
@keyframes drift { to { --a: 180deg; --c1: #1db954; } }
@media (prefers-reduced-motion: reduce) { .bg { animation: none; } }
@property tells the browser that --a is an angle and --c1 a colour, so it can blend
between values. Without the registration the variables jump from one value to the next.
What is the common mistake?
The snippet found everywhere sets background-size: 400% 400% and animates
background-position. It works, but the browser must repaint the full gradient on every frame, on the processor.
On a large hero that can keep a laptop fan running. A cheaper way to get movement is two or three blurred, coloured circles
moved with transform, which the graphics card handles.
When is WebGL worth it?
When you want colour that flows like liquid or silk. A fragment shader works out the colour of every pixel from a noise function and the time. It needs a canvas, about a hundred lines of setup and a short GLSL program. Three rules keep it light:
- Cap
devicePixelRatioat 1.5 or 2. A gradient does not need full retina sharpness. - Pause the loop with
IntersectionObserverwhen the canvas is off screen, and when the tab is hidden. - Put a still CSS gradient behind the canvas for browsers with WebGL turned off, and for reduced motion.
Which finished backgrounds can I copy?
| Element | Method | What it does |
|---|---|---|
| Shader gradient background | WebGL | A slow, flowing noise gradient from one fragment shader, with a CSS fallback. |
| Aurora background | WebGL | Soft northern-lights ribbons that bend toward the pointer. |
| Iridescence sheen | WebGL | A soft, flowing thin-film colour shift. |
| Gradient waves | Canvas | Layered, flowing sine-wave bands filled with gradients. |
| Plasma field | Canvas | A classic plasma colour field drawn at low resolution and scaled up. |
| Silk flow | Canvas | Smooth silky folds of light that drift and can be pulled by dragging. |
All are free in the Enrichr Library, with live controls and copyable code.
Where to go next
- How to get the CSS gradient from a website.
- How to tell if a website uses WebGL.
- Animated SVG background patterns.
FAQ
Can you animate a CSS gradient?
Not directly. Browsers do not blend between two background-image values. You can animate custom properties used inside the gradient if you register them with @property, or animate the position of an oversized gradient.
Why is my animated gradient using so much CPU?
Animating background-position or background-size makes the browser repaint the whole area every frame. Move a layer with transform instead, or use a shader.
How do sites make the flowing mesh gradient effect?
Usually with a WebGL fragment shader that mixes a few colours using animated noise. Some use a low-resolution canvas scaled up with a blur, which looks similar for less code.
Is a WebGL background bad for performance?
A single full-screen shader with simple noise is light for any recent phone. What hurts is running it at full retina resolution and never pausing it. Cap the pixel ratio and stop when hidden.