標籤:style http color 使用 strong os
css border屬性竟然可以用來畫圖,今天項目中遇到該問題,藉此有機會深入瞭解了下。
css border屬性在我的印象當中只是方方正正的東西,無非就是增加邊框的寬度,變換邊框的顏色,因此一直以為邊框不管怎麼變換都是一個長方體,這個念頭今天終於可以拋棄了。
先看下普通邊框效果:
.border1{width:50px; height:50px; border:2px solid; border-color:#ff3300 #0000ff #339966 #00ff00;}<div class="border1"></div>
我們再將border的值增加到20px看看效果,驚喜出現了,發現竟然繪製出了4個梯形。
.border1{width:50px; height:50px; border:20px solid; border-color:#ff3300 #0000ff #339966 #00ff00;}<div class="border1"></div>
我們再將div的高度、寬度都設定為0,width:0;height:0;再看看效果。此時繪製出了4個等腰直角三角形。
.border1{width:0px; height:0px; border:20px solid; border-color:#ff3300 #0000ff #339966 #00ff00;}<div class="border1"></div>
我們再將左、右、下三個邊框變為透明,border-color:#ff3300 transparent transparent transparent;
.border1{width:0px; height:0px; border:20px solid; border-color:#ff3300 transparent transparent transparent;}<div class="border1"></div>
成功繪製出一個三角形。哈哈。。。
利用邊框繪圖的原理就是這樣,可以改變邊框的寬度繪製不規則的形狀。
下面是利用邊框實現氣泡對話方塊的例子:
<style>.box{width:300px; padding:30px 20px; border:5px solid #beceeb; position:relative;}.box span{width:0;height:0;font-size:0;overflow:hidden;position:absolute;}.box span.bot{border-width:20px;border-style:solid dashed dashed;border-color:#beceeb transparent transparent; left:80px;bottom:-40px;}.box span.top{border-width:20px;border-style:solid dashed dashed;border-color:#ffffff transparent transparent; left:80px;bottom:-33px;}</style><div class="box"> <span class="bot"></span> <span class="top"></span> css 邊框法實現氣泡對話方塊</div>
:
下面解決IE6問題:
IE6的空div高度bug:
IE6下,空div會有莫名的高度,也就是說“height: 0;”不頂用,此時形成的尖角的實際占高於其他瀏覽器是有差異的。可使用:
font-size: 0;
line-height: 0;
overflow: hidden;(來解決空的div會在ie6下有高度)
#test3 {
height: 0;
width: 0;
overflow: hidden;
font-size: 0;
line-height: 0;
border-color: #FF9600 transparent transparent;
border-style: solid;
border-width: 20px;
}
可是,IE6下不支援透明啊,會出現的樣子:
找到一個在IE6下邊框透明的文章中找到解決辦法,設定餘下三條邊的border-style為dashed,即可達到透明的效果:
#test4 {
height: 0;
width: 0;
overflow: hidden;
font-size: 0;
line-height: 0;
border-color: #FF9600 transparent transparent;
border-style: solid dashed dashed;(這樣的話在ie6下就透明了,想知道為啥嗎???看下面的玄機,哈哈)
border-width: 20px;
}
當然,在IE6下,不設定透明,將其顏色設定為背景色,使其看不出來也是可以的。
- 透明:
IE6 瀏覽器不支援transparent透明屬性,就border產生三角技術而言,直接設定對應的透明邊框的border- style屬性為dotted或是dashed即可解決這一問題,原因是在IE6下,點線與虛線均以邊框寬度為基準,點線長度必須是其寬度的3倍以上 (height>=border-width*3),虛線寬長度必須是其寬度的5倍以上(height>=border-width*5), 否則點線和虛線都不會出現。