CSS背景色漸變寫(xiě)法兼容ie6至ie9
最近在項(xiàng)目中 , 有很多地方都用到了線性漸變 , 比如:表單提交按鈕的背景,數(shù)據(jù)展示的標(biāo)題背景等等,按照以前的做法是切 1px 圖片然后 repeat-x 。下面我將介紹如何用 css 來(lái)完成該效果 。
css3:linear-gradient
比如:黑色漸變到白色,代碼如下:
復(fù)制代碼代碼如下:
.gradient{
background: -moz-linear-gradient(top, #000000 0%, #ffffff 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#000000), color-stop(100%,#ffffff));
background: -webkit-linear-gradient(top, #000000 0%,#ffffff 100%);
background: -o-linear-gradient(top, #000000 0%,#ffffff 100%);
background: -ms-linear-gradient(top, #000000 0%,#ffffff 100%);
background: linear-gradient(to bottom, #000000 0%,#ffffff 100%);
}
ie 濾鏡:filter
linear-gradient 在 ie9 以下是不支持的,所以對(duì)于 ie6 - ie8 我們可以使用濾鏡來(lái)解決 , 代碼如下:
復(fù)制代碼代碼如下:
.gradient{
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr=’#000000’, endColorstr=’#ffffff’,GradientType=0 );
}
由于 filter 是 ie 的私有屬性 , 所以我們需要針對(duì) ie9 單獨(dú)處理濾鏡效果,代碼如下:
復(fù)制代碼代碼如下:
:root {filter:none;}
總結(jié):
綜上所述,線性漸變的兼容寫(xiě)法如下:
復(fù)制代碼代碼如下:
.gradient{
background: #000000;
background: -moz-linear-gradient(top, #000000 0%, #ffffff 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#000000), color-stop(100%,#ffffff));
background: -webkit-linear-gradient(top, #000000 0%,#ffffff 100%);
background: -o-linear-gradient(top, #000000 0%,#ffffff 100%);
background: -ms-linear-gradient(top, #000000 0%,#ffffff 100%);
background: linear-gradient(to bottom, #000000 0%,#ffffff 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr=’#000000’, endColorstr=’#ffffff’,GradientType=0 );
}
:root .gradient{filter:none;}
PS:
在實(shí)際的項(xiàng)目中,往往會(huì)碰到圓角和漸變的組合,如果使用上面的寫(xiě)法,那么在 ie9 下會(huì)有 bug(在 ie9 下背景色不能完全切完),解決方法是SVG
相關(guān)經(jīng)驗(yàn)推薦
- 部分 IE Firefox在css中的差別
- 針對(duì)IE8正式版的CSS hack
- Axure8設(shè)置自定義背景圖片詳細(xì)操作方法
- IE6、IE7、IE8瀏覽器下的CSS、JS兼容性對(duì)比
- ie-css3.htc 讓IE6, 7, and 8也支持box-shadow
- IE下Css圓角沒(méi)有的解決方法
- css控制圖片按等比例縮放實(shí)例兼容IE6/IE7/火狐/谷歌
- 針對(duì)firefox ie6 ie7 ie8的css樣式hack
- 針對(duì)主流瀏覽器的CSS-HACK寫(xiě)法及IE常用條件注釋
- css 條件注釋區(qū)分非IE瀏覽器
