返回

你不知道的CSS图形绘制技巧

前端

纯CSS实现三角形、圆角三角形、圆形、正方形、多边形、箭头、气泡、爱心等图形,是前端面试中的常见题型之一,也是前端开发人员必备的技能之一。利用CSS实现图形的方式多种多样,通过调整border属性、transform属性、box-shadow属性等,即可实现各种各样的图形效果。

一、CSS三角形

  1. 利用border属性实现等边三角形:
.triangle {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid red;
}
  1. 利用transform属性实现旋转三角形:
.triangle {
  width: 100px;
  height: 100px;
  background-color: red;
  transform: rotate(45deg);
}

二、CSS圆角三角形

.rounded-triangle {
  width: 100px;
  height: 100px;
  background-color: red;
  border-radius: 50%;
  transform: rotate(45deg);
}

三、CSS圆形

  1. 利用border-radius属性实现圆形:
.circle {
  width: 100px;
  height: 100px;
  background-color: red;
  border-radius: 50%;
}
  1. 利用transform属性实现旋转圆形:
.circle {
  width: 100px;
  height: 100px;
  background-color: red;
  border-radius: 50%;
  transform: rotate(45deg);
}

四、CSS正方形

.square {
  width: 100px;
  height: 100px;
  background-color: red;
}

五、CSS多边形

.polygon {
  width: 100px;
  height: 100px;
  background-color: red;
  clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
}

六、CSS箭头

.arrow {
  width: 0;
  height: 0;
  border-left: 20px solid transparent;
  border-right: 20px solid transparent;
  border-bottom: 40px solid red;
}

七、CSS气泡

.bubble {
  width: 100px;
  height: 100px;
  background-color: red;
  border-radius: 50%;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}

八、CSS爱心

.heart {
  width: 100px;
  height: 100px;
  background-color: red;
  clip-path: polygon(50% 0%, 75% 40%, 100% 40%, 100% 100%, 50% 100%, 0% 100%, 0% 40%, 25% 40%);
}

以上是纯CSS绘制各种图形的技巧,希望对您有所帮助。