goldEli / blog

Blog
MIT License
2 stars 1 forks source link

纯CSS实现三角形 #16

Open goldEli opened 4 years ago

goldEli commented 4 years ago

Go Back

goldEli commented 4 years ago

大体思路:

  1. 创建一个 div,宽高相等,再单独设置上下左右的 border

1564047034201

  1. div 的宽高设为 0

1564047158301

  1. 将上左右的 border 颜色设置成 transparent,就得到一个向上的三角形了

1564047442465

代码如下:

    .triangle_to_top{
      width: 0; 
      height: 0; 
      border-top: 10px solid transparent;
      border-bottom: 10px solid blue;
      border-left: 10px solid transparent;
      border-right: 10px solid transparent;
    }
    .triangle_to_left{
      width: 0; 
      height: 0; 
      border-top: 10px solid transparent;
      border-bottom: 10px solid transparent;
      border-right: 10px solid red;
    }
    .triangle_to_right{
      width: 0; 
      height: 0; 
      border-top: 10px solid transparent;
      border-bottom: 10px solid transparent;

      border-left: 10px solid green;
    }
    .triangle_to_bottom{
      width: 0; 
      height: 0; 
      border-top: 10px solid brown;
      border-left: 10px solid transparent;
      border-right: 10px solid transparent;
    }
    <div class="triangle_to_top"></div>
    <div class="triangle_to_right"></div>
    <div class="triangle_to_bottom"></div>
    <div class="triangle_to_left"></div>

Reference: CSS Triangle