我想在div的顶部放一个渐变边框。
因此,起始颜色应为#c4268c,并以#9a0b72
结束<div class="bordertest"></div>
为了缓和,这里是小提琴:http://jsfiddle.net/aKhjk/
我搜索但找不到合适的方式。
答案 0 :(得分:11)
您可以使用图片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
答案 1 :(得分:3)
试试这个,它也可以跨浏览器了。所以没有那么多工作要做。
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 */
}
答案 2 :(得分:1)
现在,您可以在所有现代浏览器中使用带有border-image属性的线性渐变。
.bordertest {
color: pink;
border: 10px solid pink;
border-image: repeating-linear-gradient( 45deg, pink, pink 1%, purple 1%, purple 8%) 10;
}
请参阅:https://css-tricks.com/almanac/properties/b/border-image/