怎样使用纯css绘制空的三角形

html-css017

怎样使用纯css绘制空的三角形,第1张

直接用样式一步到位还无法实现空心的三角形,你姑且使用一大一小的三角形的实现假空心效果好了,我大概写一个方法,如下

<style type="text/css">

body{ margin:0 padding:0}

#triangle-bottomright {

position:absolute

left:0px

top:0px

    width: 0

    height: 0

    border-bottom: 100px solid red

    border-left: 100px solid transparent

padding:2px

}

#triangle-bottomright02 {

position:absolute

left:6px

top:6px

    width: 0

    height: 0

    border-bottom: 96px solid white

    border-left: 96px solid transparent

}

</style>

</head>

<body>

 <div id="triangle-bottomright"></div>

 <div id="triangle-bottomright02"></div>

</body>

1、理论

三角形实现原理:宽度width为0;height为0;

(1)有一条横竖边(上下左右)的设置为border-方向:长度 solid red,这个画的就是底部的直线。其他边使用border-方向:长度 solid transparent。

(2)有两个横竖边(上下左右)的设置,若斜边是在三角形的右边,这时候设置top或bottom的直线,和右边的斜线。若斜边是在三角形的左边,这时候设置top或bottom的直线,和左边的斜线。

二、实现

2.1 Triangle Up

#triangle-up {width:0    height:0    

border-left:50px solid transparent    

border-right:50px solid transparent    

border-bottom:100px solid red}

2.2 Triangle Down

#triangle-down {width:0    height:0  

  border-left:50px solid transparent    

border-right:50px solid transparent    

border-top:100px solid red}

2.3 Triangle Left

#triangle-left {

width:0  

  height:0  

  border-top:50px solid transparent  

  border-right:100px solid red   

 border-bottom:50px solid transparent}

2.4 Triangle Right

#triangle-right {width:0    

height:0    

border-top:50px solid transparent   

 border-left:100px solid red    

border-bottom:50px solid transparent}

2.5 Triangle Top Left

#triangle-topleft {width:0  

  height:0    

border-top:100px solid red   

 border-right:100px solid transparent}

2.6 Triangle Top Right

#triangle-topright {width:0   

 height:0  

  border-top:100px solid red    

border-left:100px solid transparent}

2.7 Triangle Bottom Left

#triangle-bottomleft {width:0   

 height:0    

border-bottom:100px solid red   

 border-right:100px solid transparent}

2.8 Triangle Bottom Right

#triangle-bottomright {width:0    

height:0   

 border-bottom:100px solid red    

border-left:100px solid transparent}

通过设置 宽和高为0 ,改变 border-color 属性即可实现三角形效果。

在当前的三角形后面添加一个一个实心三角形,然后将这个三角形绝对定位到当前三角行的位置切割。