Why CSS Linear-Gradient Doesn't Show on Scrollable Div

Problem Statement

Sometimes when you try to apply a gradient background to a scrollable div, it seems like nothing happens. For example:


.scroll-region {
    height: 200px;
    overflow: auto;
    background: linear-gradient(135deg, #1e3c72, #2a5298);
}
        

While a solid color works perfectly:


.scroll-region {
    height: 200px;
    overflow: auto;
    background-color: #1e3c72;
}
        

Why This Happens

The key reason is how CSS background works with scrolling content:

Solution

To make gradients work reliably in scrollable divs:


.scroll-region {
    height: 200px;
    overflow: auto;
    background: linear-gradient(135deg, #1e3c72, #2a5298);
    background-repeat: no-repeat;
    background-size: cover;          /* fill entire div */
    background-attachment: local;    /* gradient scrolls with content */
    padding: 1rem;
    color: white;
}
        

Demo

Notice how the gradient now fills the scrollable area and scrolls along with the content:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Integer nec odio. Praesent libero. Sed cursus ante dapibus diam.
Sed nisi. Nulla quis sem at nibh elementum imperdiet.
Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta.
Mauris massa. Vestibulum lacinia arcu eget nulla.
Class aptent taciti sociosqu ad litora torquent per conubia nostra.

Key Tips for Students

This approach ensures your scrollable divs always show the gradient as intended, even with overflowing content.

Happy coding! 🚀