1.無邊框三角形css
條件:width,height設置爲0,border-width設置必定的寬度,border-color保留一邊有顏色其餘三邊設置透明html
.triangle{ width:0px; height:0px; border-width:40px; border-style: solid; border-color: #FF7F24 transparent transparent transparent; }
效果:spa
2.有邊框三角形code
因爲咱們寫三角形的時候用到的就是border,因此給三角形添加邊框就不能再用border屬性了,咱們就使用元素覆蓋,設置大小不同的邊框,大的在下面,這就用到position:absolute,htm
//html <div class="triangle-border"> <div class="triangle"></div> </div> //css .triangle{ position:absolute; left:0px; top:0px; width:0px; height:0px; border-width:40px; border-style: solid; border-color: #FF7F24 #EEC900 #E066FF #90EE90;; } .triangle-border{ position:relative; width:0px; height:0px; border-width:42px; border-style: solid; border-color: #FF7F24 #EEC900 #E066FF #90EE90;; }
效果以下:blog
絕對定位並無覆蓋到父元素上,由於咱們的三角形是邊,而不是真正的內容區域,絕對定位(position:absolute),是根據相對定位父層內容的邊界計算的。因爲父元素div內容爲空,content的內容在中心,因此子元素是根據中心點來定位的,咱們知道這個原理,微調下子元素的定位便可,而後把父元素的邊框顏色設置爲邊框色ip
.triangle{ position:absolute; left:-40px;//修改這行 top:-40px;//修改這行 width:0px; height:0px; border-width:40px; border-style: solid; border-color: #FF7F24 #EEC900 #E066FF #90EE90; } .triangle-border{ position:relative; width:0px; height:0px; border-width:42px; border-style: solid; border-color: #333;//修改顏色 }
效果以下:it
而後隱藏父元素,子元素其餘三個邊就能夠io
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>img</title> <style> html,body{ width: 100%; height: 100%; padding:0; margin:0; } /*border-color: #FF7F24 #EEC900 #E066FF #90EE90;*/ .triangle{ position:absolute; left:-40px; top:-41px; width:0px; height:0px; border-width:40px; border-style: solid; border-color: #FF7F24 transparent transparent transparent; } .triangle-border{ position:relative; width:0px; height:0px; border-width:42px; border-style: solid; border-color: #333 transparent transparent transparent; } </style> </head> <body> <div id="main"> <div class="triangle-border"> <div class="triangle"></div> </div> </div> <script> </script> </body> </html>