How to make a doughnut segment using only CSS

前端 未结 2 1070
天涯浪人
天涯浪人 2020-12-19 11:42

How to make this shape use only css

\"doughnut

What I have tried:

2条回答
  •  情书的邮戳
    2020-12-19 12:03

    I would go with some linear/radial-gradient like this:

    .box {
      width:200px;
      height:200px;
      border-radius:50%;
      background:
       linear-gradient(-30deg, white 50%,transparent 50.5%),
       linear-gradient(30deg,  white 50%,transparent 50.5%),
       radial-gradient(farthest-side at center,transparent 40%,blue 41%);
    }

    And with border:

    .box {
      width:200px;
      height:200px;
      border-radius:50%;
      background:
       linear-gradient(to top,white 58.5%,transparent 60%),
       linear-gradient(-30deg, white calc(50% - 4px),green calc(50% - 4px) 50%,transparent 0),
       linear-gradient(30deg,  white calc(50% - 4px),green calc(50% - 4px) 50%,transparent 0),
       radial-gradient(farthest-side at center,
        transparent calc(42% - 5px),
        green calc(42% - 4px) 42%,
        blue 42% calc(100% - 4px),green calc(100% - 3px));
    }

    You can also consider SVG which can be easier:

    
      
    

    Here is another Idea with clip-path:

    .box {
      width:200px;
      height:200px;
      border-radius:50%;
      background:
      radial-gradient(farthest-side at center,transparent 40%,blue 41%);
      clip-path: polygon(0 0,0 10%, 50% 50%, 100% 10%,100% 0);
    }

    Related question: CSS Only Pie Chart - How to add spacing/padding between slices?

提交回复
热议问题