5

I have this CSS for a circle with one border color:

.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>

It looks like this:

circle with one border color

How should I change the CSS to have three border colors - as on the clock:

  • from 0 to 4 color #1
  • from 4 to 8 color #2
  • from 8 to 12 color #3

I am sure, its possible, with element <canvas>, but I am not successful with doing that.

web-tiki
  • 99,765
  • 32
  • 217
  • 249
Legionar
  • 7,472
  • 2
  • 41
  • 70
  • 2
    http://stackoverflow.com/questions/34143736/css-circle-border-with-various-colors Here you can find the answer – Frutis May 13 '16 at 10:03
  • 1
    Once a question has answers you shouldn't change it in such a way as to make those answers look incomplete or invalid. Ask another question if you need to. I've rolled back the question to the point that the answers were added. – Robert Longson May 13 '16 at 10:36

2 Answers2

8

You can achieve a circle border divided into 3 sections with an inline svg using:

Here is an example:

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>

Edit

To add text inside the circle, you can use the 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>
web-tiki
  • 99,765
  • 32
  • 217
  • 249
  • Thank you, just one more question, how can I have inside some text, like you can see in my edited question? – Legionar May 13 '16 at 10:20
  • I am trying to implement your code, but I am unable; the circle element is a marker shown in Google Maps API v3. – Legionar May 13 '16 at 10:32
6

Try this with css Pseudo-elements three different border colors(same length)

* {
  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>
Gildas.Tambo
  • 22,173
  • 7
  • 50
  • 78