我如何使用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渐变可以做的有趣事情的博文

其他回答

下面是我创建的一个MIXIN,用来处理人们可能喜欢使用的所有东西:

.background-gradient-and-image (@fallback, @imgUrl, @background-position-x, @background-position-y, @startColor, @endColor) {
    background: @fallback;
    background: url(@imgUrl) @background-position-x @background-position-y no-repeat; /* fallback */
    background: url(@imgUrl) @background-position-x @background-position-y no-repeat, -webkit-gradient(linear, left top, left bottom, from(@startColor) @background-position-x @background-position-y no-repeat, to(@endColor)); /* Saf4+, Chrome */
    background: url(@imgUrl) @background-position-x @background-position-y no-repeat, -webkit-linear-gradient(top, @startColor, @endColor); /* Chrome 10+, Saf5.1+ */
    background: url(@imgUrl) @background-position-x @background-position-y no-repeat,    -moz-linear-gradient(top, @startColor, @endColor); /* FF3.6+ */
    background: url(@imgUrl) @background-position-x @background-position-y no-repeat,     -ms-linear-gradient(top, @startColor, @endColor); /* IE10 */
    background: url(@imgUrl) @background-position-x @background-position-y no-repeat,      -o-linear-gradient(top, @startColor, @endColor); /* Opera 11.10+ */
    background: url(@imgUrl) @background-position-x @background-position-y no-repeat,         linear-gradient(top, @startColor, @endColor); /* W3C */
}

可以这样使用:

.background-gradient-and-image (#f3f3f3, "../images/backgrounds/community-background.jpg", left, top, #fafcfd, #f2f2f2);

希望这对你们有帮助。

这要归功于@Gidgidonihah找到了最初的解决方案。

使用背景-混合模式和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在所有现代浏览器上都可以运行

你可以简单地输入:

背景:线性渐变( 到下, rgba (0, 0, 0, 0), rgba (0, 0, 0, 100) ), url(. . /图片/ image.jpg);

要意识到的一件事是,第一个定义的背景图像在堆栈中最上面。最后定义的图像将位于最底部。这意味着,要在图像后面有一个背景渐变,你需要:

身体{ 背景-图像: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渐变可以做的有趣事情的博文

我总是使用以下代码使其工作。这里有一些注意事项:

如果你把图片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 >

这种技术就像我们在这里描述的有多个背景图像一样