css如何讓背景色漸層相容的寫法詳解

來源:互聯網
上載者:User
最近在項目中,有很多地方都用到了線性漸層,比如:表單提交按鈕的背景,資料展示的標題背景等等,按照以前的做法是切 1px 圖片然後 repeat-x。下面我將介紹如何用 css 來完成該效果。

css3:linear-gradient

比如:黑色漸層到白色,代碼如下:


.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%);}

說明:linear-gradient 具體用法點此進入。

ie 濾鏡:filter

linear-gradient 在 ie9 以下是不支援的,所以對於 ie6 - ie8 我們可以使用濾鏡來解決,代碼如下:


.gradient{    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#ffffff',GradientType=0 );}

由於 filter 是 ie 的私人屬性,所以我們需要針對 ie9 單獨處理濾鏡效果,代碼如下:


:root {filter:none;}

總結:

綜上所述,線性漸層的相容寫法如下:


.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;}
相關文章

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.