Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - Apply a gradient that remains fixed as the div scrolls

Tags:

html

css

I'm fairly new to web design, and I've been struggling with this effect for several hours now. I'd like to have a scrolling text box wherein there is a gradient over the text and that gradient stays fixed as you scroll up and down. The code I have now produces a scrolling div with a gradient, but that gradient is produced over the whole height of the text included in the div. I want the scope of the gradient to be limited to the div itself, if that makes sense.

A solution that seems to be almost there is to give the gradient div fixed position. However, when your mouse is over this fixed div, you cannot scroll through the (parent) div that's underneath it.

My apologies if this has been answered elsewhere. I hunted around but could have missed or misread something.

Anyhoo here is a jfiddle showing what is currently going on: http://jsfiddle.net/sP2e5/

Here is the actual html:

<div class="transcript" id="prisonerResponse">
        <p>i'm actually including an obnoxious amount of text 
           so you can see the scrolling effect: so much textso much 
            textso much 
            textso much textso much textso much 
            textso much textso much textso much textso 
            much textso much textso much textso much textso 
            much textso much textso much textso much 
            textso much textso much textso much 
            textso much textso much textso much t
            extso much textso much textso much t
            extso much textso much textso much 
            textso much textso much textso much 
            textso much textso much textso much text</p>
            <div class="transcriptGradient"> </div>

</div>

and here is the css:

 .transcript{
    overflow:scroll; 
    width:350px; 
    height:100px;
    display:inline-block;
    margin-left:20px;
    position:relative;
    }

.transcriptGradient{
    width:100%; 
    height:100%;
    position:absolute;
    top:0;
    background: -webkit-linear-gradient( rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100% );
    background: linear-gradient(to bottom, rgba(255, 255, 255, 1) 0%, rgba(255,255,255,0) 100%); 
}
like image 381
Magenta Nova Avatar asked Nov 16 '25 03:11

Magenta Nova


2 Answers

With :before selector in div.transcript, you don't need extra div to achieve this result. All you need is position: fixed; applied to .transcript:before.

The downside of these methods (put gradient above scrollable content) is in Firefox, you cannot scroll the content if your cursor is on the gradient. So put your attention on gradient height.

HTML

<div class="transcript" id="prisonerResponse">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. [...] </p>
</div>

CSS

.transcript {
    overflow: scroll;
    width: 350px;
    height: 200px;
    display: inline-block;
    margin: 10px;
    position: relative;
}
.transcript:before {
    content:"";
    width: 350px;
    height: 80px;
    position: fixed;
    background: -webkit-linear-gradient(rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%);
    background: linear-gradient(to bottom, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%);
}

Hope it helps :)

like image 111
Ian Mustafa Avatar answered Nov 17 '25 20:11

Ian Mustafa


If you don't care too much what the code looks like... here's a "ok" way to solve your issue, change the .transcriptGradient placement:

Jsfiddle example

like image 40
Davis Avatar answered Nov 17 '25 20:11

Davis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!