css制作圆形
使用 border-radius 属性
将元素的 border-radius 设置为 50%,可以将正方形或长方形元素变为圆形。元素的宽度和高度需相等,否则会变成椭圆形。
.circle {
width: 100px;
height: 100px;
border-radius: 50%;
background-color: #3498db;
}
使用 clip-path 属性
通过 clip-path 的 circle() 函数直接裁剪出圆形,适用于非正方形元素。
.circle {
width: 150px;
height: 100px;
clip-path: circle(50% at 50% 50%);
background-color: #e74c3c;
}
使用 SVG 绘制圆形
通过内联 SVG 创建圆形,适合需要复杂图形或动画的场景。
<svg width="100" height="100">
<circle cx="50" cy="50" r="50" fill="#2ecc71" />
</svg>
使用伪元素创建圆形
通过 ::before 或 ::after 伪元素生成圆形,适合装饰性元素。
.element::before {
content: "";
display: block;
width: 50px;
height: 50px;
border-radius: 50%;
background-color: #f39c12;
}
注意事项
- 确保
border-radius的父元素无overflow: hidden干扰。 clip-path的兼容性较差,需检查浏览器支持情况。- SVG 方案适合静态图形,CSS 方案更易与交互效果结合。






