border-image
属性创建带有4种颜色的渐变边框。通常,渐变从一种颜色逐渐过渡到另一种颜色,并产生模糊效果,但是通过设置color-stops(百分比值),使得一种颜色的终点与下一种颜色的起点相同,使得颜色停止并产生块状效果。border-image-width
和渐变方向来将边框设置为所需的边框。例如,顶部和底部边框需要从左侧到右侧进行渐变,而左侧和右侧边框需要从上到下进行渐变。border-image
的唯一缺点是目前对此属性的支持浏览器支持较差。IE10-不支持此属性。
.bordered-top {
border-image: linear-gradient(to right, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
border-image-slice: 1;
border-image-width: 4px 0px 0px 0px;
}
.bordered-bottom {
border-image: linear-gradient(to right, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
border-image-slice: 1;
border-image-width: 0px 0px 4px 0px;
}
.bordered-left {
border-image: linear-gradient(to bottom, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
border-image-slice: 1;
border-image-width: 0px 0px 0px 4px;
}
.bordered-right {
border-image: linear-gradient(to bottom, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
border-image-slice: 1;
border-image-width: 0px 4px 0px 0px;
}
div {
height: 100px;
width: 300px;
padding: 10px;
background: beige;
margin: 10px;
}
<!-- library added only for old browser support -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class='bordered-top'>Border only on top</div>
<div class='bordered-bottom'>Border only on bottom</div>
<div class='bordered-left'>Border only on left</div>
<div class='bordered-right'>Border only on right</div>
要支持IE10+,您可以通过使用渐变来模仿与以下代码片段中的border-image
不同的行为,而是将其用于background-image
属性。
与border-image
不同,这里无法使用border-image-width
来控制应用边框的侧面,我们必须使用background-position
来定位所需位置的图像。
background-size
确定边框的厚度。对于上下边框,x轴上的大小应为100%,y轴上的大小为边框的厚度。对于左右边框,y轴上的大小应为100%,x轴上的大小为边框的厚度。
.bordered-top {
background-image: linear-gradient(to right, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
background-size: 100% 4px;
background-repeat: no-repeat;
background-position: 0% 0%;
}
.bordered-bottom {
background-image: linear-gradient(to right, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
background-size: 100% 4px;
background-repeat: no-repeat;
background-position: 0% 100%;
}
.bordered-left {
background-image: linear-gradient(to bottom, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
background-size: 4px 100%;
background-repeat: no-repeat;
background-position: 0% 0%;
}
.bordered-right {
background-image: linear-gradient(to bottom, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
background-size: 4px 100%;
background-repeat: no-repeat;
background-position: 100% 0%;
}
div {
height: 100px;
width: 300px;
padding: 10px;
background: beige;
margin: 10px;
}
<!-- library added only for old browser support -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class='bordered-top'>Border only on top</div>
<div class='bordered-bottom'>Border only on bottom</div>
<div class='bordered-left'>Border only on left</div>
<div class='bordered-right'>Border only on right</div>
div {
width: 200px;
height: 100px;
position: relative;
background: grey;
}
div:after {
bottom: 0;
position: absolute;
content: "";
width: 50px;
height: 5px;
background: green;
box-shadow: 50px 0 0 0 red, 100px 0 0 0 orange, 150px 0 0 0 green;
}
<div></div>
如果应用在一个更大的 div
上,则会像这样:
div {
width: 500px;
height: 100px;
background: orange;
position: relative;
}
div:after {
bottom: 0;
position: absolute;
content: "";
width: 100px;
height: 5px;
background: green;
box-shadow: 100px 0 0 0 darkred, 200px 0 0 0 red, 300px 0 0 0 yellow, 400px 0 0 0 tomato;
}
<div></div>
我已经采用了哈利的方法并进行了修改以适应我的需求。现在我拥有:
border-image: linear-gradient(to right, #8CC63F 0%, #006F3B 25%, #ED1C24 25%, #9B1B1E 50%, #85CDEC 50%, #217EC2 75%, #FFC20E 75%, #F04E23 100%);
border-image-slice: 3;
border-image-width: 0px 0px 4px 0px;
border-image-repeat: round;
这是我需求中最好的解决方案。
复杂但酷炫的解决方案:使用SVG(例如<svg>
标签),添加4个路径,分配不同的stroke-dasharray
和stroke-color
属性。
更简单但仍然酷炫的解决方案:尝试使用border-image。(请参见Harry的答案)
如果您只需要一个边框,非常简单的解决方案是创建一个图像,将其作为背景图像,仅在一个轴上重复它,将其定位在容器的边缘,例如(用于底部边框)
.container {
background-image: url(image.png);
background-repeat: repeat-x;
background-position: bottom left;
}
.solid{
width: 300px;
border-image: linear-gradient(to right, red 25%, blue 25%, blue 50%, green 50%, green 75%, orange 75%);
border-image-slice: 4;
}
width
、相同的border-image-slice
和相同的颜色顺序,这让人怀疑你抄袭了别人的作品。请注意修改以避免此类情况。 - Harry最好的解决方案是使用linear-gradient
。毫无疑问。
但对于初学者来说,这个解决方案可能很有用。通过使用2-3-4种甚至更多的颜色,这是正确的方法。虽然不是这个问题的最佳解决方案,但也许在阅读本文时,有人想更好地理解如何使用边框来处理颜色。
<html>
<head>
<style>
p.one {
border-style: solid;
border-color: #0000ff;
}
p.two {
border-style: solid;
border-color: #ff0000 #0000ff;
}
p.three {
border-style: solid;
border-color: #ff0000 #00ff00 #0000ff;
}
p.four {
border-style: solid;
border-color: #ff0000 #00ff00 #0000ff rgb(250,0,255);
}
</style>
</head>
<body>
<p class="one">One-colored border!</p>
<p class="two">Two-colored border!</p>
<p class="three">Three-colored border!</p>
<p class="four">Four-colored border!</p>
</body>
</html>