目录实心圆圆环(空心圆)半圆四分之一圆(扇形)心形吃豆人鸡蛋椭圆胶囊叶子(花瓣)五叶花瓣四叶草三角形平行四边形梯形边框对话框太极八卦阵图月亮立体球形
我们在编写前端代码时,经常会遇到各种各样的形状图形(如:边框对话框,三角形,平行四边形、圆角边框、圆形、四叶草、花瓣等),除了用背景图片(css雪碧图或css精灵图+定位引用)和插入img图片的方法,我们还可以用css边框、圆角(border-radius)、渐变和定位的方法结合使用,绘制各种各样的形状图形。
实心圆
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116100103231-1859513694.png)
.circle {
width: 120px;
height: 120px;
background: #317ef3;
border-radius: 100%;
}
圆环(空心圆)
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116100116756-1415258225.png)
.ring {
width: 120px;
height: 120px;
border: 10px solid #317ef3;
border-radius: 100%;
box-sizing: border-box;
}
半圆
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116100135020-468188565.png)
//上半圆
.top-semicircle {
width: 120px;
height: 60px;
background: #317ef3;
border-radius: 60px 60px 0 0;
/*顺时针方向,四个值依次分别表示左上、右上、右下、左下*/
}
//右半圆
.rt-semicircle {
width: 60px;
height: 120px;
background: #317ef3;
border-radius: 0 60px 60px 0;
}
//左半圆
.lf-semicircle {
width: 60px;
height: 120px;
background: #317ef3;
border-radius: 60px 0 0 60px;
}
//下半圆
.bot-semicircle {
width: 120px;
height: 60px;
background: #317ef3;
border-radius: 0 0 60px 60px;
}
四分之一圆(扇形)
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116100152273-1831042581.png)
.toplf-sector,
.toprt-sector,
.botlf-sector,
.botrt-sector {
width: 60px;
height: 60px;
background: #317ef3;
}
.toprt-sector {
border-radius: 60px 0 0 0;
}
.toplf-sector {
border-radius: 0 60px 0 0;
}
.botlf-sector {
border-radius: 0 0 60px 0;
}
.botrt-sector {
border-radius: 0 0 0 60px;
}
心形
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116100204422-578025979.png)
.love {
width: 100px;
height: 100px;
background: #317ef3;
position: relative;
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-o-transform: rotate(45deg);
}
.love::before,.love:after{
content: "";
background: #317ef3;
position: absolute;
}
.love:after {
width: 50px;
height: 100px;
background: #317ef3;
border-radius: 50px 0 0 50px;
right: 99px;
top: 0;
}
.love::before {
width: 100px;
height: 50px;
border-radius: 50px 50px 0 0;
bottom: 99px;
left: 0;
}
原理图,奉上
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116100236062-230541311.png)
吃豆人
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116100259456-1079671937.png)
.eat {
width: 0;
height: 0;
border: 60px solid #317ef3;
border-radius: 100%;
border-right-color: transparent;
}
值得注意的是如果右边框的颜色设置为白色的话,右边会有一条蓝色线条
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116100338748-322195674.png)
鸡蛋
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116100403716-1715014197.png)
.egg {
width: 100px;
height: 150px;
background: #317ef3;
border-radius: 50px 50px 50px 50px/90px 90px 60px 60px;
}
原理图奉上:
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116100445703-1770779496.png)
椭圆
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116100509488-193305815.png)
.ellipse1 {
width: 180px;
height: 120px;
background: #317ef3;
border-radius: 100%;
/* border-radius: 90px/60px; */
}
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116100540695-1974621353.png)
.ellipse2 {
width: 120px;
height: 180px;
background: #317ef3;
border-radius: 100%;
/* border-radius: 60px/90px; */
}
胶囊
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116100624914-533754944.png)
.capsule1 {
width: 200px;
height: 100px;
background: #317ef3;
border-radius: 50px;
}
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116100655183-708016212.png)
.capsule2 {
width: 100px;
height: 200px;
background: #317ef3;
border-radius: 50px;
}
叶子(花瓣)
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116142203795-1078585944.png)
.leaf1 {
width: 100px;
height: 100px;
background: #317ef3;
border-radius: 0 100px;
}
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116142238052-931524399.png)
.leaf2 {
width: 100px;
height: 100px;
background: #317ef3;
border-radius: 80px 0px;
}
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116142303627-1143870666.png)
.leaf3 {
width: 100px;
height: 100px;
background: #317ef3;
border-radius: 50px 50px 0 50px;
}
五叶花瓣
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116142333440-170335576.png)
.five-petal {
width: 160px;
height: 160px;
}
.five-petal span {
display: block;
width: 80px;
height: 80px;
background: #317ef3;
border-radius: 0 100px;
transform-origin: 100% 100%;
position: absolute;
}
.five-petal .petal2 {
transform: rotate(72deg);
}
.five-petal .petal3 {
transform: rotate(144deg);
}
.five-petal .petal4 {
transform: rotate(216deg);
}
.five-petal .petal5 {
transform: rotate(288deg);
}
四叶草
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116142450121-1580361082.png)
.four-leaf {
width: 210px;
height: 210px;
position: relative;
}
.four-leaf span {
display: block;
width: 100px;
height: 100px;
background: #317ef3;
position: absolute;
}
.four-leaf span.leaf1 {
left: 0;
top: 0;
border-radius: 50px 50px 0 50px;
}
.four-leaf span.leaf2 {
right: 0;
top: 0;
border-radius: 50px 50px 50px 0;
}
.four-leaf span.leaf3 {
left: 0;
bottom: 0;
border-radius: 50px 0 50px 50px;
}
.four-leaf span.leaf4 {
right: 0;
bottom: 0;
border-radius: 0 50px 50px 50px;
}
三角形
实现原理:
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116141038401-642899799.png)
.triangle-shiyi {
width: 0;
height: 0;
border: 50px solid #317ef3;
border-left-color: red;
border-top-color: aquamarine;
border-right-color: bisque;
}
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116141131177-30107340.png)
.dbzj-sanjiao1 {
width: 0;
height: 0;
border-bottom: 100px solid #317ef3;
border-right: 100px solid transparent;
}
.dbzj-sanjiao2 {
width: 0;
height: 0;
border-bottom: 100px solid #317ef3;
border-left: 100px solid transparent;
}
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116141207465-1695977530.png)
.dbzj-sanjiao3 {
width: 0;
height: 0;
border-top: 100px solid #317ef3;
border-right: 100px solid transparent;
}
.dbzj-sanjiao4 {
width: 0;
height: 0;
border-top: 100px solid #317ef3;
border-left: 100px solid transparent;
}
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116141250869-2134095246.png)
.dbzj-sanjiao5 {
width: 0;
height: 0;
border-bottom: 50px solid #317ef3;
border-left: 100px solid transparent;
/* border-left、border-right决定宽度 */
/* border-top、border-bottom决定高度 */
}
.dbzj-sanjiao6 {
width: 0;
height: 0;
border-bottom: 50px solid #317ef3;
border-right: 100px solid transparent;
}
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116141327202-311925553.png)
.dbzj-sanjiao7 {
width: 0;
height: 0;
border-bottom: 100px solid #317ef3;
border-right: 50px solid transparent;
}
.dbzj-sanjiao8 {
width: 0;
height: 0;
border-bottom: 100px solid #317ef3;
border-left: 50px solid transparent;
}
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116141357922-1628750086.png)
.dbzj-sanjiao9 {
width: 0;
height: 0;
border-top: 50px solid #317ef3;
border-right: 100px solid transparent;
}
.dbzj-sanjiao10 {
width: 0;
height: 0;
border-top: 50px solid #317ef3;
border-left: 100px solid transparent;
}
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116141449394-226722640.png)
.dbzj-sanjiao11 {
width: 0;
height: 0;
border-left: 40px solid transparent;
border-right: 40px solid transparent;
border-bottom: 100px solid #317ef3;
/*三角形的宽度w=left+right,h=bottom*/
}
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116141518923-542900188.png)
.dbzj-sanjiao12 {
width: 0;
height: 0;
border-bottom: 50px solid #317ef3;
border-right: 100px solid transparent;
position: relative;
}
.dbzj-sanjiao12::before {
content: "";
position: absolute;
left: 0;
top: 0;
border-bottom: 50px solid #fff;
border-right: 20px solid transparent;
}
平行四边形
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116141603091-1574546226.png)
.pxsbx1 {
width: 200px;
height: 120px;
background: #317ef3;
color: #fff;
text-align: center;
line-height: 120px;
font-size: 18px;
transform: skewX(30deg);
}
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116141638093-507718784.png)
.pxsbx2 {
width: 200px;
height: 120px;
text-align: center;
line-height: 120px;
position: relative;
font-size: 18px;
color: #fff;
}
.pxsbx2::after {
content: "";
position: absolute;
left: 0;
top: 0;
z-index: -1;
width: 200px;
height: 120px;
background: #317ef3;
transform: skewX(30deg);
}
与上面的平行四边形相比,可以看到该图形中的文字不会发生倾斜,方法是在文字层后添加一个伪元素绘制平行四边。
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116141727147-6263280.png)
.pxsbx3 {
width: 100px;
height: 160px;
background: #317ef3;
transform: skewY(30deg);
}
.pxsbx4 {
width: 100px;
height: 160px;
background: #317ef3;
transform: skewY(-30deg);
}
梯形
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116141804809-2137440781.png)
.tixing1 {
width: 100px;
height: 0;
border-bottom: 100px solid #317ef3;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
}
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116141831404-702410855.png)
.tixing2 {
width: 100px;
height: 100px;
background: #317ef3;
position: relative;
}
.tixing2::before {
content: "";
width: 0;
height: 0;
border-bottom: 100px solid #317ef3;
border-right: 50px solid transparent;
position: absolute;
left: 100%;
top: 0;
}
边框对话框
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116141936878-268343715.png)
.duihuakuang1 {
width: 200px;
height: 120px;
border: 2px solid #317ef3;
position: relative;
}
.duihuakuang1::before {
content: "";
position: absolute;
left: 40px;
top: 92%;
width: 20px;
height: 20px;
border-right: 2px solid #317ef3;
border-bottom: 2px solid #317ef3;
transform: rotate(45deg);
background: #fff;
}
太极八卦阵图
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116101203184-1443138779.png)
#yin-yang {
width: 96px;
height: 48px;
background: #eee;
border-color: red;
border-style: solid;
border-width: 2px 2px 50px 2px;
border-radius: 100%;
position: relative;
}
#yin-yang:before {
content: "";
position: absolute;
top: 50%;
left: 0;
background: #eee;
border: 18px solid red;
border-radius: 100%;
width: 12px;
height: 12px;
}
#yin-yang:after {
content: "";
position: absolute;
top: 50%;
left: 50%;
background: red;
border: 18px solid #eee;
border-radius:100%;
width: 12px;
height: 12px;
}
月亮
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116101425568-784128722.png)
#moon {
width: 80px;
height: 80px;
border-radius: 50%;
box-shadow: 15px 15px 0 0 red;
}
立体球形
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116142017815-425963795.png)
.litiqiuxing1 {
width: 100px;
height: 100px;
border-radius: 100%;
background: #317ef3;
background: radial-gradient(circle at 25% 35%, #fff, #317ef3, #071529);
}
![](https://img2020.cnblogs.com/blog/1287814/202101/1287814-20210116142046672-2055521614.png)
.litiqiuxing2 {
width: 100px;
height: 100px;
border-radius: 100%;
background: #317ef3;
background: radial-gradient(circle at top, #fff, #317ef3, #071529)
}
参考:
(用CSS绘制最常见的40种形状和图形)[https://www.cnblogs.com/zs-xanadu/p/9229242.html]
|