Does anybody know how to create this in CSS or if it's even possible. I know how to make quarter circles but I am not sure how to apply it in this format. Small chunks of the border need to be in different colors.
相关问题
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
- jQuery hover to slide?
- Issue with star rating css
Using CSS:
It is definitely possible to achieve with CSS (as already shown in Quantastical's answer and here) but is CSS really the right tool to do this? My answer would be NO. The reason is because creating such shapes/effects using CSS is very tough in itself and in-addition to it, they come with some restrictions. For example, the below snippet works only when the background is a solid color. Clip Path example is not supported in IE completely and in FF it works only with inline SVG.
If you still wish to proceed doing it using CSS then the below is another alternate. Here, we make use of
skew
transforms on 4 elements (real or pseudos) all of which are 50% x 50% of their parent's size. Theskew
transforms produces the sector like appearance and thus looks more realistic. Background color assigned to these skewed elements is overlayed on top of another element which has full border and it looks as though part of the border is differently colored. Finally, we add anotherdiv
with white background on top of all these to mask the inner portions of the circle (so that only border is visible).Using SVG:
Because of all the aforementioned reasons, I would recommend you to use SVG for this. SVG makes creation of such shapes/effects very easy, the code is easily understandable, they are responsive by nature.
My SVG skills aren't great and it is very much possible that the no. of elements could be reduced. The below snippet is only a sample to illustrate what is possible.
Here, we use 5
circle
elements (1 for the gray border and 1 each for the colors). The#gray
circle has a full gray border whereas all the other circles only have partial border (in the required color). The partial borders are produced usingstroke-dasharray
andstroke-dashoffset
.The
stroke-dasharray
property is used to produce dashed borders by giving the length of the stroke (color) and the length of the dash (transparent) as values. For this case, the length of the dash should be equal to the circle's circumference (2 * PI * r) whereas for length of the stroke, I have used 1/8th of the circumference of the value.The
stroke-dashoffset
property is used to specify the offset from where the stroke should start. The offset is calculated from the 0deg position (which is the point represented by (100%, 50%)). By setting the appropriate offset values, the required effect can be produced.You have already good answers.
Just to give you another way to get this result, you can do it with multiple backgrounds. The good news about this approach is that you only need a div for it.
The sectors can be get with an inclined linear gradient, limited to one quarter of the space available. - we need 4 of these, changing the position and the angle.
Over those, another gradint, set fully to white, will hide the center.
And changing the background-origin and clip to border-box or content-box makes the colors use the space reserved for the border.
Note that this solution will work for any border / border-radius combination
If you really want to do this with CSS, you could potentially use a clipping mask to get the effect you're after. This approach is subject to browser compatibility, though, so I don't know how useful it would be, unless you're in a closed environment.
It's not perfect, either. The clipping path should probably be a polygon to ensure the segment edges are pointed toward the center of the circle instead of in-line with bounding box.