CSS Animated Gradient Background

An animated gradient background in pure CSS: four colors that slowly drift across the element, no JavaScript. Pick each color and set the speed live.

#cc3f47
#aa2068
#880088
#223b8c
12s
.bg-animated-gradient {
  --c1: #cc3f47;
  --c2: #aa2068;
  --c3: #880088;
  --bg: #223b8c;
  --speed: 12s;
  background-color: var(--bg);
  background-image: linear-gradient(-45deg, var(--c1), var(--c2), var(--c3), var(--bg));
  background-size: 400% 400%;
  animation: bg-animated-gradient var(--speed) ease infinite;
}
@keyframes bg-animated-gradient {
  0%, 100% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
}
@media (prefers-reduced-motion: reduce) {
  .bg-animated-gradient { animation: none; }
}
<div class="
  [--c1:#cc3f47]
  [--c2:#aa2068]
  [--c3:#880088]
  [--bg:#223b8c]
  [--speed:12s]
  [background-color:var(--bg)]
  [background-image:linear-gradient(-45deg,_var(--c1),_var(--c2),_var(--c3),_var(--bg))]
  [background-size:400%_400%]
  [animation:bg-animated-gradient_var(--speed)_ease_infinite]
"></div>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Animated Gradient — CSS background</title>
  <style>
    html, body { height: 100%; margin: 0; }
    .bg-animated-gradient {
      --c1: #cc3f47;
      --c2: #aa2068;
      --c3: #880088;
      --bg: #223b8c;
      --speed: 12s;
      background-color: var(--bg);
      background-image: linear-gradient(-45deg, var(--c1), var(--c2), var(--c3), var(--bg));
      background-size: 400% 400%;
      animation: bg-animated-gradient var(--speed) ease infinite;
    }
    @keyframes bg-animated-gradient {
      0%, 100% { background-position: 0% 50%; }
      50% { background-position: 100% 50%; }
    }
    @media (prefers-reduced-motion: reduce) {
      .bg-animated-gradient { animation: none; }
    }
  </style>
</head>
<body class="bg-animated-gradient"></body>
</html>

How this animated gradient works

This animated gradient background moves without JavaScript. The trick is that the gradient is much bigger than the element: background-size: 400% 400% makes it four times as wide and tall, so only a slice of it shows at a time.

The @keyframes rule then slides that oversized gradient back and forth by animating background-position from 0% 50% to 100% 50% and back. Because the gradient runs at -45deg, the colors appear to drift diagonally across the element.

The four stops run red, pink, purple and navy, neighbors on the brand color wheel, so every in-between moment looks clean. --speed sets how long one full cycle takes. Slower is calmer, and around 10 to 15 seconds works well behind text.

The @media (prefers-reduced-motion: reduce) rule stops the animation for people who ask their system for less motion. Keep it when you paste the code.

Use it for landing page heroes, login screens, music and event sites, and “coming soon” pages.

All CSS background gradients →