css border畫圖

來源:互聯網
上載者:User

標籤: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), 否則點線和虛線都不會出現。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.