Animated Moving + Random Gradient Background

Last updated 3 weeks, 3 days ago | 33 views 75     5

Tags:- HTML JQuery CSS

Fifth page link: Moving Gradient Background with Pure CSS

Awesome! You're about to build a hybrid effect: a background that both shifts position and uses randomly changing gradients over time.

This effect combines the CSS-powered movement and JavaScript-powered randomness, creating a lively and dynamic animated background.


Animated Moving + Random Gradient Background

This version:

  • Animates the gradient position (moving background)

  • Randomizes the gradient colors every few seconds


Complete Working Example (HTML + CSS + JS)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Random Moving Gradient</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
      background: #111;
      font-family: Arial, sans-serif;
    }

    .gradient-box {
      width: 90vw;
      height: 90vh;
      border-radius: 20px;
      color: white;
      font-size: 28px;
      font-weight: bold;
      display: flex;
      justify-content: center;
      align-items: center;
      background-size: 400% 400%;
      animation: moveGradient 12s ease infinite;
      transition: background-image 2s ease;
    }

    @keyframes moveGradient {
      0% {
        background-position: 0% 50%;
      }
      50% {
        background-position: 100% 50%;
      }
      100% {
        background-position: 0% 50%;
      }
    }
  </style>
</head>
<body>

  <div class="gradient-box">Random + Moving Gradient</div>

  <script>
    function getRandomColor() {
      const letters = "0123456789ABCDEF";
      let color = "#";
      for (let i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)];
      }
      return color;
    }

    function generateGradient() {
      const angle = Math.floor(Math.random() * 360);
      const color1 = getRandomColor();
      const color2 = getRandomColor();
      const color3 = getRandomColor();
      return `linear-gradient(${angle}deg, ${color1}, ${color2}, ${color3})`;
    }

    function updateGradient() {
      const box = document.querySelector('.gradient-box');
      box.style.backgroundImage = generateGradient();
    }

    // Initial load
    updateGradient();

    // Update every 5 seconds
    setInterval(updateGradient, 5000);
  </script>

</body>
</html>

✅ How It Works

  • CSS animation (@keyframes moveGradient) moves the gradient.

  • JS updates the background-image with new random colors every 5 seconds.

  • transition ensures the gradient swap is smooth.


Tips

  • Use developer tools to tweak colors or timing live!

  • Replace getRandomColor() with a predefined palette for more aesthetic control.

  • Works great on both desktop and mobile — super lightweight!


⚠️ Common Pitfalls

  • Text readability: If colors get too light, add text-shadow.

  • Performance: Too frequent updates (like every 1s) may cause CPU/GPU load on low-end devices.

  • Color clashes: Try limiting the color space for better visuals.


Would you like to expand this into a full-page animated website background or make it work on scroll?

Click here