仅为顶部和底部边缘创建线性渐变边框?

问题描述 投票:3回答:1

使用下面的代码,我只能为元素的底部边缘生成线性渐变border-image。如何修改代码使其也成为顶部代码?

div {
    /* gradient shining border */
    border-style: solid;
    border-width: 3px;
    -webkit-border-image: -webkit-linear-gradient(
        left,
        rgba(0,0,0,1) 1%,
        rgba(0,255,255,1) 50%,
        rgba(0,0,0,1) 100%
    ) 0 0 100% 0/0 0 3px 0 stretch;
    -moz-border-image: -moz-linear-gradient(
        left,
        rgba(0,0,0,1) 1%,
        rgba(0,255,255,1) 50%,
        rgba(0,0,0,1) 100%
    ) 0 0 100% 0/0 0 3px 0 stretch;
    -o-border-image: -o-linear-gradient(
        left,
        rgba(0,0,0,1) 1%,
        rgba(0,255,255,1) 50%,
        rgba(0,0,0,1) 100%
    ) 0 0 100% 0/0 0 3px 0 stretch;
    border-image: linear-gradient(
        to left,
        rgba(0,0,0,1) 1%,
        rgba(0,255,255,1) 50%,
        rgba(0,0,0,1) 100%
    ) 0 0 100% 0/0 0 3px 0 stretch;
}

当前输出:

enter image description here

html css border linear-gradients
1个回答
10
投票

您正在使用速记border-image属性来设置渐变的大小,并且根据提供的值,顶部,左侧和右侧边框均无效。

100%设置为顶部边界的宽度,将3px设置为其高度将导致仅在顶部和底部应用渐变。

border-image: linear-gradient(to left, rgba(0, 0, 0, 1) 1%, rgba(0, 255, 255, 1) 50%, rgba(0, 0, 0, 1) 100%) 
              100% 0 100% 0/3px 0 3px 0 stretch;

在以上代码行中,100% 0 100% 0/3px 0 3px 0表示每侧渐变边框的大小(读为[top] [right] [bottom] [left])。最初是0 0 100% 0/0 0 3px 0

div {
  /* gradient shining border */
  border-style: solid;
  border-width: 3px;
  border-image: linear-gradient(to left, rgba(0, 0, 0, 1) 1%, rgba(0, 255, 255, 1) 50%, rgba(0, 0, 0, 1) 100%) 
                100% 0 100% 0/3px 0 3px 0 stretch;
  
  /* other demo stuff */
  height: 50px;
  line-height: 50px;
  background-color: #222;
  color: white;
  text-align: center;  
}
<div>Some content</div>

请注意,如果需要支持IE10及更低版本,则border-image property still has pretty low browser support和将不起作用。代替它,您可以像下面的代码片段一样使用border-image来产生类似的效果。这也可以在IE10中使用(但仍无法在IE9中使用,因为它们根本不支持渐变)。

background-image
div {
  /* gradient shining border */
  background-image: linear-gradient(to left, rgba(0, 0, 0, 1) 1%, rgba(0, 255, 255, 1) 50%, rgba(0, 0, 0, 1) 100%), 
                    linear-gradient(to left, rgba(0, 0, 0, 1) 1%, rgba(0, 255, 255, 1) 50%, rgba(0, 0, 0, 1) 100%);
  background-size: 100% 3px;
  background-position: 0% 0%, 0% 100%;
  background-repeat: no-repeat;
  
  /* other demo stuff */
  height: 50px;
  line-height: 50px;
  background-color: #222;
  color: white;
  text-align: center;
}
© www.soinside.com 2019 - 2024. All rights reserved.