幾個常用的CSS3樣式代碼以及不相容的解決辦法

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   width   

border-radius實現圓角效果
1 CSS3代碼:2 3 -webkit-border-radius:10px;4 -moz-border-radius:10px;5 border-radius:10px;6 background-color:#666;7 width:200px;height:100px;

Firefox,Chrome Google,Safari等瀏覽器的顯示效果0-0:

                     圖0-0

但是IE這個異類不支援CSS3的這個屬性,在IE9下的顯示效果0-1:

                     圖0-1

但是不能放任它不管,還是找辦法解決這個相容性問題。

解決方案:在CSS檔案中通過behavior屬性——這個屬性只能被IE識別,引入一個htc檔案, ie-css3.htc

這個是由Remiz Rahnas使用 VML 編寫了一個 HTC 檔案,為 IE 瀏覽器提供圓角效果的支援。

 1 div{ 2     -webkit-border-radius:10p; 3     -moz-border-radius:10px; 4     border-radius:10px; 5     background-color:#666; 6     width:200px; 7     height:100px; 8     color:#fff; 9     behavior: url(ie-css3.htc);10 }

截兩幅圖看看效果,一幅來自IE6,一幅來自IE9:

                                             

                                                                                                                                                                     

注意:首先,在 Server 端需要引入一個 HTC 檔案,經過 gzip 壓縮後對 server 端和 client 端效能應該不會有太大的影響;其次,它會使你的 CSS 驗證不合法;另外,這個指令碼在 IE8 上有一些問題,它會使 z-index 值變成負數。因此使用時還需要小心.

box-shadow實現陰影製作效果
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 3 <head> 4     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 5     <title></title> 6     <style type="text/css"> 7         div img{ 8             background-color: #fff; 9             -webkit-box-shadow:5px 5px 5px #000;10             -moz-box-shadow:5px 5px 5px #000;11             box-shadow:5px 5px 5px #000;12             width:295px;13             height:300px;14             /* For IE 8 */15             -ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=8, Direction=135, Color=‘#000000‘)";16             /* For IE 5.5 - 7 */17             filter: progid:DXImageTransform.Microsoft.Shadow(Strength=8, Direction=135, Color=‘#000000‘);18         }19     </style>20 </head>21 <body>22     <div>23         <img src="beautiful.jpg">24     </div>25 </body>26 </html>

Chrome,Firefox,IE9下的效果顯示:

接下來要做的事情,想必讀者朋友都知道了,相容IE6-8。首先想到的IE的濾鏡。來看看效果吧。

 1 加上濾鏡之後的程式碼片段: 2  3 div img{ 4             background-color: #fff; 5             -webkit-box-shadow:5px 5px 5px #000; 6             -moz-box-shadow:5px 5px 5px #000; 7             box-shadow:5px 5px 5px #000; 8             width:295px; 9             height:300px;10             /* For IE 8 */11             -ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=8, Direction=135, Color=‘#000000‘)";12             /* For IE 5.5 - 7 */13             filter: progid:DXImageTransform.Microsoft.Shadow(Strength=8, Direction=135, Color=‘#000000‘);14         }

測試之後的效果,分別是IE5.5-IE8:

雖然差強人意,但湊合著用。如果有朋友知道除此之外的方法,能否告知一聲,共同進步嘛!^_^

opacity實現透明度效果
 1 div img{  2     width:295px; 3     height:300px; 4     -webkit-opacity:0.3; 5     -moz-opacity:0.3; 6     opacity: 0.3; 7     /*for IE 6,7,8*/ 8     filter:alpha(opacity=30); 9     border:1px solid #ccc;10 }

相容IE 6,7,8。效果(來自IE6):

transform:rotate(度數) 將元素旋轉X度
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 3 <head> 4     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 5     <title></title> 6     <style type="text/css"> 7         div { 8              9             width:150px;10             height:50px;11             margin: 50px;12             -webkit-transform:rotate(7deg);13             -moz-transform:rotate(7deg);14             -ms-transform:rotate(7deg);15             -o-transform:rotate(7deg);16             transform:rotate(7deg);17               border:1px solid #ccc;18         }19     </style>20 </head>21 <body>22     <div>23         24     </div>25 </body>26 </html>

讓我們來去W3C看看transform的相容性:

以上代碼已經實現了對以上瀏覽器及其版本的支援,但是IE6,7,8呢?俗話說,兵來將擋,水來土掩,山人自有妙計,只不過這妙計是借來的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.