使用 css 带渐变的顶部边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21028005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Top border with gradient using css
提问by Prithviraj Mitra
I would like to put a gradient border at the top of a div.
我想在 div 的顶部放置一个渐变边框。
So the start color should be #c4268c and ends with #9a0b72
所以起始颜色应该是#c4268c,以#9a0b72结尾
<div class="bordertest"></div>
For easing here is the fiddle :http://jsfiddle.net/aKhjk/
为了放松这里是小提琴:http: //jsfiddle.net/aKhjk/
I searched but could not find a suitable way.
我搜索但找不到合适的方法。
采纳答案by G-Cyrillus
you could use an image http://border-image.com/or use a pseudo element over your border :
您可以使用图像http://border-image.com/或在边框上使用伪元素:
.bordertest {
height:300px;
width:300px;
border-top:30px solid #c4268c;
background:#000;
position:relative;
margin:1em;
}
.bordertest:first-child:before {
content:'';
position:absolute;
width:100%;
height:30px;
background:linear-gradient(to left, #c4268c, #9a0b72);
top:-30px;
left:0;
}
http://jsfiddle.net/aKhjk/1/- jsfiddle.net/aKhjk/3
http://jsfiddle.net/aKhjk/1/- jsfiddle.net/aKhjk/3
回答by Cam
Try this, its also cross browser ready. So not so much work for you to do.
试试这个,它也准备好跨浏览器了。所以没有那么多工作让你做。
http://jsfiddle.net/cornelas/aKhjk/6/
http://jsfiddle.net/cornelas/aKhjk/6/
.bordertest {
height:300px;
width:300px;
border-top:30px solid #c4268c;
/** Created at http://www.colorzilla.com/gradient-editor/ **/
background: rgb(196,38,140); /* Old browsers */
background: -moz-linear-gradient(top, rgba(196,38,140,1) 0%, rgba(154,11,114,1) 5%, rgba(0,0,0,1) 10%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(196,38,140,1)), color-stop(5%,rgba(154,11,114,1)), color-stop(10%,rgba(0,0,0,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(196,38,140,1) 0%,rgba(154,11,114,1) 5%,rgba(0,0,0,1) 10%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(196,38,140,1) 0%,rgba(154,11,114,1) 5%,rgba(0,0,0,1) 10%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, rgba(196,38,140,1) 0%,rgba(154,11,114,1) 5%,rgba(0,0,0,1) 10%); /* IE10+ */
background: linear-gradient(to bottom, rgba(196,38,140,1) 0%,rgba(154,11,114,1) 5%,rgba(0,0,0,1) 10%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#c4268c', endColorstr='#000000',GradientType=0 ); /* IE6-9 */
}
回答by Mehmet Ali Peker
Now, you can use linear gradients with border-image attribute in all modern browsers.
现在,您可以在所有现代浏览器中使用带有边框图像属性的线性渐变。
.repeating-linear {
color: pink;
border: 10px solid pink;
border-image: repeating-linear-gradient( 45deg, pink, pink 1%, purple 1%, purple 8%) 10;
}
See: https://css-tricks.com/almanac/properties/b/border-image/
请参阅:https: //css-tricks.com/almanac/properties/b/border-image/