【问题标题】:Circle with three different border colors具有三种不同边框颜色的圆圈
【发布时间】:2016-09-09 10:50:46
【问题描述】:

我有一个带有一种边框颜色的圆圈的 CSS:

.circle {
  border: 6px solid #ffd511;
  border-radius: 30px;
  -moz-border-radius: 30px;
  -webkit-border-radius: 30px;
  -khtml-border-radius: 30px;
  width: 30px;
  height: 18px;
  line-height: 20px;
  padding: 12px 6px;
  text-align: center;
}
<div class="circle">17</div>

看起来像这样:

我应该如何将 CSS 更改为具有三种边框颜色 - 就像时钟一样:

  • 从0到4色#1
  • 从4到8色#2
  • 从8到12色#3

我敢肯定,它有可能使用元素 <canvas>,但我没有成功。

【问题讨论】:

  • 一旦问题有了答案,您就不应更改它,使这些答案看起来不完整或无效。如果需要,请提出另一个问题。我已将问题回滚到添加了答案的地步。

标签: css svg border css-shapes


【解决方案1】:

用css试试这个Pseudo-elements 三种不同的边框颜色(相同的长度)

* {
  box-sizing: border-box
}
body {
  margin: 0;
  padding-top: 20px;
  background: skyblue;
  transition: background 0.6s ease
}
body:hover {
  background: #CBE0E8
}
div {
  box-shadow: inset 0 0 3px 0px #484848, 0 0 6px 0px #484848;
  width: 150px;
  height: 150px;
  margin: 0 auto;
  border-radius: 50%;
  border-top: 10px solid green;
  border-right: 10px solid red;
  border-bottom: 10px solid red;
  border-left: 10px solid green;
  transform: rotate(15deg);
  position: relative
}
div:before, div:after {
  content: "";
  position: absolute;
  left: -10px;
  top: -10px;
  width: 100%;
  height: 100%;
  border-radius: 50%;
}

div:before {
  border-top: 10px solid yellow;
  border-right: 10px solid transparent;
  border-bottom: 10px solid transparent;
  border-left: 10px solid transparent;
  transform: rotate(60deg)
}
div:after {
  border-top: 10px solid yellow;
  border-right: 10px solid transparent;
  border-bottom: 10px solid transparent;
  border-left: 10px solid transparent;
  transform: rotate(30deg)
}
<div></div>

【讨论】:

  • 谢谢,这也是解决办法。
【解决方案2】:

您可以使用inline svg 实现圆形边框分为3 部分

这是一个例子:

svg{width:30%;height:auto;}
<svg viewbox="0 0 10 10">
  <defs>
    <circle id="circle" cx="5" cy="5" r="4" stroke-width="0.5" fill="transparent" />
  </defs>
  <use xlink:href="#circle" stroke="pink" stroke-dasharray="0,2.09,8.38,30" />
  <use xlink:href="#circle" stroke="green" stroke-dasharray="0,10.47,8.38,30" />
  <use xlink:href="#circle" stroke="orange" stroke-dasharray="2.09,16.75,6.3" />
</svg>

编辑

要在圆圈内添加文字,您可以使用svg text element

svg{width:30%;height:auto;}
<svg viewbox="0 0 10 10">
  <defs>
    <circle id="circle" cx="5" cy="5" r="4" stroke-width="0.5" fill="transparent" />
  </defs>
  <use xlink:href="#circle" stroke="pink" stroke-dasharray="0,2.09,8.38,30" />
  <use xlink:href="#circle" stroke="green" stroke-dasharray="0,10.47,8.38,30" />
  <use xlink:href="#circle" stroke="orange" stroke-dasharray="2.09,16.75,6.3" />
  <text x="5" y="6.5" text-anchor="middle" font-size="5">17</text>
</svg>

【讨论】:

  • 谢谢你,再问一个问题,我怎么能在一些文本里面,就像你在我编辑的问题中看到的那样?
  • 我正在尝试实现你的代码,但我做不到;圆圈元素是 Google Maps API v3 中显示的标记。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-30
  • 1970-01-01
  • 1970-01-01
  • 2019-11-17
  • 2013-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多