我如何使用CSS3渐变为我的背景颜色,然后应用背景图像应用某种轻透明纹理?
当前回答
使用背景-混合模式和rgba混合背景图像和颜色
这是你需要的:
.myblendedbg {
background-image: url("some_image.png");
background-color: rgba(0, 0, 0, 0.85); /* use rgba for fine adjustments */
background-blend-mode: multiply;
}
如果你调整rgba颜色值的alpha值(在这个例子中是。85),你可以控制透明度。
此外,背景-混合模式还有其他值,你可以用它来获得一些真正有创意的结果。
注:background-blend-mode: color;在火狐浏览器上不能运行,而multiply在所有现代浏览器上都可以运行
其他回答
要意识到的一件事是,第一个定义的背景图像在堆栈中最上面。最后定义的图像将位于最底部。这意味着,要在图像后面有一个背景渐变,你需要:
身体{ 背景-图像:url(“http://www.skrenta.com/images/stackoverflow.jpg”),线性渐变(红,黄); Background-image: url("http://www.skrenta.com/images/stackoverflow.jpg"), -webkit-gradient(线性,左上,左下,从(红色),到(黄色)); Background-image: url("http://www.skrenta.com/images/stackoverflow.jpg"), -moz-linear-gradient(顶部,红色,黄色); }
您还可以为图像定义背景位置和背景大小。 我写了一篇关于CSS3渐变可以做的有趣事情的博文
我希望这是跨浏览器的:
(修改了从渐变编辑器的基础,黑色到透明的垂直渐变在图像的顶部)
background-image: url('YOURIMAGE.JPG');
background-image: -moz-linear-gradient(left, rgba(0,0,0,1) 0%, rgba(0,0,0,0) 100%),url('YOURIMAGE.JPG'); /* FF3.6-15 */
background-image: -webkit-linear-gradient(left, rgba(0,0,0,1) 0%,rgba(0,0,0,0) 100%),url('YOURIMAGE.JPG'); /* Chrome10-25,Safari5.1-6 */
background-image: linear-gradient(to right, rgba(0,0,0,1) 0%,rgba(0,0,0,0) 100%),url('YOURIMAGE.JPG'); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#00000000',GradientType=1 ), progid:DXImageTransform.Microsoft.AlphaImageLoader(src='YOURIMAGE.JPG', sizingMethod='crop'); /* IE6-9 */
如果你也想为你的图像设置背景位置,那么你可以使用这个:
background-color: #444; // fallback
background: url('PATH-TO-IMG') center center no-repeat; // fallback
background: url('PATH-TO-IMG') center center no-repeat, -moz-linear-gradient(top, @startColor, @endColor); // FF 3.6+
background: url('PATH-TO-IMG') center center no-repeat, -webkit-gradient(linear, 0 0, 0 100%, from(@startColor), to(@endColor)); // Safari 4+, Chrome 2+
background: url('PATH-TO-IMG') center center no-repeat, -webkit-linear-gradient(top, @startColor, @endColor); // Safari 5.1+, Chrome 10+
background: url('PATH-TO-IMG') center center no-repeat, -o-linear-gradient(top, @startColor, @endColor); // Opera 11.10
background: url('PATH-TO-IMG') center center no-repeat, linear-gradient(to bottom, @startColor, @endColor); // Standard, IE10
或者你也可以创建一个LESS mixin (bootstrap风格):
#gradient {
.vertical-with-image(@startColor: #555, @endColor: #333, @image) {
background-color: mix(@startColor, @endColor, 60%); // fallback
background-image: @image; // fallback
background: @image, -moz-linear-gradient(top, @startColor, @endColor); // FF 3.6+
background: @image, -webkit-gradient(linear, 0 0, 0 100%, from(@startColor), to(@endColor)); // Safari 4+, Chrome 2+
background: @image, -webkit-linear-gradient(top, @startColor, @endColor); // Safari 5.1+, Chrome 10+
background: @image, -o-linear-gradient(top, @startColor, @endColor); // Opera 11.10
background: @image, linear-gradient(to bottom, @startColor, @endColor); // Standard, IE10
}
}
对于我的响应式设计,我的下拉框在框的右侧(垂直手风琴),接受百分比作为位置。最初,下箭头是“位置:绝对;右:13 px;”。在97%的定位下,它的工作原理如下:
> background: #ffffff;
> background-image: url(PATH-TO-arrow_down.png); /*fall back - IE */
> background-position: 97% center; /*fall back - IE */
> background-repeat: no-repeat; /*fall back - IE */
> background-image: url(PATH-TO-arrow_down.png) no-repeat 97% center;
> background: url(PATH-TO-arrow_down.png) no-repeat 97% center, -moz-linear-gradient(top, #ffffff 1%, #eaeaea 100%);
> background: url(PATH-TO-arrow_down.png) no-repeat 97% center, -webkit-gradient(linear, left top, left bottom, color-stop(1%,#ffffff), color-stop(100%,#eaeaea));
> background: url(PATH-TO-arrow_down.png) no-repeat 97% center, -webkit-linear-gradient(top, #ffffff 1%,#eaeaea 100%);
> background: url(PATH-TO-arrow_down.png) no-repeat 97% center, -o-linear-gradient(top, #ffffff 1%,#eaeaea 100%);<br />
> filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#eaeaea',GradientType=0 );
附注:对不起,我不知道如何处理滤镜。
我的解决方案:
background-image: url(IMAGE_URL); /* fallback */
background-image: linear-gradient(to bottom, rgba(0,0,0,0.7) 0%,rgba(0,0,0,0.7) 100%), url(IMAGE_URL);