【问题标题】:Create linear gradient border for top and bottom edges only?仅为顶部和底部边缘创建线性渐变边框?
【发布时间】:2015-09-01 10:43:13
【问题描述】:

使用下面的代码,我只能为元素的底部边缘生成线性渐变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;
}

电流输出:

【问题讨论】:

    标签: html css border linear-gradients


    【解决方案1】:

    您正在使用简写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>

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

    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;
    }
    <div>Some content</div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 2023-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多