86

I have a full screen fixed background image. I would like the text in my scrolling div to fade out at the top, presumably by applying a gradient mask to the background at only the top part of the div. I'm interested in having the text look like it fades away as it the user scrolls down, but still having a large area of full opacity for actually reading the text.

I know there are masking options in the webkit, but I can't find a way to fade through to the page's background over the contained text only applying the gradient to a small portion of the element.

Here's an image of the desired result:

Flimm
  • 136,138
  • 45
  • 251
  • 267
aviemet
  • 1,463
  • 1
  • 15
  • 19
  • read this and follow totorial: http://webdesignerwall.com/tutorials/css-gradient-text-effect – Luca Filosofi Mar 01 '12 at 22:32
  • 4
    Thanks, I saw that before I posted this, and it would work if I just wanted to fade to white, but I want to fade to the image background of the page. – aviemet Mar 01 '12 at 23:52

5 Answers5

114

I've been wondering this exact same thing. The solution is actually quite simple. Although this is of course quite a modern feature, so you're stuck to browser compatibility.

Webkit can take care of this with a single line of CSS:

-webkit-mask-image: -webkit-gradient(linear, left 90%, left bottom, from(rgba(0,0,0,1)), to(rgba(0,0,0,0)))

(The new standardised way of doing it is would use mask-image and linear-gradient using its new syntax. See caniuse.com for mask-image and linear-gradient.)

This would fade out the bottom 10% of whatever element it's applied to, without using even so much as an image. You could add padding-bottom: 50% to make sure that content is only faded when there is more to scroll to.

Source: http://www.webkit.org/blog/181/css-masks/

A Mozilla (Gecko) fallback is a bit trickier though: you can use its 'mask' feature, but this demands a SVG-image. You could try to base64 embed that image into your stylesheet... Use mask-image in Firefox now.

Flimm
  • 136,138
  • 45
  • 251
  • 267
Adrian van Vliet
  • 1,411
  • 1
  • 12
  • 17
  • 33
    Obligatory - http://www.alistapart.com/articles/every-time-you-call-a-proprietary-feature-css3-a-kitten-dies :P – BoltClock Mar 12 '12 at 16:26
  • 1
    It was great to discover this and works amazingly well on `webkit`. However, I'm having a hard time accomplishing the same effect on Firefox (let alone IE). Any pointers or examples you can give? I'm not an SVG guru, so this would be much appreciated. – keystorm Feb 23 '13 at 19:11
  • webkit you handsome bastard – Max Oct 28 '14 at 23:58
  • 1
    For Firefox you can use the `mask-image` property. It seems the syntax for the attribute value is slightly different. – lex82 Aug 23 '17 at 15:28
  • 2
    It's nearly 2020 and we still don't have simple cross-browser masks. This is ridiculous. – Gavin Oct 08 '19 at 10:18
  • caniuse tables https://caniuse.com/?search=mask-image – sheriffderek Sep 10 '21 at 15:04
  • @BoltClock mask-image is a current W3C proposal though, it just so happens that WebKit rushed to implement it, others didn't but then realized it's a good feature and now everyone uses the same prefix. – rnmp Sep 28 '22 at 22:00
32

Here's how you can do it using modern mask-image and linear-gradient properties. Sadly, in 2021, they are not completely supported in all browsers. (See caniuse.com for CSS masks and linear-gradient.)

In this code snippet, I've given the html element an orange and yellow gradient background, to prove that this method is using real transparency and uses the element underneath it as background.

html {
  background: linear-gradient(to right, orange, yellow, orange);
  height: 100%;
}
div {
  -webkit-mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 1));
  mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 1));
}
<div>
One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten
One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten One two three four five six seven eight nine ten
</div>
Flimm
  • 136,138
  • 45
  • 251
  • 267
  • I wonder if this would work with blur filter as well, that would really rock! Imagine a navbar on top that fades nicely with a gradient, blurring the content behind it gradualy. – Ovi Trif Apr 22 '23 at 19:33
19

If you're fading to a solid color, you can use a psudo element:

.image-container {
    position: relative;
    display: inline-block;
  }

.image-container:after {
  content: "";
  display: block;
  position: absolute;
  width: 100%;
  height: 55%;
  bottom: 0;
  background: -webkit-linear-gradient(transparent, #FFF) left repeat;
  background: linear-gradient(transparent, #FFF) left repeat; 
}
<div class="image-container">
  <img src="https://photosharingsites.files.wordpress.com/2014/11/tiger-wild-cat-animal-winter-snow-wallpapers-free-backgrounds.jpg?w=474&h=344" alt="don't shoot me">
  </div>
Jeff
  • 13,943
  • 11
  • 55
  • 103
ecolema
  • 588
  • 4
  • 15
6

I struggled with this once when i needed to fade both top and bottom sides, and then i've created a simple generator for me, it was working well, so i published it here (css mask generator), it might be useful.

(you need to use -webkit prefix if you're not using some parser that already check caniuse's support)

The generator works with linear gradients and radial gradients when needed, just like at @Flimm's example

Example top and bottom fade:

.container {
    height: 200px;
    width: 100%;
    overflow: auto;
    -webkit-mask-image: linear-gradient(180deg, transparent 5%, rgba(0, 0, 0, 1) 52%, rgba(0, 0, 0, 1) 44%, transparent 95%);
  }
<div class="container">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer luctus velit et neque sodales, vel malesuada libero pharetra. Phasellus rutrum ligula non nisi porttitor commodo. Sed mattis nibh metus, ac tristique augue iaculis vitae. Nam et finibus nulla. Morbi ornare vulputate interdum. Vivamus ultricies libero at sapien posuere, id luctus lacus facilisis. Praesent egestas tristique arcu a facilisis. Donec cursus, purus et convallis facilisis, velit erat vestibulum ligula, at porta nunc nunc tempor massa. Suspendisse lorem mauris, tincidunt in erat nec, ultricies viverra tellus. Praesent ligula ante, maximus ut orci a, pretium convallis diam. Nunc eget magna consectetur nisl ultrices feugiat et a eros. Pellentesque vitae lectus libero. Nulla sed dignissim nulla, rutrum fermentum sapien. Vestibulum aliquet, neque elementum pharetra mollis, velit libero ullamcorper leo, sed mattis elit lectus sed libero.</p>
</div>
1

Assuming I'm understanding what you want correctly, you could duplicate the top 300ish pixels of your image and apply a transparency gradient to them in Photoshop (making the top completely opaque and the bottom completely transparent).

Then place this image in a div or some other element fixed over at the top of the fixed image but with a high z-index. The main text would then scroll over your background but under the div overlay and fade out as the overlay becomes more opaque towards the top of the screen.

Gareth
  • 5,693
  • 3
  • 28
  • 37
  • That would be a good solution except that I place a higher priority on the full page background scaling to the window size. I was more hoping to apply a mask that would affect the element I applied it to and everything contained therein, including the text. – aviemet Mar 01 '12 at 23:53