我如何使用CSS3渐变为我background-color
,然后应用一个background-image
应用某种光透明纹理?
多个背景!
body {
background: #eb01a5;
background-image: url("IMAGE_URL"); /* fallback */
background-image: url("IMAGE_URL"), linear-gradient(#eb01a5, #d13531); /* W3C */
}
如果您还想为图像设置背景位置,则可以使用此方法:
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
}
}
要意识到的是,第一个定义的背景图像位于堆栈的最顶层.最后定义的图像将位于最底部.这意味着,要在图像后面有背景渐变,您需要:
body {
background-image: url("http://www.skrenta.com/images/stackoverflow.jpg"), linear-gradient(red, yellow);
background-image: url("http://www.skrenta.com/images/stackoverflow.jpg"), -webkit-gradient(linear, left top, left bottom, from(red), to(yellow));
background-image: url("http://www.skrenta.com/images/stackoverflow.jpg"), -moz-linear-gradient(top, red, yellow);
}
你可以简单地输入:
background: linear-gradient(
to bottom,
rgba(0,0,0, 0),
rgba(0,0,0, 100)
),url(../images/image.jpg);
我总是使用以下代码使其工作.有一些注意事项:
如果在渐变之前放置图像URL,则此图像将按预期显示在渐变上方.
.background-gradient {
background: url('http://trungk18.github.io/img/trungk18.png') no-repeat, -moz-linear-gradient(135deg, #6ec575 0, #3b8686 100%);
background: url('http://trungk18.github.io/img/trungk18.png') no-repeat, -webkit-gradient(135deg, #6ec575 0, #3b8686 100%);
background: url('http://trungk18.github.io/img/trungk18.png') no-repeat, -webkit-linear-gradient(135deg, #6ec575 0, #3b8686 100%);
background: url('http://trungk18.github.io/img/trungk18.png') no-repeat, -o-linear-gradient(135deg, #6ec575 0, #3b8686 100%);
background: url('http://trungk18.github.io/img/trungk18.png') no-repeat, -ms-linear-gradient(135deg, #6ec575 0, #3b8686 100%);
background: url('http://trungk18.github.io/img/trungk18.png') no-repeat, linear-gradient(135deg, #6ec575 0, #3b8686 100%);
height: 500px;
width: 500px;
}
我的解决方案
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负责交叉浏览器代码,现在我必须声明的是每个按钮的精灵位置.对于按钮活动和悬停状态,可以很容易地扩展此主体.
如果您在下载背景图片时遇到奇怪的错误,请使用W3C链接检查器:https : //validator.w3.org/checklink
这是我使用的最新混合器(鸣谢:PSA:不要使用渐变发生器):
.buttonAkc { .gradientBackground(@imageName: 'accept.png'); background-repeat: no-repeat !important; background-position: center right, top left !important; } .buttonAkc:hover { .gradientBackgroundHover('accept.png'); } .gradientBackground(@startColor: #fdfdfd, @endColor: #d9d9db, @imageName) { background-color: mix(@startColor, @endColor, 60%); // fallback background-image: url("@{img-folder}/@{imageName}?v=@{version}"); // fallback background: url("@{img-folder}/@{imageName}?v=@{version}") no-repeat scroll right center, -webkit-linear-gradient(top, @startColor 0%, @endColor 100%) no-repeat scroll left top; // Chrome 10-25, Safari 5.1-6 background: url("@{img-folder}/@{imageName}?v=@{version}") no-repeat scroll right center, linear-gradient(to bottom, @startColor 0%, @endColor 100%) no-repeat scroll left top; } .gradientBackgroundHover(@imageName) { .gradientBackground(#fdfdfd, #b5b6b9, @imageName); }