【问题标题】:css gradient border with transparency具有透明度的CSS渐变边框
【发布时间】:2020-12-16 12:36:34
【问题描述】:

我有这个项目需要画一个圆圈。我尝试用一​​个 div 来做到这一点,我给它一个绘制线性渐变的边框。但是这个边界也需要是透明的。但我似乎无法让它工作。我让渐变工作。但我不知道如何为这个边框添加透明度。

这是我现在使用的代码:

.gradient-circle {
  --b: 5px; /* border width*/

  display: inline-block;
  margin: 10px;
  z-index: 0;

  width: 26rem;
  height: 26rem;
}

.gradient-circle:before {
  content: "";
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--c, linear-gradient(to top, #5454d4, rgba(249, 116, 104)));
  -webkit-mask: radial-gradient(
    farthest-side,
    transparent calc(100% - var(--b) - 1px),
    #fff calc(100% - var(--b))
  );
  mask: radial-gradient(
    farthest-side,
    transparent calc(100% - var(--b) - 1px),
    #fff calc(100% - var(--b))
  );
  border-radius: 50%;
}

这是我使用上述 CSS 得到的边框:

圆应该是这样的:

【问题讨论】:

    标签: css border gradient transparency


    【解决方案1】:

    使用2个遮罩层结合mask-composite

    .gradient-circle {
      --b: 5px; /* border width*/
    
      display: inline-block;
      margin: 10px;
      z-index: 0;
    
      width: 26rem;
      height: 26rem;
      position:relative;
    }
    
    .gradient-circle:before {
      content: "";
      position: absolute;
      z-index: -1;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: var(--c, linear-gradient(to top, #5454d4, rgba(249, 116, 104)));
      -webkit-mask: 
      linear-gradient(45deg,#fff,transparent 80%),
      radial-gradient(
        farthest-side,
        transparent calc(100% - var(--b) - 1px),
        #fff calc(100% - var(--b))) content-box;
      -webkit-mask-composite: destination-in;
      mask-composite: intersect;
      border-radius: 50%;
      padding:1px;
    }
    
    body {
      background:#f2f2f2;
    }
    <div class="gradient-circle"></div>

    【讨论】:

    • 有没有办法去掉圆圈右上角的细线?感谢您的解决方案。这帮助很大!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    相关资源
    最近更新 更多