CSS 背景图像本身的渐变

CSS gradients of the background image itself

我想使用 CSS 创建线性渐变,不是颜色而是图像。

理想情况下,我希望代码看起来类似于

background: lineargradient(135deg, Image1, Image2)

类似于使用颜色但使用图像完成的方式。

我该如何完成?

方法是用 css 渐变在图像上添加叠加层,使图像看起来逐渐淡入背景:

HTML

<div class="wrapper">
   <div class="overlay">
   </div>
   <img src="http://www.placecage.com/500/200"/> 
</div>

CSS

.wrapper{
    width: 500px;
    height: 200px;
    overflow: hidden;
    position: relative;

}

.overlay{
    position: absolute;
    top: 0;
    left: 0;
    background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzAwMDAwMCIgc3RvcC1vcGFjaXR5PSIwIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiMwMDAwMDAiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
    background: -moz-linear-gradient(top,  rgba(0,0,0,0) 0%, rgba(0,0,0,1) 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0)), color-stop(100%,rgba(0,0,0,1)));
    background: -webkit-linear-gradient(top,  rgba(0,0,0,0) 0%,rgba(0,0,0,1) 100%);
    background: -o-linear-gradient(top,  rgba(0,0,0,0) 0%,rgba(0,0,0,1) 100%);
    background: -ms-linear-gradient(top,  rgba(0,0,0,0) 0%,rgba(0,0,0,1) 100%);
    background: linear-gradient(to bottom,  rgba(0,0,0,0) 0%,rgba(0,0,0,1) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00000000', endColorstr='#000000',GradientType=0 );
    width: 100%;
    height: 100%;
    padding: 20px;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box; 
}

EXAMPLE 1

这是另一个具有匹配背景颜色的 fiddle:

EXAMPLE 2

您可以使用 svgmasklinearGradient 在两个图像之间转换。

<svg id="img" width="600" height="300" viewBox="0 0 600 300">
  <defs>
    <linearGradient id="r" gradientUnits="userSpaceOnUse">
      <stop offset="0%" stop-color="white" />
      <stop offset="70%" stop-color="none" />
      <stop offset="100%" stop-color="none" />
    </linearGradient>
    <mask id="m" maskUnits="userSpaceOnUse" x="0" y="0" width="600" height="300">
      <path d="M0,0 h600 v300 h-600z" fill="url(#r)" />
    </mask>
  </defs>
  <image width="600" height="300" xlink:href="http://www.lorempixel.com/600/300/sports/6" />
  <image mask="url(#m)" width="600" height="300" xlink:href="http://www.lorempixel.com/600/300/sports/2" />
</svg>


角度 135deg

<svg id="img" width="600" height="300" viewBox="0 0 600 300">
  <defs>
    <linearGradient id="r" gradientTransform="translate(600, 0) rotate(135)" gradientUnits="userSpaceOnUse">
      <stop offset="0%" stop-color="white" />
      <stop offset="30%" stop-color="white" />
      <stop offset="100%" stop-color="none" />
    </linearGradient>
    <mask id="m" maskUnits="userSpaceOnUse" x="0" y="0" width="600" height="300">
      <path d="M0,0 h600 v300 h-600z" fill="url(#r)" />
    </mask>
  </defs>
  <image width="600" height="300" xlink:href="http://www.lorempixel.com/600/300/sports/6" />
  <image mask="url(#m)" width="600" height="300" xlink:href="http://www.lorempixel.com/600/300/sports/2" />
</svg>