Segments in a circle using CSS3

后端 未结 4 1956
失恋的感觉
失恋的感觉 2020-11-27 04:06

I know you can make a circle in CSS3 using the border radius hack. But is there any way to make them have segments like this picture? Is there a way of doing this through HT

4条回答
  •  北海茫月
    2020-11-27 04:17

    You can use a conic gradient

    Conic gradients basically go around the shape, like a circle, from 0° to 360°.

    Here is a basic conic gradient, with a circle:

    div {
        width: 500px;
        height: 500px;
        border-radius: 9999px;
        background: red; /* fallback */
        background: conic-gradient(red, orange, yellow, green, blue, purple);
    }

    Using color stops, we can then, magically, turn it into segments:

    div {
        width: 500px;
        height: 500px;
        border-radius: 9999px;
        background: red; /* fallback */
        background: conic-gradient(red 10%, orange 10%, orange 30%, yellow 30%, yellow 50%, green 50%, green 60%, blue 60%, blue 70%, purple 70%);
    }

    Optionally, if we only want one slice, we can now change this so we only have one colour, and now we're good to go :)

    div {
        width: 500px;
        height: 500px;
        border-radius: 9999px;
        background: red; /* fallback */
        background: conic-gradient(#0000 40%, red 40%, red 70%, #0000 70%);
    }

提交回复
热议问题