答案:CSS中通过border-radius: 50%将正方形元素变为圆形,矩形元素变为椭圆,因水平和垂直圆角半径分别为宽高的一半,形成闭合曲线。

要在CSS里画圆形或椭圆,最直接也最常用的方法就是利用
border-radius
div
width
height
border-radius
width
height
border-radius
50%
width
height
border-radius: 50%
border-radius
说实话,每次提到CSS画圆,我第一个想到的就是
border-radius
绘制圆形:
要画一个完美的圆形,你需要两个条件:
立即学习“前端免费学习笔记(深入)”;
width
height
border-radius
50%
举个例子:
.circle {
width: 100px;
height: 100px; /* 宽高相等,确保是正方形 */
background-color: #3498db; /* 给个背景色方便看 */
border-radius: 50%; /* 关键一步,让它变成圆形 */
}<div class="circle"></div>
这样,你就会得到一个直径为100px的蓝色圆形。这个
50%
绘制椭圆:
画椭圆其实和画圆形差不多,唯一的区别就是元素不再是正方形,而是矩形。
width
height
border-radius
50%
看这个例子:
.ellipse {
width: 150px; /* 宽度 */
height: 80px; /* 高度,不等于宽度 */
background-color: #2ecc71;
border-radius: 50%; /* 同样是50% */
}<div class="ellipse"></div>
这时,
border-radius: 50%
width
height
width
height
当然,
border-radius
border-radius: 10px / 20px;
50%
border-radius: 50%
这个问题其实挺有意思的,它涉及到
border-radius
border-radius
width
height
所以,当你写下
border-radius: 50%;
width
50%
height
50%
现在,我们考虑一个正方形元素,比如
width: 100px; height: 100px;
100px * 50% = 50px
100px * 50% = 50px
这意味着,每个角的圆弧都会以
50px
50%
如果是矩形,比如
width: 150px; height: 80px;
150px * 50% = 75px
80px * 50% = 40px
由于水平和垂直方向的半径不相等,形成的自然就是椭圆了。这个机制理解起来不复杂,但它正是
border-radius
border-radius
border-radius
1. 单独控制每个角的圆角:
border-radius
border-radius: 10px;
border-radius: 10px 20px;
border-radius: 10px 20px 30px;
border-radius: 10px 20px 30px 40px;
通过这种方式,你可以轻松做出类似“药丸”形状(两个相对的角完全圆滑,另外两个保持直角),或者其他不对称的圆角。
.asymmetrical-round {
width: 150px;
height: 100px;
background-color: #f39c12;
border-radius: 30px 0 50px 10px; /* 左上30px, 右上0, 右下50px, 左下10px */
}2. 使用斜杠(/
这是
border-radius
border-radius: [水平半径值] / [垂直半径值];
例如:
border-radius: 50% / 10%;
border-radius: 10px 20px 30px 40px / 5px 15px 25px 35px;
.blob-shape {
width: 200px;
height: 120px;
background-color: #9b59b6;
border-radius: 60% 40% 70% 30% / 30% 60% 40% 70%; /* 这就能画出各种不规则的“水滴”或“云朵”状 */
}这种斜杠语法特别适合需要更精细控制圆角曲率的场景,比如模拟一些有机形状或者UI中特殊的按钮样式。通过组合不同的百分比值,你几乎可以绘制出任何你想要的圆角边缘。我个人在设计一些卡片或按钮时,就喜欢用这种方式来打破常规的矩形,让界面看起来更活泼。
border-radius
虽然
border-radius
1. clip-path
clip-path
实现方式:
clip-path: circle(50% at 50% 50%);
clip-path: ellipse(60px 40px at 50% 50%);
优点:
clip-path
transform
缺点:
border-radius
clip-path
border-radius
2. transform
scale
这种方法通常是先创建一个圆形(用
border-radius: 50%
transform: scaleX()
scaleY()
实现方式:
.transformed-ellipse {
width: 100px;
height: 100px; /* 先是正方形 */
background-color: #e74c3c;
border-radius: 50%; /* 变成圆形 */
transform: scaleX(1.5); /* 水平方向拉伸1.5倍,变成椭圆 */
}优点:
transform
transform
缺点:
transform
3. SVG (Scalable Vector Graphics):
对于更复杂或需要极高精度的圆形和椭圆,SVG无疑是最佳选择。你可以直接在HTML中嵌入SVG代码。
实现方式:
<svg width="100" height="100"> <circle cx="50" cy="50" r="40" fill="#f1c40f" /> </svg> <svg width="150" height="80"> <ellipse cx="75" cy="40" rx="70" ry="35" fill="#1abc9c" /> </svg>
优点:
缺点:
div
总结一下我的看法:
对于绝大多数简单的圆形和椭圆需求,
border-radius
clip-path
以上就是CSS圆形怎么画_CSS绘制圆形与椭圆形状方法教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号