How to create a gradient background color on scroll with CSS?

Creating a gradient background that changes on scroll provides an engaging visual effect. This is achieved by using CSS animations triggered by scroll events, or by using fixed positioning with a gradient that shifts as content moves.

Syntax

body {
    height: 100vh; /* or more for scrollable content */
    background: linear-gradient(angle, color1, color2, color3, ...);
    background-attachment: fixed; /* keeps gradient fixed while content scrolls */
}

Method 1: Fixed Gradient Background

This method creates a gradient that appears to shift as you scroll by using a fixed background attachment −

<!DOCTYPE html>
<html>
<head>
<style>
    body {
        margin: 0;
        height: 300vh;
        color: white;
        font-family: Arial, sans-serif;
        background: linear-gradient(
            45deg,
            #ff6b6b 0%,
            #4ecdc4 25%,
            #45b7d1 50%,
            #96ceb4 75%,
            #feca57 100%
        );
        background-attachment: fixed;
        padding: 20px;
    }
    
    .content {
        background: rgba(0, 0, 0, 0.3);
        padding: 30px;
        border-radius: 10px;
        margin: 50px 0;
    }
</style>
</head>
<body>
    <div class="content">
        <h1>Gradient Background on Scroll</h1>
        <p>Scroll down to see the gradient background effect. The gradient stays fixed while the content moves.</p>
    </div>
    
    <div class="content">
        <h2>More Content</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. The gradient creates a beautiful shifting effect as you scroll through the content.</p>
    </div>
    
    <div class="content">
        <h2>Keep Scrolling</h2>
        <p>Notice how the background gradient appears to move and change as you scroll, creating an engaging visual experience.</p>
    </div>
</body>
</html>
A colorful diagonal gradient background with semi-transparent content boxes. As you scroll, the gradient appears to shift and change colors, creating a dynamic visual effect.

Method 2: Animated Gradient with CSS

This approach uses CSS animations to continuously change the gradient colors −

<!DOCTYPE html>
<html>
<head>
<style>
    body {
        margin: 0;
        height: 200vh;
        color: white;
        font-family: Arial, sans-serif;
        background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
        background-size: 400% 400%;
        animation: gradientShift 8s ease infinite;
        padding: 20px;
    }
    
    @keyframes gradientShift {
        0% {
            background-position: 0% 50%;
        }
        50% {
            background-position: 100% 50%;
        }
        100% {
            background-position: 0% 50%;
        }
    }
    
    .section {
        min-height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        text-align: center;
    }
</style>
</head>
<body>
    <div class="section">
        <div>
            <h1>Animated Gradient Background</h1>
            <p>This gradient continuously animates and changes as you scroll.</p>
        </div>
    </div>
    
    <div class="section">
        <div>
            <h2>Smooth Animation</h2>
            <p>The gradient smoothly transitions between different color positions.</p>
        </div>
    </div>
</body>
</html>
A continuously animated gradient background that smoothly transitions between orange, pink, blue, and green colors. The animation runs independently of scrolling, creating a dynamic, flowing effect.

Key Properties

Property Description
background-attachment: fixed Keeps the gradient fixed while content scrolls
background-size: 400% 400% Makes the gradient larger for animation effect
animation Controls the gradient animation timing and behavior

Conclusion

Gradient backgrounds on scroll can be achieved through fixed positioning or CSS animations. Use background-attachment: fixed for a shifting effect or combine large background-size with animations for continuous color transitions.

Updated on: 2026-03-15T14:57:17+05:30

502 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements