css制作扇形
使用 CSS 制作扇形
CSS 可以通过 border-radius 和 clip-path 属性来实现扇形效果。以下是几种常见的方法:
方法一:使用 border-radius 和 transform
通过设置 border-radius 和旋转来实现扇形。

<div class="fan"></div>
.fan {
width: 100px;
height: 100px;
background-color: #ff6b6b;
border-radius: 100% 0 0 0;
transform: rotate(45deg);
}
方法二:使用 clip-path
clip-path 可以更灵活地裁剪元素形状,适合制作任意角度的扇形。
<div class="fan-clip"></div>
.fan-clip {
width: 100px;
height: 100px;
background-color: #4ecdc4;
clip-path: polygon(50% 50%, 100% 0, 100% 100%);
}
方法三:使用 SVG 和 CSS
结合 SVG 的 path 元素可以更精确地控制扇形形状。

<svg width="100" height="100" viewBox="0 0 100 100">
<path d="M50,50 L100,0 A50,50 0 0,1 100,100 Z" fill="#ffbe76" />
</svg>
方法四:使用 conic-gradient
conic-gradient 可以创建圆锥渐变,适合制作扇形色块。
<div class="fan-conic"></div>
.fan-conic {
width: 100px;
height: 100px;
background: conic-gradient(#54a0ff 0deg, #54a0ff 90deg, transparent 90deg);
border-radius: 50%;
}
方法五:使用伪元素
通过伪元素和旋转实现扇形。
<div class="fan-pseudo"></div>
.fan-pseudo {
width: 100px;
height: 100px;
position: relative;
overflow: hidden;
}
.fan-pseudo::before {
content: "";
position: absolute;
width: 100%;
height: 100%;
background-color: #5f27cd;
transform: rotate(45deg);
transform-origin: 0 0;
}
注意事项
clip-path的兼容性较好,但某些旧浏览器可能需要前缀。conic-gradient是较新的 CSS 特性,部分浏览器可能不支持。- SVG 方法适合需要复杂形状或动画的场景。
以上方法可以根据需求选择,clip-path 和 SVG 通常是实现扇形的推荐方案。






