【问题标题】:How to make diagonal circle border gradient?如何制作对角圆边框渐变?
【发布时间】:2016-01-04 21:57:57
【问题描述】:

我对 CSS3 有疑问。我不知道如何制作这样的对角圆形渐变边框:

我发现了类似this:

.box {
  width: 250px;
  height: 250px;
  margin: auto;
  background: #eee;
  border: 20px solid transparent;
  -moz-border-image: -moz-linear-gradient(top left, #3acfd5 0%, #3a4ed5 100%);
  -webkit-border-image: -webkit-linear-gradient(top left, #3acfd5 0%, #3a4ed5 100%);
  border-image: linear-gradient(to bottom right, #3acfd5 0%, #3a4ed5 100%);
  border-image-slice: 1;
}
<div class="box"></div>

但不幸的是,这只适用于正方形。

任何帮助将不胜感激。

【问题讨论】:

  • 嗯……你想要一个锥形渐变,我想……他们还没来。我想我会在这里使用 SVG。
  • 呃。好的,谢谢,但这是唯一的方法吗?

标签: css gradient css-shapes linear-gradients


【解决方案1】:

锥形渐变是沿着围绕中心的圆弧进行的渐变。这就是我们在色轮中看到的。正如 Paulie_D 所指出的,这些目前无法通过 CSS 实现,但 Lea Verou has developed a polyfill for it。

话虽如此,您正在寻找的似乎不是锥形渐变,它是正常角度的线性渐变,但仅适用于边框。 这无法通过 CSS border-image 属性来实现,因为它是按照 specs 的方式工作的。

一个盒子的背景,而不是它的边框图像,被剪裁到适当的曲线


如果圆圈的中心部分是纯色,则可以使用 Vitorino 回答中提到的方法。如果它不是纯色(即页面背景是渐变或需要显示的图像),则无济于事。对于这种情况,可以使用以下方法。

使用蒙版图片:

这种方法使用圆形遮罩图像来遮盖圆形的内部。这使它看起来好像只有边框应用了渐变。 缺点是目前仅在支持 Webkit 的浏览器中支持此功能。

.border-gradient-mask {
  height: 200px;
  width: 200px;
  border-radius: 50%;
  background-image: linear-gradient(to bottom left, #7B73A4 0%, #150E5E 100%);
  -webkit-mask-image: radial-gradient(circle at center, transparent 57%, white 58%);
  mask-image: radial-gradient(circle at center, transparent 57%, white 58%);
}
body {
  background: radial-gradient(circle at center, sandybrown, chocolate);
}
<div class="border-gradient-mask"></div>

使用 SVG 形状或蒙版:

另一种方法是使用 SVG circle 元素创建圆,然后将渐变分配给 stroke 属性。渐变还应用了gradientTransform,因为这是使用 SVG 生成有角度的线性渐变的唯一方法。

.border-gradient-svg {
  position: relative;
  height: 200px;
  width: 200px;
  border-radius: 50%;
}
.border-gradient-svg svg {
  position: absolute;
  top: 0px;
  left: 0px;
  height: 100%;
  width: 100%;
}
.border-gradient-svg circle {
  fill: transparent;
  stroke: url(#grad);
  stroke-width: 8;
}
body {
  background: radial-gradient(circle at center, sandybrown, chocolate);
}
<div class="border-gradient-svg">
  <svg viewBox="0 0 100 100">
    <defs>
      <linearGradient id="grad" gradientUnits="objectBoundingBox" gradientTransform="rotate(135 0.5 0.5)">
        <stop offset="0%" stop-color="#7B73A4" />
        <stop offset="100%" stop-color="#150E5E" />
      </linearGradient>
    </defs>
    <circle r="46" cx="50" cy="50" />
  </svg>
</div>

同样可以通过使用 SVG mask 来实现。所需要的只是创建一个包含两个circle 元素的蒙版,用白色填充较大的圆圈,用黑色填充较小的圆圈,然后将蒙版应用于我们原来的circle 元素。小圆圈(黑色填充)占据的区域将是透明的。

.border-gradient-svg {
  position: relative;
  height: 200px;
  width: 200px;
  border-radius: 50%;
}
.border-gradient-svg svg {
  position: absolute;
  top: 0px;
  left: 0px;
  height: 100%;
  width: 100%;
}
.border-gradient-svg .grad-border {
  fill: url(#grad);
  mask: url(#masker);
}
body {
  background: radial-gradient(circle at center, sandybrown, chocolate);
}
<div class="border-gradient-svg">
  <svg viewBox="0 0 100 100">
    <defs>
      <linearGradient id="grad" gradientUnits="objectBoundingBox" gradientTransform="rotate(135 0.5 0.5)">
        <stop offset="0%" stop-color="#7B73A4" />
        <stop offset="100%" stop-color="#150E5E" />
      </linearGradient>
      <mask id="masker" x="0" y="0" width="100" height="100">
        <circle r="50" cx="50" cy="50" fill="#fff" />
        <circle r="42" cx="50" cy="50" fill="#000" />
      </mask>
    </defs>
    <circle r="50" cx="50" cy="50" class="grad-border"/>
  </svg>
</div>

使用剪辑路径:

创建它的另一种方法是使用clip-path(带有内联SVG)和clip-rule set to evenodd。 剪辑路径解决方案相对于其他解决方案的优势是,这只会在悬停在填充区域(而不是透明区域)时触发悬停效果。 缺点是 IE 不支持剪辑路径(即使是 SVG)。

.border-gradient-clip {
  height: 200px;
  width: 200px;
  border-radius: 50%;
  background-image: linear-gradient(to bottom left, #7B73A4 0%, #150E5E 100%);
  -webkit-clip-path: url(#clipper);
  clip-path: url(#clipper);
}
body {
  background: radial-gradient(circle at center, sandybrown, chocolate);
}
<svg width="0" height="0">
  <defs>
    <clipPath id="clipper" clipPathUnits="objectBoundingBox">
      <path d="M0,0.5 a0.5,0.5 0 1,0 1,0 a0.5,0.5 0 1,0 -1,0z M0.08,0.5 a0.42,0.42 0 1,0 0.84,0 a0.42,0.42 0 1,0 -0.84,0z" clip-rule="evenodd" />
    </clipPath>
  </defs>
</svg>
<div class="border-gradient-clip"></div>

【讨论】:

    【解决方案2】:

    你可以尝试这样的事情,我使用了一个带有 -ve z-index 的伪元素

    注意:背景不是透明的,因为我使用了background-color 作为内部元素

    .box {
      width: 250px;
      height: 250px;
      position: relative;
      margin: auto;
      margin: 30px;
      border-radius: 50%;
      background: #fff;
    }
    .box:after {
      content: '';
      position: absolute;
      top: -15px;
      bottom: -15px;
      right: -15px;
      left: -15px;
      background-image: linear-gradient(to bottom left, #7B73A4 0%, #150E5E 100%);
      z-index: -1;
      border-radius: inherit;
    }
    &lt;div class="box"&gt;&lt;/div&gt;

    【讨论】:

    • 透明背景而不是白色背景?
    猜你喜欢
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 2012-03-01
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    相关资源
    最近更新 更多