我想使用CSS来创建线性渐变,而不是颜色而是图像。
理想情况下,我希望代码看起来像
background: lineargradient(135deg, Image1, Image2)
与使用颜色的方式类似,而不是使用图像。
我如何做到这一点?
答案 0 :(得分:2)
这样做的方法是使用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 :(得分:1)
您可以使用svg
的{{1}}和mask
在两张图片之间进行转换。
linearGradient
角度<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