filter:grayscale not always working in Edge

2019-09-10 08:04发布

问题:

I can see it working on Microsoft's website: https://developer.microsoft.com/en-us/microsoft-edge/testdrive/demos/css3filters/

But on the website I am currently working on, it only works some times.

For example, when I remove max-width on the img tag, the grayscale suddenly works, but then when I hover it, it stops working.

The filter was originally on an img tag, but I also tried on a div with the same result.

I know it might be too specific a case, but I can't seem to find anything related to this that might work. Is there some properties known to mess with filters in Edge?

EDIT

After tweaking around in the inspector, I have found that hiding a pseudo-element solves the grayscale bug.

.Cta:before {
    position: absolute;top: 50%;left: 0;right: 0;bottom: 0;
    display: block;content: '';z-index: 1;
    background: -webkit-gradient(linear,left top,left bottom,color-stop(0,hsla(0,0%,100%,0)),to(#fff));
    background: -webkit-linear-gradient(top,hsla(0,0%,100%,0),#fff);
    background: linear-gradient(180deg,hsla(0,0%,100%,0) 0,#fff);
    -webkit-transition: opacity .2s ease;
    transition: opacity .2s ease;
}

Code sample

<div class="Cta">
    <div class="cta-image">
        <img src="{{baseUrl}}{{image}}" alt="{{title}}" />
    </div>
    <div class="cta-content">
        <h3 class="cta-title">{{{title}}}</h3>
        <h4 class="cta-subtitle">{{subtitle}}</h4>
    </div>
</div>

CSS:

.Cta {position:relative;}
.Cta .cta-image {filter:grayscale(100%);opacity:0.2;}
.Cta img {display:block;width:100%;}
.Cta .cta-content {position:absolute;bottom:0;left:0;right:0;padding:0 30px 30px;}

回答1:

Finally found the culprit, removing the z-index from my pseudo-element was clearing the bug.

Putting z-index on each element to specifically tell the order instead of relying on browser defaults resolved the problem of the grayscale filter not being applied.

.Cta {position:relative;}
.Cta:before {position:absolute;top:50%;left:0;right:0;bottom:0;z-index:2;content:'';background:/*gradient*/;}
.Cta .cta-image {position:relative;z-index:1;filter:grayscale(100%);opacity:0.2;}
.Cta img {display:block;width:100%;}
.Cta .cta-content {position:absolute;bottom:0;left:0;right:0;z-index:3;padding:0 30px 30px;}