我正在尝试将渐变应用到边框,我认为这样做很简单:
border-color: -moz-linear-gradient(top, #555555, #111111);
这不起作用,有谁知道边界渐变的正确方法是什么 .
WebKit现在(至少Chrome 12)支持渐变作为边框图像:
-webkit-border-image: -webkit-gradient(linear, left top, left bottom, from(#00abeb), to(#fff), color-stop(0.5, #fff), color-stop(0.5, #66cc00)) 21 30 30 21 repeat repeat;
Prooflink - http://www.webkit.org/blog/1424/css3-gradients/浏览器支持:http://caniuse.com/#search=border-image
而不是边框,我会使用背景渐变和填充 . 相同的外观,但更容易,更支持 .
一个简单的例子:
<div class="g"> <div>bla</div> </div>
CSS:
.g { background-image: -webkit-gradient(linear, left bottom, left top, color-stop(0.33, rgb(14,173,173)), color-stop(0.67, rgb(0,255,255))); background-image: -moz-linear-gradient(center bottom, rgb(14,173,173) 33%, rgb(0,255,255) 67% ); padding: 2px; } .g > div { background: #fff; }
编辑:您也可以利用 :before 选择器@WalterSchwarz在jsFiddle指出
:before
Mozilla目前仅支持CSS渐变作为background-image属性的值,以及简写背景中的值 .
Example 3 - Gradient Borders border: 8px solid #000; -moz-border-bottom-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc; -moz-border-top-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc; -moz-border-left-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc; -moz-border-right-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc; padding: 5px 5px 5px 15px;
这(据我所知)可以防止将“图像”默认切片为部分 - 没有它,如果边框仅在一侧,则不会出现任何内容,如果它位于整个元素周围,则每个角落会出现四个微小的渐变 .
border-bottom: 6px solid transparent; border-image: linear-gradient(to right, red , yellow); border-image-slice: 1;
试试这个,在网络套件上工作正常
http://jsfiddle.net/284sa/
.border { width: 400px; padding: 20px; border-top: 10px solid #FFFF00; border-bottom:10px solid #FF0000; background-image: linear-gradient(#FFFF00, #FF0000), linear-gradient(#FFFF00, #FF0000) ; background-size:10px 100%; background-position:0 0, 100% 0; background-repeat:no-repeat; } <div class="border">Hello!</div>
这是一个黑客攻击,但是在某些情况下你可以通过使用background-image指定渐变然后使用box-shadow屏蔽实际背景来实现这种效果 . 例如:
p { display: inline-block; width: 50px; height: 50px; /* The background is used to specify the border background */ background: -moz-linear-gradient(45deg, #f00, #ff0); background: -webkit-linear-gradient(45deg, #f00, #ff0); /* Background origin is the padding box by default. Override to make the background cover the border as well. */ -moz-background-origin: border; background-origin: border-box; /* A transparent border determines the width */ border: 4px solid transparent; border-radius: 8px; box-shadow: inset 0 0 12px #0cc, /* Inset shadow */ 0 0 12px #0cc, /* Outset shadow */ inset -999px 0 0 #fff; /* The background color */ }
来自:http://blog.nateps.com/the-elusive-css-border-gradient
我同意szajmon . 他和Quentin的答案唯一的问题是跨浏览器兼容性 .
HTML:
.g { background-image: -webkit-linear-gradient(300deg, white, black, white); /* webkit browsers (Chrome & Safari) */ background-image: -moz-linear-gradient(300deg, white, black, white); /* Mozilla browsers (Firefox) */ filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#000000', gradientType='1'); /* Internet Explorer */ background-image: -o-linear-gradient(300deg,rgb(255,255,255),rgb(0,0,0) 50%,rgb(255,255,255) 100%); /* Opera */ } .g > div { background: #fff; }
Webkit supports gradients in borders,现在接受Mozilla格式的渐变 .
Firefox声称以两种方式支持渐变:
Using border-image with border-image-source
Using border-right-colors (right/left/top/bottom)
IE9没有支持 .
实现相同效果的另一个方法是利用多个背景图像,IE9,新版Firefox和大多数基于WebKit的浏览器支持的功能:http://caniuse.com/#feat=multibackgrounds
在IE6-8中还有一些使用多个背景的选项:http://www.beyondhyper.com/css3-multiple-backgrounds-in-non-supportive-browsers/
例如,假设您想要一个5px宽的左边框,它是从蓝色到白色的线性渐变 . 将渐变创建为图像并导出到PNG . 列出左边框渐变之后的任何其他CSS背景:
#theBox { background: url(/images/theBox-leftBorderGradient.png) left no-repeat, ...; }
您可以通过更改 background 速记属性的背景位置部分,使此技术适应顶部,右侧和底部边框渐变 .
background
这是给定示例的jsFiddle:http://jsfiddle.net/jLnDt/
来自Css-Tricks的渐变边界:http://css-tricks.com/examples/GradientBorder/
.multbg-top-to-bottom { border-top: 3px solid black; background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#000), to(transparent)); background-image: -webkit-linear-gradient(#000, transparent); background-image: -moz-linear-gradient(#000, transparent), -moz-linear-gradient(#000, transparent); background-image: -o-linear-gradient(#000, transparent), -o-linear-gradient(#000, transparent); background-image: linear-gradient(#000, transparent), linear-gradient(#000, transparent); -moz-background-size: 3px 100%; background-size: 3px 100%; background-position: 0 0, 100% 0; background-repeat: no-repeat; }
对于跨浏览器支持,您可以尝试使用 :before 或 :after 伪元素模仿渐变边框,具体取决于您要执行的操作 .
:after
试试这段代码
.gradientBoxesWithOuterShadows { height: 200px; width: 400px; padding: 20px; background-color: white; /* outer shadows (note the rgba is red, green, blue, alpha) */ -webkit-box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.4); -moz-box-shadow: 0px 1px 6px rgba(23, 69, 88, .5); /* rounded corners */ -webkit-border-radius: 12px; -moz-border-radius: 7px; border-radius: 7px; /* gradients */ background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, white), color-stop(15%, white), color-stop(100%, #D7E9F5)); background: -moz-linear-gradient(top, white 0%, white 55%, #D5E4F3 130%); }
或者可以参考这个小提琴:http://jsfiddle.net/necolas/vqnk9/
这是一种很好的半跨浏览器方式,可以使渐变边框淡出一半 . 只需将 color-stop 设置为 rgba(0, 0, 0, 0)
rgba(0, 0, 0, 0)
.fade-out-borders { min-height: 200px; /* for example */ -webkit-border-image: -webkit-gradient(linear, 0 0, 0 50%, from(black), to(rgba(0, 0, 0, 0))) 1 100%; -webkit-border-image: -webkit-linear-gradient(black, rgba(0, 0, 0, 0) 50%) 1 100%; -moz-border-image: -moz-linear-gradient(black, rgba(0, 0, 0, 0) 50%) 1 100%; -o-border-image: -o-linear-gradient(black, rgba(0, 0, 0, 0) 50%) 1 100%; border-image: linear-gradient(to bottom, black, rgba(0, 0, 0, 0) 50%) 1 100%; } <div class="fade-out-border"></div>
用法说明:
Formal grammar: linear-gradient( [ <angle> | to <side-or-corner> ,]? <color-stop> [, <color-stop>]+ ) \---------------------------------/ \----------------------------/ Definition of the gradient line List of color stops
更多这里:https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient
试试这个,它对我有用 .
.yourDivClass { border: 1px solid transparent; border-image: linear-gradient(to top, #3a4ed5 0%, #f2f2f2 100%); border-image-slice: 1; }
链接是小提琴https://jsfiddle.net/yash009/kayjqve3/1/希望这有帮助
14 回答
WebKit现在(至少Chrome 12)支持渐变作为边框图像:
Prooflink - http://www.webkit.org/blog/1424/css3-gradients/
浏览器支持:http://caniuse.com/#search=border-image
而不是边框,我会使用背景渐变和填充 . 相同的外观,但更容易,更支持 .
一个简单的例子:
CSS:
JsFiddle
编辑:您也可以利用
:before
选择器@WalterSchwarz在jsFiddle指出border-image-slice将扩展CSS边框图像渐变
这(据我所知)可以防止将“图像”默认切片为部分 - 没有它,如果边框仅在一侧,则不会出现任何内容,如果它位于整个元素周围,则每个角落会出现四个微小的渐变 .
试试这个,在网络套件上工作正常
http://jsfiddle.net/284sa/
这是一个黑客攻击,但是在某些情况下你可以通过使用background-image指定渐变然后使用box-shadow屏蔽实际背景来实现这种效果 . 例如:
来自:http://blog.nateps.com/the-elusive-css-border-gradient
我同意szajmon . 他和Quentin的答案唯一的问题是跨浏览器兼容性 .
HTML:
CSS:
Webkit supports gradients in borders,现在接受Mozilla格式的渐变 .
Firefox声称以两种方式支持渐变:
Using border-image with border-image-source
Using border-right-colors (right/left/top/bottom)
IE9没有支持 .
实现相同效果的另一个方法是利用多个背景图像,IE9,新版Firefox和大多数基于WebKit的浏览器支持的功能:http://caniuse.com/#feat=multibackgrounds
在IE6-8中还有一些使用多个背景的选项:http://www.beyondhyper.com/css3-multiple-backgrounds-in-non-supportive-browsers/
例如,假设您想要一个5px宽的左边框,它是从蓝色到白色的线性渐变 . 将渐变创建为图像并导出到PNG . 列出左边框渐变之后的任何其他CSS背景:
您可以通过更改
background
速记属性的背景位置部分,使此技术适应顶部,右侧和底部边框渐变 .这是给定示例的jsFiddle:http://jsfiddle.net/jLnDt/
来自Css-Tricks的渐变边界:http://css-tricks.com/examples/GradientBorder/
对于跨浏览器支持,您可以尝试使用
:before
或:after
伪元素模仿渐变边框,具体取决于您要执行的操作 .试试这段代码
或者可以参考这个小提琴:http://jsfiddle.net/necolas/vqnk9/
这是一种很好的半跨浏览器方式,可以使渐变边框淡出一半 . 只需将 color-stop 设置为
rgba(0, 0, 0, 0)
用法说明:
更多这里:https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient
试试这个,它对我有用 .
链接是小提琴https://jsfiddle.net/yash009/kayjqve3/1/希望这有帮助