0

Is there a way to achieve the following design with CSS?

transparent gradient example

It is basically a container with a background-image: url("...") and an inner div that is gradually faded into transparency at the bottom.

I know how to do a fade out with a mono color background using linear-gradient(transparent, white), but not with a background-image like in the example above. The content of the inner div could be anything, not just text.

Toshiro
  • 21
  • 5
  • 1
    It's not possible to apply opacity to **part** of a div/or its contents or a background image, even with a gradient. – Paulie_D Feb 06 '20 at 12:18
  • @Awais I don't want an image with a gradient overlay, which is what your link is suggesting. Instead, I want the inner div to gradually fade into transparency. I updated the image in the original post so hopefully it's clearer what I'm after. – Toshiro Feb 06 '20 at 12:34
  • 1
    @Paulie_D In case you're interested, I found a way to solve my particular problem. See my answer below. – Toshiro Feb 06 '20 at 13:14

2 Answers2

2

Alright, I found the solution: mask-image: -webkit-gradient(...)

Browser support is not optimal though...

Example:

body {
  background: url('https://images.unsplash.com/photo-1531315630201-bb15abeb1653?w=500') center no-repeat;
  background-size: cover;
}

div {
  width: 200px;
  height: 150px;
  overflow: hidden;
  background: cyan;
  -webkit-mask-image: -webkit-gradient(linear, center top, center bottom, 
   color-stop(0.50,  rgba(0,0,0,1)),
   color-stop(1.00,  rgba(0,0,0,0)));
  mask-image: -webkit-gradient(linear, center top, center bottom, 
   color-stop(0.50,  rgba(0,0,0,1)),
   color-stop(1.00,  rgba(0,0,0,0)));
}
<div>
   Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>

More info: https://css-tricks.com/clipping-masking-css/

Toshiro
  • 21
  • 5
0

try this

inner Div CSS Properties

background: linear-gradient(to top, rgba(0, 0, 0, 0), rgba(255, 255, 255, 0.5));
  • This adds a gradient overlay on top of the inner div. Instead, I want the inner div to gradually fade into transparency. I updated the image in the original post so hopefully it's clearer what I'm after. – Toshiro Feb 06 '20 at 12:39