用css制作半圆
使用 CSS 制作半圆的方法
方法 1:通过 border-radius 和固定宽高
通过设置元素的 border-radius 属性,结合固定的宽度和高度,可以创建半圆。以下是一个示例代码:
.half-circle {
width: 100px;
height: 50px;
background-color: #ff0000;
border-radius: 50px 50px 0 0;
}
width设置为100px,height设置为50px(即高度为宽度的一半)。border-radius设置为50px 50px 0 0,表示左上和右上角为圆角,左下和右下角为直角。
方法 2:使用 clip-path
通过 clip-path 属性可以直接裁剪出半圆形状,灵活性更高。示例代码如下:

.half-circle {
width: 100px;
height: 100px;
background-color: #00ff00;
clip-path: circle(50% at 50% 0);
}
width和height设置为相同的值(100px)。clip-path使用circle(50% at 50% 0),表示从顶部中心点裁剪出一个半圆。
方法 3:通过伪元素和 transform
利用伪元素和 transform 属性可以实现更复杂的半圆效果。示例代码如下:

.half-circle {
position: relative;
width: 100px;
height: 50px;
overflow: hidden;
}
.half-circle::after {
content: "";
position: absolute;
width: 100px;
height: 100px;
background-color: #0000ff;
border-radius: 50%;
bottom: 0;
}
- 父容器设置
overflow: hidden以隐藏超出部分。 - 伪元素
::after设置为完整的圆,并通过bottom: 0定位到容器底部,显示上半部分。
方法 4:通过 svg 嵌入
直接使用 svg 可以更精确地绘制半圆。示例代码如下:
<svg width="100" height="50" viewBox="0 0 100 50">
<path d="M 0,50 A 50,50 0 0 1 100,50 L 100,0 Z" fill="#ff00ff" />
</svg>
svg的width和height分别设置为100和50。path的d属性定义了半圆的路径,通过贝塞尔曲线绘制。
方法 5:通过 linear-gradient 和 border-radius
结合 linear-gradient 和 border-radius 可以实现渐变半圆效果。示例代码如下:
.half-circle {
width: 100px;
height: 50px;
background: linear-gradient(to bottom, #ff0000, #00ff00);
border-radius: 50px 50px 0 0;
}
background使用linear-gradient实现渐变效果。border-radius设置为50px 50px 0 0,形成半圆形状。
以上方法可以根据实际需求选择使用,每种方法都有其适用场景和灵活性。






