如何用CSS写一个三角形

html-css010

如何用CSS写一个三角形,第1张

1.可以用css3的border-radius属性来实现,支持ie9+

<div class="dm">

</div>

<div class="dm1">

</div>

<div class="dm2">

</div>

<div class="dm3">

</div>

<div class="dm4">

</div>

<div class="dm5">

</div>

<div class="dm6">

</div>

<div class="dm7">

</div>

<style>

.dm {

    width: 0

    height: 0

    border-left: 50px solid transparent

    border-right: 50px solid transparent

    border-bottom: 100px solid #00897B

}

.dm1{

  width: 0

    height: 0

    border-left: 50px solid transparent

    border-right: 50px solid transparent

    border-top: 100px solid #00897Bmargin-top: 20px

}

.dm2{

width: 0

    height: 0

    border-top: 50px solid transparent

    border-right: 100px solid #00897B

    border-bottom: 50px solid transparentmargin-top: 20px

}

.dm3{

  width: 0

    height: 0

    border-top: 50px solid transparent

    border-left: 100px solid #00897B

    border-bottom: 50px solid transparentmargin-top: 20px

}

.dm4{

   width: 0

    height: 0

    border-top: 100px solid #00897B

    border-right: 100px solid transparentmargin-top: 20px

}

.dm5{

 width: 0

    height: 0

    border-top: 100px solid #00897B

    border-left: 100px solid transparent margin-top: 20px

}

.dm6{

  width: 0

    height: 0

    border-bottom: 100px solid #00897B

    border-right: 100px solid transparent

}

.dm7{

  width: 0

    height: 0

    border-bottom: 100px solid #00897B

    border-left: 100px solid transparent

}

</style>

当div宽度为0,高度为0的时候,只设置border的大小和四边不同颜色可看到下图,只保留一边的颜色,另外三边的颜色设置为transparent可得到一个方向的三角形。因此,可延伸出设置一边的border的颜色,相邻两边的border设置为transparent可得到一个方向的三角形。

border的大小的值为三角形底边上的高,三角形的底为相邻两边的border的高的和,如下图所示的红色锐角三角形

若想实现直角三角形的效果,如下图所示直角在左上的三角形,按原理可设置上border和左border的颜色,另外两条边的颜色为transparent,此样式可简写为只设置上border的颜色,另一条相邻的右border为transparent

你也许会遇到要画个奇怪三角形的时候,只要参考锐角三角形的方式,找到三角形底边和高,计算出三角形高和把相邻两条边的高相加作为底部,你可以画出各种各样的三角形,如果再加上角度旋转的css,你便啥三角形都能画!

此类三角形解决思路通常使用两个不同颜色的三角形做颜色叠加,比如做如上图所示的边框为1px的红色边框白色底三角形,就先画一个红色三角形,再画一个尺寸少2px的白色底三角形,然后设置两个三角形的position将三角形重叠,这里使用伪元素实现

1、向上正箭头

2、向下正箭头

3、向左正箭头

4、向右正箭头

5、向左上箭头

6、向右上箭头

7、向左下箭头

8、向右下箭头