绘制扁圆和椭圆的核心是利用CSS的border-radius、clip-path、transform及SVG等技术,通过调整宽高比和半径值实现不同形状。1. 使用border-radius: 50%可将不等宽高的元素变为椭圆;2. 胶囊形状可通过border-radius设为短边一半或50%实现;3. 斜杠语法如border-radius: 100px / 50px可精细控制各角弧度;4. clip-path: ellipse()支持更灵活的椭圆裁剪;5. transform可拉伸正圆成椭圆;6. SVG提供高精度矢量椭圆;7. 响应式设计推荐使用百分比、vw/vh单位结合aspect-ratio属性保持宽高比。

CSS绘制扁圆和椭圆的核心技巧在于巧妙地结合
width
height
border-radius
border-radius: 50%
border-radius
要绘制扁圆和椭圆,我们主要依赖CSS的
border-radius
1. 绘制基本椭圆: 最直接的方法就是给一个矩形元素设置
border-radius: 50%
div
<div class="ellipse"></div>
对应的CSS:
.ellipse {
width: 200px; /* 宽度 */
height: 100px; /* 高度 */
background-color: #3498db; /* 填充颜色 */
border-radius: 50%; /* 关键:设置为50% */
}这里,
width
height
border-radius: 50%
width
height
立即学习“前端免费学习笔记(深入)”;
2. 绘制“扁圆”(胶囊/药丸形状): “扁圆”在很多UI设计中常指那种两端是半圆形、中间是矩形的胶囊状或药丸状。实现这种效果,我们需要确保
border-radius
例如,如果你想创建一个宽度是高度两倍的胶囊:
<div class="pill-shape"></div>
.pill-shape {
width: 200px;
height: 50px; /* 高度是宽度的一半 */
background-color: #2ecc71;
border-radius: 25px; /* 关键:等于高度的一半 (50px / 2 = 25px) */
/* 也可以直接写 border-radius: 50px; 达到同样效果,因为当 radius 大于高度的一半时,会自动取高度的一半 */
/* 更简洁的写法是 border-radius: 50%; 同样能实现,因为当 width > height 时,50% 会让短边完全圆弧化 */
}这里,
border-radius: 25px
border-radius: 50%
3. 使用border-radius
border-radius
/
border-radius: 水平半径 / 垂直半径;
.custom-oval {
width: 200px;
height: 100px;
background-color: #f39c12;
border-radius: 100px / 50px; /* 水平半径100px,垂直半径50px */
}这个例子中,
border-radius: 100px / 50px;
border-radius: 50%;
.asymmetrical-oval {
width: 200px;
height: 100px;
background-color: #e74c3c;
border-top-left-radius: 100px 50px; /* 左上角水平100px,垂直50px */
border-top-right-radius: 50px 100px; /* 右上角水平50px,垂直100px */
border-bottom-right-radius: 100px 50px; /* 右下角水平100px,垂直50px */
border-bottom-left-radius: 50px 100px; /* 左下角水平50px,垂直100px */
}这能让你绘制出非常独特、非对称的扁圆或异形。我觉得这才是
border-radius
border-radius
说实话,
border-radius
/
我们知道
border-radius
border-radius: 10px 20px 30px 40px;
当加入斜杠时,语法就变成了:
border-radius: [top-left-radius] [top-right-radius] [bottom-right-radius] [bottom-left-radius] / [top-left-radius] [top-right-radius] [bottom-right-radius] [bottom-left-radius];
斜杠前的四个值是水平半径,斜杠后的四个值是垂直半径。如果只提供一个值,它会应用于所有角;如果提供两个值,第一个用于左上和右下,第二个用于右上和左下,以此类推。
举个例子:绘制一个“水滴”形状的扁圆。 这需要一些想象力,但通过控制每个角的半径,我们可以模拟出一些不规则的形状。 假设我们想让底部圆润,顶部尖锐一点:
<div class="water-drop"></div>
.water-drop {
width: 100px;
height: 150px;
background-color: #3498db;
/* 底部完全圆弧,顶部几乎是直角或小圆角 */
border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
/* 解释:
水平半径:50% 50% 50% 50% (所有角水平半径都是50%)
垂直半径:60% (左上) 60% (右上) 40% (右下) 40% (左下)
这样,顶部两个角的垂直半径更大,配合水平半径,会让顶部显得更“收敛”或“尖”
底部两个角的垂直半径较小,会让底部显得更“饱满”
*/
/* 另一个更直观的尝试,让底部形成一个半圆,顶部收窄 */
/* border-radius: 50% 50% 50% 50% / 0 0 50% 50%; */
/* 这个会使得顶部直角,底部半圆 */
border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%; /* 调整后,更接近水滴 */
position: relative;
overflow: hidden; /* 防止内容溢出,如果里面有内容的话 */
}这种高级用法确实需要反复尝试和调整,才能得到理想的形状。它考验的不仅仅是CSS知识,还有一点点几何直觉。在我看来,这就像是用代码在画布上雕塑,挺有意思的。
border-radius
当然有!虽然
border-radius
1. clip-path
clip-path
circle()
ellipse()
polygon()
inset()
clip-path: ellipse()
<div class="clip-ellipse"></div>
.clip-ellipse {
width: 200px;
height: 100px;
background-color: #9b59b6;
clip-path: ellipse(50% 50% at 50% 50%); /* rx ry at cx cy */
/* 解释:
rx: 水平半径,50%表示元素宽度的一半
ry: 垂直半径,50%表示元素高度的一半
at cx cy: 椭圆中心点的位置,50% 50% 表示元素中心
*/
/* 绘制一个更扁的椭圆,比如rx是70%,ry是30% */
/* clip-path: ellipse(70% 30% at 50% 50%); */
}clip-path
border-radius
2. transform
transform: scaleX()
scaleY()
<div class="transform-ellipse"></div>
.transform-ellipse {
width: 100px;
height: 100px; /* 先画一个正圆 */
background-color: #1abc9c;
border-radius: 50%; /* 变成正圆 */
transform: scaleX(2); /* 水平方向拉伸2倍,变成一个扁平的椭圆 */
/* transform: scaleY(0.5); 垂直方向压缩,变成一个扁平的椭圆 */
transform-origin: center center; /* 确保从中心点拉伸 */
}这个方法的优点是简单直观,尤其适合需要动态拉伸椭圆的场景。但要注意,
transform
3. SVG(可伸缩矢量图形): 如果对形状的复杂度和精确度要求极高,或者需要绘制非常规的曲线,那么SVG是你的终极武器。SVG是基于XML的矢量图形格式,可以直接嵌入到HTML中。
<svg width="200" height="100"> <ellipse cx="100" cy="50" rx="90" ry="40" fill="#f1c40f" /> </svg>
SVG的
<ellipse>
cx
cy
rx
ry
在我看来,选择哪种方法,很大程度上取决于你的具体需求和对复杂度的接受程度。对于简单的扁圆,
border-radius
clip-path
光会画还不够,实际项目中总要考虑响应式不是?让扁圆在不同设备上都能看起来协调,这可不仅仅是把
width
height
1. 使用相对单位: 这是最基础也是最重要的一步。避免使用固定的
px
width
height
border-radius
%
.responsive-ellipse {
width: 80%; /* 占据父元素宽度的80% */
height: 40%; /* 占据父元素高度的40% */
border-radius: 50%;
/* 或者如果父元素高度不确定,可以基于宽度计算高度 */
/* padding-bottom: 40%; height: 0; */
}vw
vh
.responsive-pill {
width: 60vw; /* 视口宽度的60% */
height: 10vw; /* 视口宽度的10% (保持宽高比) */
border-radius: 5vw; /* 高度的一半 */
background-color: #e67e22;
}使用
vw
height
border-radius
2. 利用 aspect-ratio
padding-bottom
.aspect-ratio-ellipse {
width: 200px; /* 或百分比,或vw */
aspect-ratio: 2 / 1; /* 宽度是高度的两倍 */
background-color: #f39c12;
border-radius: 50%;
}结合
width
aspect-ratio
以上就是CSS怎么画扁圆_CSS绘制椭圆和扁圆形状技巧教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号