CSS animated backgrounds: 6 effects and what each costs
Six animated backgrounds in pure CSS (sliding gradient, drifting blobs, conic glow, hue cycle, stripes, grain), with a Chrome trace of which ones repaint.
Updated
An animated background in CSS is a layer behind your content plus a @keyframes animation that moves it. What you animate decides what it costs. Move the layer with transform, rotate, opacity or filter and Chrome animates it on the compositor, without repainting it. Animate background-position or a gradient's colours and angle, and it repaints the whole background on every frame.
We traced all six effects below in headless Chrome 154 at 1280×720:
| Effect | Animates | Paints in 3 s |
|---|---|---|
| Sliding gradient, the usual recipe | background-position | about 360 |
| Sliding gradient, wide layer | transform | 0 |
| Drifting blurred blobs | transform | 0 |
| Rotating conic glow | rotate | 0 |
Rotating conic, @property angle | a registered <angle> | about 360 |
| Colour cycle | filter: hue-rotate() | 0 |
Moving stripes, background-position | background-position | about 360 |
| Moving stripes, wide layer | transform | 0 |
| Film grain overlay | transform with steps() | 0 |
Every effect in the demo is plain CSS, no JavaScript:
/**
* Four pure-CSS animated backgrounds from the "CSS animated backgrounds" blog
* post. Every effect moves with transform, rotate or filter, so Chrome animates
* it on the compositor without repainting the gradient.
*/
const GRAIN =
"url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.8' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E\")";
const tiles = [
{ id: "blobs", label: "Drifting blobs" },
{ id: "conic", label: "Rotating conic glow" },
{ id: "stripes", label: "Moving stripes" },
{ id: "grain", label: "Gradient with grain" },
] as const;
export function CssAnimatedBackgroundsDemo() {
return (
<div className="grid w-full grid-cols-1 gap-3 p-3 sm:grid-cols-2">
<style>{`
.cab-tile { position: relative; isolation: isolate; overflow: hidden; height: 180px; border-radius: 12px; background: #0b0b12; }
.cab-blob { position: absolute; width: 70%; aspect-ratio: 1; border-radius: 50%; filter: blur(40px); opacity: 0.85; animation: cab-drift 18s ease-in-out infinite alternate; }
.cab-b1 { background: #7c5cff; left: -15%; top: -35%; }
.cab-b2 { background: #38d6f5; right: -20%; top: 5%; animation-duration: 22s; }
.cab-b3 { background: #ff5ca8; left: 20%; bottom: -60%; animation-duration: 26s; }
@keyframes cab-drift { to { transform: translate(15%, 10%) scale(1.15); } }
.cab-conic::before { content: ""; position: absolute; left: 50%; top: 50%; width: 150%; aspect-ratio: 1; translate: -50% -50%; z-index: -1; background: conic-gradient(#7c5cff, #38d6f5, #ff5ca8, #7c5cff); filter: blur(30px); animation: cab-spin 20s linear infinite; }
@keyframes cab-spin { to { rotate: 1turn; } }
.cab-stripes::before { content: ""; position: absolute; inset: 0 -56.57px; z-index: -1; background: repeating-linear-gradient(45deg, #1e1b4b 0 20px, #312e81 20px 40px); animation: cab-stripe 2s linear infinite; }
@keyframes cab-stripe { to { transform: translateX(56.57px); } }
.cab-grain { background: linear-gradient(120deg, #7c5cff, #38d6f5, #ff5ca8); }
.cab-grain::after { content: ""; position: absolute; inset: -100px; z-index: -1; pointer-events: none; background-image: ${GRAIN}; opacity: 0.18; mix-blend-mode: overlay; animation: cab-grain 1s steps(6) infinite; }
@keyframes cab-grain {
20% { transform: translate(-40px, 30px); }
40% { transform: translate(30px, -50px); }
60% { transform: translate(-60px, -20px); }
80% { transform: translate(50px, 40px); }
}
@media (prefers-reduced-motion: reduce) {
.cab-blob, .cab-conic::before, .cab-stripes::before, .cab-grain::after { animation: none; }
}
`}</style>
{tiles.map((tile) => (
<div
key={tile.id}
className={`cab-tile cab-${tile.id} flex items-end p-4`}
>
{tile.id === "blobs" ? (
<>
<div aria-hidden className="cab-blob cab-b1 -z-10" />
<div aria-hidden className="cab-blob cab-b2 -z-10" />
<div aria-hidden className="cab-blob cab-b3 -z-10" />
</>
) : null}
<span className="relative text-sm font-medium text-white [text-shadow:0_1px_12px_rgb(0_0_0/0.5)]">
{tile.label}
</span>
</div>
))}
</div>
);
}
The setup every effect shares
Put the moving layer in a pseudo-element or an aria-hidden child behind the content, and keep it inside the section:
.hero {
position: relative;
isolation: isolate; /* the layer's z-index: -1 stays inside .hero */
overflow: hidden; /* clips layers bigger than the section */
}
.hero::before {
content: "";
position: absolute;
inset: 0;
z-index: -1;
}isolation: isolate makes .hero a stacking context, so z-index: -1 puts the layer behind the text but still above the page background. Without it, the layer can slide behind the parent's own background and disappear.
Whichever effects you use, end your CSS with a reduced-motion block. It has to come after the effect rules: at the same specificity, the later animation declaration wins.
/* last in the file, after the effects */
@media (prefers-reduced-motion: reduce) {
.hero,
.hero::before,
.hero::after,
.blob {
animation: none;
}
}That stops every effect below for visitors who asked their system for less motion; each one still shows its first frame.
1. Sliding gradient
The recipe most tutorials give makes the background twice as wide and animates background-position:
.hero {
background-image: linear-gradient(90deg, #7c5cff, #38d6f5, #ff5ca8, #7c5cff);
background-size: 200% 100%;
animation: slide 12s linear infinite;
}
@keyframes slide {
to {
background-position: 200% 0;
}
}It loops cleanly because the gradient starts and ends on the same colour and the background moves by exactly one tile. It also repaints the gradient 60 times a second: about 360 Paint events and 180 commits in 3 seconds, 18 to 20 ms of painting in total in our trace. That's fine on a button. On a full-screen hero on a slow phone it competes with your JavaScript for the main thread.
The cheaper version paints the gradient once on a layer twice as wide, and slides the layer with transform:
.hero::before {
width: 200%;
inset: 0 auto 0 0;
background-image: linear-gradient(90deg, #7c5cff, #38d6f5, #ff5ca8, #7c5cff);
background-size: 50% 100%;
animation: slide 12s linear infinite;
}
@keyframes slide {
to {
transform: translateX(-50%);
}
}Once it was running, the trace showed no paints for it at all. CSS gradient vs WebGL gradient has the full trace for this one.
2. Drifting blurred blobs
Three circles of flat colour, blurred until they melt into each other, each drifting on its own timer:
<section class="hero">
<div class="blob b1" aria-hidden="true"></div>
<div class="blob b2" aria-hidden="true"></div>
<div class="blob b3" aria-hidden="true"></div>
<h1>Your headline</h1>
</section>.blob {
position: absolute;
z-index: -1;
width: 60%;
aspect-ratio: 1;
border-radius: 50%;
filter: blur(80px);
opacity: 0.8;
animation: drift 18s ease-in-out infinite alternate;
}
.b1 {
background: #7c5cff;
left: -10%;
top: -20%;
}
.b2 {
background: #38d6f5;
right: -15%;
top: 10%;
animation-duration: 22s;
}
.b3 {
background: #ff5ca8;
left: 25%;
bottom: -40%;
animation-duration: 26s;
}
@keyframes drift {
to {
transform: translate(15%, 10%) scale(1.15);
}
}Different durations (18, 22 and 26 seconds) keep the blobs from moving in step, so the pattern takes a long time to repeat. alternate plays each animation forwards then backwards, so there's no jump at the end.
Chrome recorded no paints for this either. That doesn't make it free: the blur is applied by the compositor, and our headless trace doesn't measure GPU time. If a big blur makes scrolling stutter on a phone, swap the blur for a soft-edged radial gradient on each blob. background: radial-gradient(closest-side, #7c5cffcc, transparent) on a larger circle gives a similar glow with no filter, and it also traced at zero paints.
3. Rotating conic glow
A conic gradient, blurred and spun slowly, gives light that sweeps round the section:
.hero::before {
left: 50%;
top: 50%;
width: 150%;
aspect-ratio: 1;
translate: -50% -50%;
background: conic-gradient(#7c5cff, #38d6f5, #ff5ca8, #7c5cff);
filter: blur(60px);
animation: spin 20s linear infinite;
}
@keyframes spin {
to {
rotate: 1turn;
}
}The layer is a square 150% of the section's width, centred with translate. Its sides stay outside the section at every angle of the turn as long as the section is no taller than it is wide (the square's inscribed circle, 0.75 of the width in radius, then reaches past the section's corners). For a tall mobile section, size the square from the height instead. The first colour repeats at the end, so there's no seam at 12 o'clock.
The other way to spin a conic gradient is to register its start angle with @property and animate that:
@property --angle {
syntax: "<angle>";
inherits: false;
initial-value: 0deg;
}
.hero {
--angle: 0deg; /* a start value for browsers without @property */
background: conic-gradient(
from var(--angle),
#7c5cff,
#38d6f5,
#ff5ca8,
#7c5cff
);
animation: turn 20s linear infinite;
}
@keyframes turn {
to {
--angle: 360deg;
}
}That one repaints every frame (about 360 paints in 3 s). It's still the right tool when the gradient can't rotate as a whole, like the ring in a gradient border, where the box has to stay put while the colours move. @property works in Chrome 85, Firefox 128 and Safari 16.4 (MDN browser compat data); older browsers show the gradient standing still. They animate an unregistered --angle in one jump halfway through the cycle, so set a start value on the element, as above, or the gradient has no angle for half the loop and disappears.
4. Colour cycle with hue-rotate
To shift every colour round the colour wheel without moving anything, animate a filter:
.hero::before {
background: linear-gradient(120deg, #7c5cff, #38d6f5, #ff5ca8);
animation: hue 10s linear infinite;
}
@keyframes hue {
to {
filter: hue-rotate(360deg);
}
}hue-rotate(360deg) lands back on the starting colours, so the loop is seamless. It traced at zero paints. The catch is the colour: hue-rotate() is a fixed matrix in RGB, not a true hue turn, so lightness and saturation drift on the way round. In Chrome 154, #7c5cff becomes #ff4561 at 90° and an olive #618100 at 180°. White text on that violet has a 4.35:1 contrast ratio; at the 90° frame it drops to 3.35:1. Look at the whole cycle before you ship it, and check text contrast at the worst frame.
5. Moving stripes
Barber-pole stripes come from repeating-linear-gradient. The trick is the distance: 45° stripes with a 40px period repeat every 40 ÷ sin 45° ≈ 56.57px along the horizontal, so moving the layer by exactly that much loops with no jump.
.hero::before {
inset: 0 -56.57px; /* wider than the section by one repeat each side */
background: repeating-linear-gradient(
45deg,
#1e1b4b 0 20px,
#312e81 20px 40px
);
animation: stripes 2s linear infinite;
}
@keyframes stripes {
to {
transform: translateX(56.57px);
}
}Moving background-position by the same 56.57px works too, but it traced at about 360 paints in 3 seconds against zero for the transform version. Stripes are busy, so keep them to a progress bar, a loading state or a thin banner rather than a whole page.
6. Film grain overlay
Grain makes flat gradients look printed and hides banding. Generate the noise with an SVG feTurbulence filter in a data URI, lay it over the gradient, and jump it to a new position a few times a second:
.hero {
background: linear-gradient(120deg, #7c5cff, #38d6f5, #ff5ca8);
}
.hero::after {
content: "";
position: absolute;
inset: -100px; /* bigger than the biggest jump below */
z-index: -1; /* above the .hero background, below the text */
pointer-events: none;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.8' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
opacity: 0.18;
mix-blend-mode: overlay;
animation: grain 1s steps(6) infinite;
}
@keyframes grain {
20% {
transform: translate(-40px, 30px);
}
40% {
transform: translate(30px, -50px);
}
60% {
transform: translate(-60px, -20px);
}
80% {
transform: translate(50px, 40px);
}
}steps(6) makes the noise jump instead of glide, which reads as grain rather than a drifting texture. The overlay has to be bigger than the section by more than the largest jump (60px here), or its edge slides into view. This traced at zero paints too, because the noise tile is drawn once and only the layer moves. Film grain on a gradient covers how the SVG noise works, per-pixel grain in a shader and what each costs.
What the trace does and doesn't show
The numbers above come from a 3-second Chrome performance trace of each effect on a 1280×720 box, run twice, counting Paint and Commit events once the animation was running. Every zero-paint effect was confirmed to be moving: its animations were in the running state and two screenshots 0.7 seconds apart differed.
The trace measures main-thread work. It doesn't measure GPU time, and headless Chrome had no GPU. So read "0 paints" as "doesn't block on your JavaScript", not "free". Large blurred layers and blend modes still cost the GPU something on every frame, and several animated layers on one page add up. Test on the slowest phone you care about.
Three rules cover most of it:
- Animate
transform,rotate,scale,opacityandfilter. Avoid animatingbackground-position,background-sizeand gradient colours or angles on anything large. - Stop the animation for
prefers-reduced-motion: reduce. - Keep the layer count low. One or two moving layers behind a hero is plenty.
When to use a live gradient instead
CSS can slide, spin, drift and tint a gradient. It can't make colours fold into each other the way the Stripe homepage does; that takes a shader. CSS can hide banding with a grain overlay like the one in section 6, while a shader can add grain per pixel as part of the gradient itself. The free gradients.sh components draw one on the GPU (WebGPU, or WebGL2 where WebGPU isn't available), loop seamlessly, stop drawing off screen and in hidden tabs, and show one still frame to visitors who prefer reduced motion.
Mesh Gradient is the closest match to the drifting blobs above. Install it with the shadcn CLI:
pnpm dlx shadcn@latest add https://gradients.sh/r/mesh-gradient.jsonIt adds about 50 KB of gzipped JavaScript up front, plus about 8 KB that loads later in browsers that draw with WebGPU, shared by every gradients.sh component on the page. Linear Flow is the live version of the sliding gradient, and Aurora the live version of a northern-lights glow. To pick colours and motion by eye, tune a gradient in the editor and install what you see.
Which one to use
| You want | Use |
|---|---|
| A gradient that slowly moves | Sliding gradient with transform |
| Soft colour that drifts | Blurred blobs, or radial-gradient blobs |
| Light sweeping round a section | Rotating conic glow |
| The same layout, shifting colours | hue-rotate, checked for contrast |
| A loading or progress state | Moving stripes |
| Texture on a flat gradient | Grain overlay |
| Colour that flows and folds | A shader component like Mesh Gradient |
Related
- Animated gradient backgrounds in Tailwind CSS builds the sliding gradient and the blobs with Tailwind utilities.
- CSS gradient vs WebGL gradient goes further into when CSS runs out and what WebGL costs.
- Gradient background for a website covers full-page gradients that don't repeat in stripes, readable text along the whole gradient and frosted cards.
- Background video vs animated gradient compares these with a looping hero video.
- Export an animated gradient as a seamless MP4 loop turns a moving gradient into a video file for slides, video edits or a muted
<video>hero. - How seamless gradient loops work explains why some loops jump and how to make one that doesn't.