Ron*_*ld 1203 css gradient background-image css3
我如何使用CSS3渐变为我background-color,然后应用一个background-image应用某种光透明纹理?
Gid*_*hah 1488
多个背景!
body {
background: #eb01a5;
background-image: url("IMAGE_URL"); /* fallback */
background-image: url("IMAGE_URL"), linear-gradient(#eb01a5, #d13531); /* W3C */
}Run Code Online (Sandbox Code Playgroud)
这两行是任何不做渐变的浏览器的后备.请参阅下面的IE <9堆叠图像的注意事项.
最后一行为可以处理它们的浏览器设置背景图像和渐变.
几乎所有当前浏览器都支持多个背景图像和CSS背景.有关浏览器支持,请参阅http://caniuse.com/#feat=css-gradients.有关您不需要多个浏览器前缀的好帖子,请参阅http://codepen.io/thebabydino/full/pjxVWp/
层堆栈
应该注意的是,第一个定义的图像将位于堆栈的最顶层.在这种情况下,图像位于渐变的TOP上.
有关背景分层的更多信息,请参见http://www.w3.org/TR/css3-background/#layering.
仅堆叠图像(声明中没有渐变)对于IE <9
IE9及以上版本可以以相同的方式堆叠图像.您可以使用它为ie9创建渐变图像,但我个人不会.但是,在仅使用图像时要注意,即<9将忽略回退语句而不显示任何图像.包含渐变时不会发生这种情况.在这种情况下,为了使用单个后备图像,我建议使用Paul Irish的精彩条件HTML元素以及您的后备代码:
.lte9 #target{ background-image: url("IMAGE_URL"); }
Run Code Online (Sandbox Code Playgroud)
背景位置,尺码等
适用于单个图像的其他属性也可以用逗号分隔.如果仅提供1个值,则将应用于包括渐变的所有堆叠图像.background-size: 40px;将图像和渐变限制为40px的高度和宽度.然而,使用background-size: 40px, cover;将使图像40px和渐变将覆盖元素.要仅将设置应用于一个图像,请为另一个图像设置默认值:background-position: 50%, 0 0;或者对于支持它的浏览器使用initial:background-position: 50%, initial;
您也可以使用背景速记,但这会删除后备颜色和图像.
body{
background: url("IMAGE_URL") no-repeat left top, linear-gradient(#eb01a5, #d13531);
}
Run Code Online (Sandbox Code Playgroud)
这同样适用于背景位置,背景重复等.
Tam*_*Pap 81
如果您还想为图像设置背景位置,则可以使用此方法:
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
Run Code Online (Sandbox Code Playgroud)
或者你也可以创建一个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
}
}
Run Code Online (Sandbox Code Playgroud)
小智 45
要意识到的是,第一个定义的背景图像位于堆栈的最顶层.最后定义的图像将位于最底部.这意味着,要在图像后面有背景渐变,您需要:
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);
}Run Code Online (Sandbox Code Playgroud)
您还可以定义图像的背景位置和背景大小.我整理了一篇关于CSS3渐变可以做的一些有趣事情的博客文章
Nej*_*eli 28
你可以简单地输入:
background: linear-gradient(
to bottom,
rgba(0,0,0, 0),
rgba(0,0,0, 100)
),url(../images/image.jpg);Run Code Online (Sandbox Code Playgroud)
tru*_*k18 17
我总是使用以下代码使其工作.有一些注意事项:
.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;
}Run Code Online (Sandbox Code Playgroud)
<div class="background-gradient"></div>Run Code Online (Sandbox Code Playgroud)
.background-gradient {
background: -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;
background: -webkit-linear-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat;
background: -o-linear-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat;
background: -ms-linear-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat;
background: linear-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat;
width: 500px;
height: 500px;
}Run Code Online (Sandbox Code Playgroud)
<div class="background-gradient"></div>Run Code Online (Sandbox Code Playgroud)
这种技术与我们在此处描述的多个背景图像相同
Fra*_*rzi 16
我的解决方案
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);
Run Code Online (Sandbox Code Playgroud)
cor*_*lls 14
我有一个实现,我需要将这项技术更进一步,并想要概述我的工作.下面的代码做了同样的事情,但使用了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);
}
Run Code Online (Sandbox Code Playgroud)
SASS和Bourbon负责交叉浏览器代码,现在我必须声明的是每个按钮的精灵位置.对于按钮活动和悬停状态,可以很容易地扩展此主体.
background-blend-mode和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;
}
Run Code Online (Sandbox Code Playgroud)
如果调整颜色值的alpha 值rgba(在示例中为.85),则可以控制透明度。
此外,background-blend-mode您还可以使用其他值来获得一些真正有创意的结果。
注意:background-blend-mode: color;在 Firefox 上失败,但multiply在所有现代浏览器上工作
小智 5
如果您在下载背景图片时遇到奇怪的错误,请使用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);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您想要一个中心有单个背景图像的渐变,您可以使用一行代码来完成,如下所示:
body {
background: url(logo.png) no-repeat fixed center center, linear-gradient(#00467f, #a5cc82) fixed;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
677278 次 |
| 最近记录: |