css制作圆
使用 border-radius 属性制作圆
通过设置 border-radius 为 50%,可以将一个正方形元素变为圆形。元素的宽度和高度必须相等。
.circle {
width: 100px;
height: 100px;
border-radius: 50%;
background-color: #3498db;
}
使用 clip-path 制作圆
clip-path 可以通过 circle() 函数直接裁剪出圆形区域。
.circle {
width: 100px;
height: 100px;
clip-path: circle(50% at 50% 50%);
background-color: #e74c3c;
}
使用 SVG 制作圆
SVG 的 <circle> 元素可以精确绘制圆形,适合需要复杂图形或动画的场景。
<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;
}
使用 aspect-ratio 确保宽高一致
现代 CSS 的 aspect-ratio 属性可以强制元素的宽高比例,避免手动设置相同数值。

.circle {
width: 100px;
aspect-ratio: 1 / 1;
border-radius: 50%;
background-color: #9b59b6;
}





