CSS图形每日一练(上)

前言

本文将持续更新CSS图形练习,同步上传 GitHub

1. 正方形(Square)

#square {
  width: 100px;
  height: 100px;
  background: red;
}
复制代码

2. 矩形(Rectangle)

#rectangle {
  width: 200px;
  height: 100px;
  background: red;
}
复制代码

3. 圆形(Circle)

#circle {
  width: 100px;
  height: 100px;
  background: red;
  border-radius: 50%;
}
复制代码

4. 椭圆形(Oval)

#oval {
  width: 200px;
  height: 100px;
  background: red;
  border-radius: 100px / 50px;
}
复制代码

5. 上三角形(Triangle Up)

#triangle-up {
  width: 0px;
  height: 0px;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid red;
}
复制代码

6. 下三角形(Triangle Down)

#triagnle-down {
  width: 0px;
  height: 0px;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-top: 100px solid red;
}
复制代码

7. 左三角形(Triangle Left)

#triangle-left {
  width: 0px;
  height: 0px;
  border-right: 100px solid red;
  border-top: 50px solid transparent;
  border-bottom: 50px solid transparent;
}
复制代码

8. 右三角形(Triangle Right)

#triangle-right {
  width: 0px;
  height: 0px;
  border-left: 100px solid red;
  border-top: 50px solid transparent;
  border-bottom: 50px solid transparent;
}
复制代码

9. 左上三角(Triangle Top Left)

#triangle-topleft {
  width: 0px;
  height: 0px;
  border-top: 100px solid red;
  border-right: 100px solid transparent;
}
复制代码

10. 右上三角(Triangle Top Right)

#triangle-topright {
  width: 0px;
  height: 0px;
  border-top: 100px solid red;
  border-left: 100px solid transparent;
}
复制代码

11. 左下三角(Triangle Bottom Left)

#triangle-bottomleft {
  width: 0px;
  height: 0px;
  border-bottom: 100px solid red;
  border-right: 100px solid transparent;
}
复制代码

12. 右下三角(Triangle Bottom Right)

#triangle-bottomright {
  width: 0px;
  height: 0px;
  border-bottom: 100px solid red;
  border-left: 100px solid transparent;
}
复制代码

13. 上半圆(Semicircle Top)

实现原理:把高度 height 设置为宽度 width 的一半,并且左上角和右上角的圆角半径与元素的高度一致,而右下角和左下角的圆角半径设置为 0

#semicircle-top {
  width: 100px;
  height: 50px;
  background: red;
  border-radius: 100px 100px 0 0;
  /* 左上、右上、右下、左下 */
}
复制代码

14. 下半圆(Semicircle Bottom)

#semicircle-bottom {
  width: 100px;
  height: 50px;
  background: red;
  border-radius: 0 0 100px 100px;
  /* 左上、右上、右下、左下 */
}
复制代码

15. 左半圆(Semicircle Left)

#semicircle-left {
  width: 50px;
  height: 100px;
  background: red;
  border-radius: 50px 0 0 50px;
  /* 左上、右上、右下、左下 */
}
复制代码

16. 右半圆(Semicircle Right)

#semicircle-right {
  width: 50px;
  height: 100px;
  background: red;
  border-radius: 0 50px 50px 0;
  /* 左上、右上、右下、左下 */
}
复制代码

17. 扇形(Sector)

#sector {
  width: 100px;
  height: 100px;
  background: red;
  border-radius: 100px 0 0 0;
}
复制代码

18. 梯形(Trapezoid)

#trapezoid {
  width: 100px;
  height: 0;
  border-bottom: 100px solid red;
  border-left: 25px solid transparent;
  border-right: 25px solid transparent;
}
复制代码

19. 平行四边形(Parallelogram)

#parallelogram {
  width: 150px;
  height: 100px;
  background: red;
  transform: skew(20deg);
}
复制代码

20. Diamond Square

