2

Say I have these two images:

How do I combine them like this, where one fades gradually into the other, using just CSS?

I need to do this using CSS because the images are user-supplied.

I've tried using CSS gradients, multiple background images, and blending modes, but wasn't able to achieve what I wanted. Also, wasn't able to find what I needed by googling.

Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80

3 Answers3

3

Here's a solution using the mask-image property.

Scale the browser window to see the images cross fade.

#Wrap {
  position: relative;
  height: 300px;
  width: 100%;
}

#Left,
#Right {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
}

#Left {
  left: 0;
  background: url('https://i.stack.imgur.com/kqa1P.jpg') top left no-repeat;
  -webkit-mask-image: -webkit-linear-gradient(right, rgba(0, 0, 0, 0), rgba(0, 0, 0, 1));
  background-size: contain;
}

#Right {
  right: 0;
  background: url('https://i.stack.imgur.com/UvGLY.jpg') top right no-repeat;
  -webkit-mask-image: -webkit-linear-gradient(right, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0));
  background-size: contain;
}
<div id="Wrap">
  <div id="Left"></div>
  <div id="Right"></div>
</div>
DreamTeK
  • 32,537
  • 27
  • 112
  • 171
  • Upvoted since this does what I need. Unfortunately, [it isn't supported by IE or Edge](http://caniuse.com/#search=mask)... I'll accept your answer tomorrow if no one can find a way to do it with better browser compatibility. – Nathan Arthur Oct 18 '17 at 15:02
  • @NathanArthur Thanks, is anything supported in IE. It's been holding us developers back for decades, time it went in the bin lol. – DreamTeK Oct 18 '17 at 15:10
0

i have made you an example with backgrounds. position relative + both gradients bgs. maybe this works with your pictures

<body>
 <div style="position:relative">
  <div id="grad1">Image1asBG</div>
  <div id="grad2">Image2asBG</div>
 </div>
</body>

css:

    #grad1 {
    position: absolute;
    height: 400px;
    width:100%;
    background: -webkit-linear-gradient(left, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Safari 5.1 to 6.0 */
    background: -o-linear-gradient(right, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Opera 11.1 to 12.0 */
    background: -moz-linear-gradient(right, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Firefox 3.6 to 15 */
    background: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1)); /* Standard syntax (must be last) */
}

#grad2 {
    position: absolute;
    left: 0;
    top: 0;
    height: 200px;
    width:100%;
    background: -webkit-linear-gradient(left, rgba(255,255,0,1), rgba(255,255,0,0)); /* For Safari 5.1 to 6.0 */
    background: -o-linear-gradient(right, rgba(255,255,0,1), rgba(255,255,0,0)); /* For Opera 11.1 to 12.0 */
    background: -moz-linear-gradient(right, rgba(255,255,0,1), rgba(255,255,0,0)); /* For Firefox 3.6 to 15 */
    background: linear-gradient(to right, rgba(255,2550,0,1), rgba(255,255,0,0)); /* Standard syntax (must be last) */
}

fiddle: https://jsfiddle.net/d8v23w60/1/

Gabbax0r
  • 1,696
  • 3
  • 18
  • 31
0

I used opacity. Here is a working copy. I hope it will help you.

Try like this:

#img1 {
position:relative;
}
#img2 {
position:absolute;
left:5px;
opacity:0.5;
}
<img src="https://i.stack.imgur.com/UvGLY.jpg" width="400" id="img1">
<img src="https://i.stack.imgur.com/kqa1P.jpg" width="200" id="img2">
All about JS
  • 562
  • 2
  • 10