CSS创建三角形原理

利用盒子的均分原理,盒子都是矩形或者正方形从形状的中心,向4个上下左右划分4个部分
image.png

  1. <div class="box"></div>
  2. <style>
  3. .box{
  4. width: 0;
  5. height: 0;
  6. margin: 100px auto;
  7. border: 100px solid #000;
  8. border-top-color: red;
  9. border-right-color: yellow;
  10. border-bottom-color: green;
  11. }
  12. </style>

圆角三角形

  1. /** 圆角三角形 */
  2. <div class="rounded-triangle"></div>
  3. <style>
  4. .rounded-triangle {
  5. width: 50px;
  6. height: 50px;
  7. border-top-right-radius: 30%;
  8. background-color: cyan;
  9. transform: rotate(-60deg) skewX(-30deg) scale(1,.866);
  10. }
  11. .rounded-triangle:before,
  12. .rounded-triangle:after {
  13. content: '';
  14. position: absolute;
  15. background-color: inherit;
  16. width: 50px;
  17. height: 50px;
  18. border-top-right-radius: 30%;
  19. }
  20. .rounded-triangle:before {
  21. transform: rotate(-135deg) skewX(-45deg) scale(1.414, .707) translate(0,-50%);
  22. }
  23. .rounded-triangle:after {
  24. transform: rotate(135deg) skewY(-45deg) scale(.707, 1.414) translate(50%);
  25. }
  26. </style>

image.png