Chris Chris - 6 months ago 20
CSS Question

CSS faded section at top of scrolling div

I want to add a faded section to the top of my DIV such that, when a user scrolls, the content gradually fades out. I have set up some CSS that achieves this but has one issue.. The faded section scrolls with the content rather than staying fixed in place.

How can I fix this? Do I need help from jQuery or is it possible using CSS and will this work in anything bar CSS3 compatible browsers? (I know I dont have correct vendor prefixes on my

s yet)

Here is my code so far and a fiddle:

.fadedScroller {
overflow: auto;
position: relative;
height: 100px;

.fadedScroller:after {
content: '';
top: 0;
left: 0;
height: 20px;
right: 0;
background: linear-gradient(to bottom, rgba(251,251,251,1) 0%,rgba(251,251,251,0) 100%);
position: absolute;


I have updated my fiddle to point out that using
position: fixed
doesnt actually work as the faded section then appears above the containing div not fixed to the top.


Creating a second div to hold the gradient and shifting it over the the content div works. I know this is kindof dirty and not very intuitive to write, but it works.


<div class="fadedScroller">
<div class="fadedScroller_fade"></div>


.fadedScroller {
    overflow: auto;
    position: relative;
    height: 100px;

.fadedScroller_fade {
    margin-top: -100px;
    height: 40px;
    background: linear-gradient(to bottom, rgba(251, 251, 251, 1) 0%, rgba(251, 251, 251, 0) 100%);
    position: relative;