Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safari: drop-shadow filter remains visible even with hidden element

Tags:

html

css

safari

I'm applying drop-shadow filter to a div (dropdown) that is displayed and hidden as per user interaction, however, in Safari the drop-shadow remains visible even when the element is hidden.

The bug happens only in Safari.

document.querySelector('.bt').addEventListener('click', function () {
    let b = document.querySelector('.b');

    if (b.style.display === "none") {
        b.style.display = "block";
    } else {
        b.style.display = "none";
    }
})
.bt {
    padding: 10px;
    margin-bottom: 40px;
}

.a {
    height: 100px;
    padding: 20px;
    background: #ccc;
}

.b {
    position: relative;
    width: 50px;
    height: 50px;
    background: #0f0;
    filter: drop-shadow(0 0 0.125rem #000);
}

.b::after {
    content: '';
    position: absolute;
    display: inline-block;
    width: 0;
    height: 0;
    border-right: .5rem solid transparent;
    border-left: .5rem solid transparent;
    border-bottom: .5rem solid #0f0;
    top: -.5rem;
    left: .5rem;
}
<h1>drop-shadow filter bug with Safari</h1>

<button class="bt">CLICK ME!</button>

<div class="a">
    <div class="b">
        .b
    </div>
</div>
like image 790
leandrof Avatar asked Jun 06 '19 13:06

leandrof


People also ask

What is difference between drop-shadow and box-shadow?

Using box-shadow gives us a rectangular shadow, even though the element has no background, while drop-shadow creates a shadow of the non-transparent parts of the image. This will work whether the image is inline in the HTML (either as an inline SVG, or in <img> tag), or a CSS background image.

How is the box-shadow different from text shadow?

Text shadow # The text-shadow property is very similar to the box-shadow property. It only works on text nodes. The values for text-shadow are the same as box-shadow and in the same order. The only difference is that text-shadow has no spread value and no inset keyword.

Can we apply transform property to box-shadow True or false?

Using transforms on the box-shadow (& transform ) property, we can create the illusion of an element moving closer or further away from the user.

What is shadow filter?

Shadow filter is used to create an attenuated shadow in the direction and color specified. The following parameters can be used in this filter.


1 Answers

I think I found a possible workaround.

Try adding will-change: filter; to your .b { ... } styles. The issue seems to be fixed this way.

Here is the code snippet with the fix:

document.querySelector('.bt').addEventListener('click', function () {
    let b = document.querySelector('.b');

    if (b.style.display === "none") {
        b.style.display = "block";
    } else {
        b.style.display = "none";
    }
})
.bt {
    padding: 10px;
    margin-bottom: 40px;
}

.a {
    height: 100px;
    padding: 20px;
    background: #ccc;
}

.b {
    will-change: filter;
    position: relative;
    width: 50px;
    height: 50px;
    background: #0f0;
    filter: drop-shadow(0 0 0.125rem #000);
}

.b::after {
    content: '';
    position: absolute;
    display: inline-block;
    width: 0;
    height: 0;
    border-right: .5rem solid transparent;
    border-left: .5rem solid transparent;
    border-bottom: .5rem solid #0f0;
    top: -.5rem;
    left: .5rem;
}
<h1>drop-shadow filter bug with Safari</h1>

<button class="bt">CLICK ME!</button>

<div class="a">
    <div class="b">
        .b
    </div>
</div>
like image 115
Vansh Avatar answered Oct 25 '22 23:10

Vansh