我如何使用CSS3渐变为我的背景颜色,然后应用背景图像应用某种轻透明纹理?
当前回答
要意识到的一件事是,第一个定义的背景图像在堆栈中最上面。最后定义的图像将位于最底部。这意味着,要在图像后面有一个背景渐变,你需要:
身体{ 背景-图像: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(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);
我有一个实现,我需要把这个技术更进一步,并想概述我的工作。下面的代码做同样的事情,但使用SASS、Bourbon和一个图像精灵。
@mixin sprite($position){
@include background(url('image.png') no-repeat ($position), linear-gradient(#color1, #color2));
}
a.button-1{
@include sprite(0 0);
}
a.button-2{
@include sprite (0 -20px);
}
a.button-2{
@include sprite (0 -40px);
}
SASS和Bourbon负责跨浏览器代码,现在我所要声明的就是每个按钮的精灵位置。很容易将此原则扩展到按钮的活动状态和悬停状态。
作为一个可靠的方法,你可以只做一个背景图像是500x5像素,在你的css使用:
background-img:url(bg.jpg) fixed repeat-x;
background:#<xxxxxx>;
其中xxxxxx对应与最终渐变颜色相匹配的颜色。
你也可以把它固定在屏幕的底部,让它与初始渐变颜色相匹配。
对于我的响应式设计,我的下拉框在框的右侧(垂直手风琴),接受百分比作为位置。最初,下箭头是“位置:绝对;右: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 );
附注:对不起,我不知道如何处理滤镜。
我总是使用以下代码使其工作。这里有一些注意事项:
如果你把图片URL放在渐变前面,这张图片将会显示在渐变上面。
.background-gradient { 背景:url('http://trungk18.github.io/img/trungk18.png') no-repeat, -moz-linear-gradient(135deg, #6ec575 0, #3b8686 100%); 背景:url('http://trungk18.github.io/img/trungk18.png') no-repeat, -webkit-gradient(135deg, #6ec575 0, #3b8686 100%); 背景:url('http://trungk18.github.io/img/trungk18.png') no-repeat, -webkit-linear-gradient(135deg, #6ec575 0, #3b8686 100%); 背景:url('http://trungk18.github.io/img/trungk18.png') no-repeat, -o-linear-gradient(135deg, #6ec575 0, #3b8686 100%); 背景:url('http://trungk18.github.io/img/trungk18.png') no-repeat, -ms-linear-gradient(135deg, #6ec575 0, #3b8686 100%); 背景:url('http://trungk18.github.io/img/trungk18.png')无重复,线性梯度(135deg, #6ec575 0, #3b8686 100%); 身高:500 px; 宽度:500 px; } < div class = " background-gradient " > < / div >
如果你把渐变放在图片URL之前,这张图片会显示在渐变下面。
.background-gradient { 背景:-moz-linear-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat; Background: -webkit-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat; 背景:-webkit-linear-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat; 背景:-o-linear-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat; 背景:-ms-linear-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat; 背景:线性梯度(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat; 宽度:500 px; 身高:500 px; } < div class = " background-gradient " > < / div >
这种技术就像我们在这里描述的有多个背景图像一样