#diamond {
  width: 0;
  height: 0;
  border: 50px solid transparent;
  border-bottom-color: red;
  position: relative;
  top: -50px;
}
#diamond::after {
  position: absolute;
  content: '';
  top: 50px;
  left: -50px;
  width: 0;
  height: 0;
  border: 50px solid transparent;
  border-top-color: red;
}
复制代码

21. Diamond Shield

#diamond-shield {
  width: 0;
  height: 0;
  border: 50px solid transparent;
  border-bottom: 20px solid red;
  position: relative;
  top: -50px;
}
#diamond-shield::after {
  position: absolute;
  content: '';
  top: 20px;
  left: -50px;
  width: 0;
  height: 0;
  border: 50px solid transparent;
  border-top: 70px solid red;
}
复制代码

22. Diamond Narrow

#diamond-narrow {
  width: 0;
  height: 0;
  border: 50px solid transparent;
  border-bottom: 70px solid red;
  position: relative;
  top: -50px;
}
#diamond-narrow::after {
  position: absolute;
  content: '';
  top: 70px;
  left: -50px;
  width: 0;
  height: 0;
  border: 50px solid transparent;
  border-top: 70px solid red;
}
复制代码

23. Cut Diamond

#cut-diamond {
  border-style: solid;
  border-color: transparent transparent red transparent;
  border-width: 0 25px 25px 25px;
  width: 50px;
  height: 0;
  position: relative;
  box-sizing: content-box;
  margin: 20px 0 50px 0;
}
#cut-diamond::after {
  position: absolute;
  content: '';
  top: 25px;
  left: -25px;
  width: 0;
  height: 0;
  border: 50px solid transparent;
  border-top: 70px solid red;
}
复制代码

24. 五边形(Pentagon)

#pentagon {
  position: relative;
  width: 54px;
  box-sizing: content-box;
  border-width: 50px 18px 0;
  border-style: solid;
  border-color: red transparent;
}
#pentagon::before {
  position: absolute;
  content: '';
  top: -85px;
  left: -18px;
  width: 0;
  height: 0;
  border-width: 0 45px 35px;
  border-style: solid;
  border-color: transparent transparent red;
}
复制代码

25. 六边形(Hexagon)

#hexagon {
  width: 100px;
  height: 55px;
  background: red;
  position: relative;
}
#hexagon::before {
  position: absolute;
  content: '';
  top: -25px;
  left: 0;
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 25px solid red;
}
#hexagon::after {
  position: absolute;
  content: '';
  bottom: -25px;
  left: 0;
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-top: 25px solid red;
}
复制代码

26. 八边形(Octagon)

在Jsbin里coding时发现,盒模型将 border 的宽度加上了,无法显示应有的图形效果。随后加上 box-sizing: border-box;,就可以正常显示了。

#octagon {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
}
#octagon::before {
  position: absolute;
  content: '';
  top: 0;
  left: 0;
  width: 100px;
  height: 0;
  border-left: 29px solid white;
  border-right: 29px solid white;
  border-bottom: 29px solid red;
}
#octagon::after {
  position: absolute;
  content: '';
  bottom: 0;
  left: 0;
  width: 100px;
  height: 0;
  border-left: 29px solid white;
  border-right: 29px solid white;
  border-top: 29px solid red;
}
复制代码

27. 弯尾箭头(Curved Tail Arrow)

#curvedarrow {
  position: relative;
  width: 0;
  height: 0;
  border-top: 9px solid transparent;
  border-right: 9px solid red;
}
#curvedarrow::after {
  position: absolute;
  content: '';
  top: -12px;
  left: -9px;
  width: 12px;
  height: 12px;
  border: 0px solid transparent;
  border-top: 3px solid red;
  border-radius: 20px 0 0 0;
  transform: rotate(45deg);
}
复制代码

参考链接

转载于:https://juejin.im/post/5cad4a475188251ae64a6d7d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值