【问题标题】:How to set multi-color border using border-image and linear-gradient?如何使用边框图像和线性渐变设置多色边框?
【发布时间】:2019-09-20 03:38:51
【问题描述】:

正在尝试制作如下图所示的多色底部边框,但失败了。

尝试使用border-image: linear-gradient(),但没有成功。它只得到其中一种颜色:#707070

a {
  color: #707070 !important;
  font-size: 20px;
  text-decoration: none;
  margin-right: 50px;
  border-bottom: 5px solid;
  border-image: linear-gradient(to bottom, #707070 20%, #a4c0e9 20%, #a4c0e9 80%, #707070 80%) 5;
}
<div id="nav">
  <a href="/">HOME</a>
  <a href="/projects">PROJECTS</a>
  <a href="/resume">RESUME</a>
</div>

【问题讨论】:

    标签: css linear-gradients border-image


    【解决方案1】:

    问题是百分比是相对于元素而不是边界的,这将使20% 大于5px

    您需要考虑像素值。您还需要从底部开始,因为顶部引用也是元素的顶部:

    a {
      color: #707070 !important;
      font-size: 20px;
      text-decoration: none;
      margin-right: 50px;
      border-bottom: 5px solid;
      border-image: 
        linear-gradient(to top, #707070 1px, #a4c0e9 1px, #a4c0e9 4px, #707070 4px) 5;
    }
    &lt;a&gt;A link element&lt;/a&gt;

    或者用它作为背景,会更容易处理:

    a {
      color: #707070 !important;
      font-size: 20px;
      text-decoration: none;
      margin-right: 50px;
      border-bottom: 5px solid transparent;
      background: 
        linear-gradient(to bottom, #707070 20%, #a4c0e9 20%, #a4c0e9 80%, #707070 80%) bottom/100% 5px border-box no-repeat;
    }
    &lt;a&gt;A link element&lt;/a&gt;

    相关:border-image-slice for gradient border image


    这是一个更好地说明您遇到的问题的示例:

    .box {
      width:100px;
      height:100px;
      display:inline-block;
      border-bottom:10px solid rgba(0,0,0,0.2);
    
    }
    <div class="box" style="background:linear-gradient(to bottom, #707070 20%, #a4c0e9 20%, #a4c0e9 80%, #707070 80%) border-box">
    </div>
    
    <div class="box" style="border-image:linear-gradient(to bottom, #707070 20%, #a4c0e9 20%, #a4c0e9 80%, #707070 80%) 10 fill">
    </div>
    
    <div class="box" style="border-image:linear-gradient(to bottom, #707070 20%, #a4c0e9 20%, #a4c0e9 80%, #707070 80%) 10 ">
    </div>

    在本例中,第一个框显示了我们将使用的渐变。在第二个中,我们使用fill 将其应用于边框,同时为中间区域着色,在最后一个中,我们只为border-bottom 着色

    【讨论】:

      【解决方案2】:

      发布您的代码或指向 jsfiddle 或 codepen 的链接总是好的。您可以参考帮助部分了解如何提问?

      就您而言,您在此处展示的设计看起来不像渐变。我有纯色和边框。渐变可以用在边框上,可以看here

      为了实现您所展示的内容,我会使用 :after 和 following 样式,

       a{
            position: relative;
            padding-bottom: 20px;
      
          &:after{
            position: absolute;
            content: '';
            height: 10px;
            width: 100%;
            left: 0;
            bottom: 0;
            background: #a4c0e9;
            border-top: 1px solid #707070;
            border-bottom: 1px solid #707070;
          }
          }
      

      【讨论】:

      • 非常感谢,您的方法可行。但我认为上面的border-image也可以工作,考虑到类似的情况:stackoverflow.com/questions/38850419/…
      • 在这些示例中,背景颜色(在您的情况下为 #a4c0e9)来自单独的元素,它们仅在边框上应用渐变。
      猜你喜欢
      • 2019-11-16
      • 1970-01-01
      • 2016-06-25
      • 2021-03-18
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 2021-11-17
      相关资源
      最近更新 更多