背景图像本身的 CSS 渐变

CSS gradients of the background image itself

本文关键字:CSS 渐变 图像 背景      更新时间:2023-09-26

我想使用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; 
}

例 1

这是另一个具有匹配背景颜色的小提琴:

例 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>