Does IE9 support CSS linear gradients?

前端 未结 3 794
庸人自扰
庸人自扰 2020-11-29 04:52

With Chrome/Safari and Firefox there\'s the -webkit-gradient and -moz-linear-gradient properties. How can I do this same sort of thing with IE9?

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 05:06

    The best cross-browser solution regarding IE 9+, that is conforming to W3C standards (doesn't result in an error in CSS validator) is the following:

    background-color: #fff; /* Browsers without linear-gradient support */
    background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #fff), color-stop(100%, #000)); /* Chrome, Safari 4+ */
    background-image: -webkit-linear-gradient(#fff 0%, #000 100%); /* Chrome 10+, Safari 5.1+ */    
    background-image:    -moz-linear-gradient(#fff 0%, #000 100%); /* Fx 3.6+ */   
    background-image:         linear-gradient(#fff 0%, #000 100%); /* W3C */
    -ms-filter: "progid:DXImageTransform.Microsoft.gradient(gradientType=0, startColorstr='#ffffff', endColorstr='#000000')";
    

    .gradient--test { 
        background-color: #fff; /* Browsers without linear-gradient support */
        background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #fff), color-stop(100%, #000)); /* Chrome, Safari 4+ */
        background-image: -webkit-linear-gradient(#fff 0%, #000 100%); /* Chrome 10+, Safari 5.1+ */    
        background-image:    -moz-linear-gradient(#fff 0%, #000 100%); /* Fx 3.6+ */   
        background-image:         linear-gradient(#fff 0%, #000 100%); /* W3C */
        -ms-filter: "progid:DXImageTransform.Microsoft.gradient(gradientType=0, startColorstr='#ffffff', endColorstr='#000000')";
    }
    .gradient--test {
      width: 61.8%;
      height: 200px;
     }

    IE 9 introduced the vendor prefix -ms-filter notation, that is according to standards, at the same time deprecated the proprietary filters.

    Neither -o- vendor prefix is necessary, nor -ms- (as IE 10 is the first IE to support gradients and it does support the W3C standards syntax). See http://caniuse.com/#feat=css-gradients
    Prefer lowercase hex color values for better gzipping, and clearly state background-color and background-image instead of background, because it could result in rendering errors in browsers without linear gradient support. Largely copied from my answer for this question.

提交回复
热议问题