[英]How to make a doughnut segment using only CSS
How to make this shape use only css如何使这个形状只使用 css
What I have tried:我尝试过的:
.button-up { border-top: 100px solid red; border-left: 50px solid transparent; border-right: 50px solid transparent; border-top-left-radius: 50%; border-top-right-radius: 50%; border-bottom: 35px solid transparent; width: 200px; }
<div class="button-up"></div>
I would go with some linear/radial-gradient
like this:我会像这样使用一些
linear/radial-gradient
:
.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%); }
<div class="box"> </div>
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)); }
<div class="box"> </div>
You can also consider SVG which can be easier:您还可以考虑使用 SVG 更简单:
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64' width='300' height='300' fill='blue'> <path stroke="green" stroke-width=1 d='M24 32 C28 28 37 28 40 32 L52 22 C38 8 26 8 12 22 Z' /> </svg>
Here is another Idea with clip-path
:这是带有
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); }
<div class="box"> </div>
Related question: CSS Only Pie Chart - How to add spacing/padding between slices?相关问题: CSS Only Pie Chart - 如何在切片之间添加间距/填充?
In case you prefer your border method more than SVG or gradients, here is an approach using just border-radius
, the pseudo element ::after
and some positioning :如果你更喜欢你的边框方法而不是 SVG 或渐变,这里是一种只使用
border-radius
、伪元素::after
和一些定位的方法:
.shape, .shape::after { position: absolute; border-radius: 100%; } .shape { height: 200px; width: 200px; border: 100px solid transparent; border-top: 100px solid #375c89; } .shape::after { content: ''; height: 190px; width: 198px; top: -95px; left: -89px; border: 90px solid transparent; border-top: 90px solid #4f81bc; }
<div class="shape"></div>
You can create this shape in two steps.您可以分两步创建此形状。
Step 1 : Create a doughnut segment using border-radius: 100%
to give it a circle like shape.第 1 步:使用
border-radius: 100%
创建一个甜甜圈段,使其具有类似圆形的形状。 Then apply a color only to the top border and make the other borders transparent.然后仅将颜色应用于顶部边框并使其他边框透明。 This way you get a circle segment.
这样你就得到一个圆段。 Now give your element a
width
and a height
greater than 0 to transform the circle into a doughnut.现在给你的元素一个大于 0 的
width
和height
,将圆圈转换成一个甜甜圈。
Step 2 : Apply the same styles to the pseudo element ::after
but giving it a slightly less width
, height
and border width.第 2 步:对伪元素
::after
应用相同的样式,但稍微减少width
、 height
和边框宽度。 Adjust the values for your needs.根据您的需要调整值。 Now position both elements with
position: absolute
to adjust the position of the overlapping pseudo element to center it properly over the main element.现在使用
position: absolute
来定位两个元素以调整重叠伪元素的位置,使其在主元素上正确居中。
clip-path
or SVG .clip-path
或SVG更好的浏览器兼容性,尤其是对于旧浏览器。height
and width
height
和width
overflow: hidden;
overflow: hidden;
::after
element::after
元素You can as well use both pseudo elements ::before
and ::after
to create the shape and easily adjust the position with sizing and margin (thanks to Temani for pointing this out in the comments):您也可以同时使用伪元素
::before
和::after
来创建形状,并使用大小和边距轻松调整位置(感谢 Temani 在评论中指出这一点):
.shape { position: relative; width: 400px; height: 400px; } .shape::before, .shape::after { content: ''; position: absolute; border-radius: 100%; right: 0; left: 0; bottom: 0; top: 0; } .shape:before { border: 100px solid transparent; border-top-color: #375c89; } .shape::after { margin: 5px 12px; border: 90px solid transparent; border-top-color: #4f81bc; height: 45%; }
<div class="shape"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.