rgba background with IE filter alternative: IE9 renders both!

后端 未结 5 783
故里飘歌
故里飘歌 2020-12-15 07:42

I\'m trying use make a div\'s background transparent using a mixture of CSS3 rgba() and microsoft\'s filter property like this:

div         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 07:55

    I’ve come up with a hacky workaround that I thought I'd share.

    IE9 and above supports the :not() CSS pseudo selector. By using an attribute that doesn’t exist on an element we can get IE9 to disable it's filter gradient:

    div {
        width: 200px;
        height: 200px;
    
        /* For FF, Chome, Opera, IE9+ */
        background: rgba(0,0,255,0.5);
    
        /* For IE6-9 */
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#7FFF0000,endColorstr=#7FFF0000);
    }
    
    div:not([dummy]) {
        /* IE9 only */
        filter: progid:DXImageTransform.Microsoft.gradient(enabled='false');
    }
    

    I ended up using this because my transparent div only features once. It also seemed a little neater keeping things to CSS, rather than using conditional comments in the HTML.

    Edit: In support of other answers, I found this article from the Microsoft dev team encouraging developers to use conditional comments, not CSS workarounds like mine.

提交回复
热议问题