纯CSS渐变圆形边框

10

我有一个UI需求 输入图像描述

目前,我有一个工作的解决方案,它由一个具有固定高度和宽度以及外部渐变边框背景图像的div和一个伪元素组成,位置为absolute,具有内部边框的背景图像。

.div {
    position: relative;
    width: 254px;
    height: 254px;
    border: 2px solid transparent;
    border-radius: 50%;
    background: url(../img/gradient_border_circle.png) no-repeat 50%;
}
div:before {
    content: "";
    position: absolute;
    top: 50%;
    transform: translate(-50%,-50%);
    left: 50%;
    width: 98px;
    height: 98px;
    border-radius: 50%;
    background: url(../img/gradient_border_circle_inner.png) no-repeat 50%;
}

然而,我正在寻找一种更优雅的解决方案(纯 CSS 或 SVG 渐变?),而不使用背景图像,使渐变可以进行无像素缩放。

我已经做了研究,最接近的解决方案是https://codepen.io/nordstromdesign/pen/QNrBRMPossible to use border-radius together with a border-image which has a gradient?,但我需要一种中心透明的解决方案以便显示页面的背景。

更新:理想情况下,我正在寻找在所有现代浏览器中具有相对良好支持的解决方案。

3个回答

8

SVG 是创建圆形和绘制渐变边框的推荐方式。

SVG 具有一个 circle 元素,可用于绘制圆形。此形状可以使用纯色、渐变或图案进行填充和轮廓。

* {box-sizing: border-box;}

body {
  background: linear-gradient(#333, #999);
  text-align: center;
  min-height: 100vh;
  padding-top: 10px;
  margin: 0;
}
svg {vertical-align: top;}
<svg width="210" height="210">
  <defs>
    <linearGradient id="grad1" x1="0" y1="1" x2="1" y2="0">
      <stop offset="0" stop-color="#f5d700" />
      <stop offset="1" stop-color="#0065da" />
    </linearGradient>
    <linearGradient id="grad2" xlink:href="#grad1" x1="1" y1="0" x2="0" y2="1"></linearGradient>
  </defs>
  <g fill="none">
    <circle cx="100" cy="100" r="95" stroke="url(#grad1)" stroke-width="2" />
    <circle cx="100" cy="100" r="40" stroke="url(#grad2)" stroke-width="5" />
  </g>
</svg>


0
这是一个仅使用 CSS 的解决方案,在所有现代浏览器中都应该正常工作(已在 Chrome、Firefox 和 Edge 上测试)。

.box {
  --it:20px; /* thickness of inner gradient */
  --ot:10px; /* thickness of outer gradient */
  --s:30%;   /* starting point of inner gradient */

  width:200px;
  display:inline-flex;
  box-sizing:border-box;
  border-radius:50%;
  border:var(--ot) solid transparent;
  background:
      /* inner gradient clipped to the padding area */
      conic-gradient(red,blue,green,red) padding-box,
      /* outer gradient visible on the border area */
      conic-gradient(purple,yellow,orange,purple) border-box;
      
  -webkit-mask:radial-gradient(farthest-side,
    transparent var(--s), 
    #fff   calc(var(--s) + 1px)  
           calc(var(--s) + var(--it)),
    #fff0  calc(var(--s) + var(--it) + 1px) 
           calc(100% - var(--ot)), 
    #fff   calc(100% - var(--ot) + 1px));
}
/* keep the ratio */
.box::before {
  content:"";
  padding-top:100%;
}

body {
  background:pink;
}
<div class="box"></div>
<div class="box" style="--s:5%;--ot:20px;width:150px;"></div>
<div class="box" style="--s:calc(100% - 20px);--it:10px;width:220px;"></div>
<div class="box" style="--s:0%;--it:50%;width:80px;"></div>

我在计算中添加了1px以避免锯齿状边缘。您可以将conic-gradient()替换为其他类型的渐变甚至是图像。


0

您可以使用掩码来实现您想要的效果。您需要一个带有透明圆的SVG文件。这里我使用了从互联网上下载的图像,但您可以根据自己的需求制作自己的图像:

mask: url(circle.svg);

谢谢,我会研究一下。虽然目前对这个功能的支持非常弱:http://caniuse.com/#search=mask - user1275105

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接