CSS Hover bug, overlay fluctuating effect on hover












3















I am trying to place one overlay div over another so that on hover on the card div displays overlay div above it. But on hover, I don't know why there is some bug which creates a fluctuating effect before displaying the overlay div.






.overlay {
opacity: 0;
z-index: -2;
height: 290px;
width: 240px;
background: #000;
border-radius: 30px;
/*display: inline-block;*/
position: relative;
top: -310px;
transition: all .4s ease;
}
.card:hover + .overlay {
opacity: 1;
z-index: 1;
transition: all .4s ease;
}

	<div class="card" style="background: #fff; height: 290px; width: 240px; border-radius: 30px; display: inline-block; margin:20px; box-shadow: 0 2px 6px rgba(112,112,112,0.2);"><img src="thumb.png" height="60%;"></div>
<div class="overlay"></div>





What am I doing wrong here?










share|improve this question


















  • 1





    I think it will work if you warp them and use the parent:hover > .overlay instead of selecting the next element like you do now (Edit: Coli's answer is better)

    – Alon Eitan
    Nov 19 '18 at 13:19








  • 1





    The problem is that you are putting another element over your hovered element - which makes your mouse cursor now rest on this element, meaning the other one is not in its hover state any more … Make that overlay a descendant of the triggering element to avoid this. (Hovering a descendant element always automatically means hovering the ancestor as well.)

    – misorude
    Nov 19 '18 at 13:20













  • if you put your overlay inside the card, then you could position it absolutely to the card (therefore not needed the manual minus top) and this would solve your hover problem

    – Pete
    Nov 19 '18 at 13:28






  • 1





    here is an example - easier way to line up your overlay to be on top of the card too: jsfiddle.net/waxzes0j

    – Pete
    Nov 19 '18 at 13:34











  • @Pete why you don't post this as an answer?

    – Coli
    Nov 19 '18 at 13:44
















3















I am trying to place one overlay div over another so that on hover on the card div displays overlay div above it. But on hover, I don't know why there is some bug which creates a fluctuating effect before displaying the overlay div.






.overlay {
opacity: 0;
z-index: -2;
height: 290px;
width: 240px;
background: #000;
border-radius: 30px;
/*display: inline-block;*/
position: relative;
top: -310px;
transition: all .4s ease;
}
.card:hover + .overlay {
opacity: 1;
z-index: 1;
transition: all .4s ease;
}

	<div class="card" style="background: #fff; height: 290px; width: 240px; border-radius: 30px; display: inline-block; margin:20px; box-shadow: 0 2px 6px rgba(112,112,112,0.2);"><img src="thumb.png" height="60%;"></div>
<div class="overlay"></div>





What am I doing wrong here?










share|improve this question


















  • 1





    I think it will work if you warp them and use the parent:hover > .overlay instead of selecting the next element like you do now (Edit: Coli's answer is better)

    – Alon Eitan
    Nov 19 '18 at 13:19








  • 1





    The problem is that you are putting another element over your hovered element - which makes your mouse cursor now rest on this element, meaning the other one is not in its hover state any more … Make that overlay a descendant of the triggering element to avoid this. (Hovering a descendant element always automatically means hovering the ancestor as well.)

    – misorude
    Nov 19 '18 at 13:20













  • if you put your overlay inside the card, then you could position it absolutely to the card (therefore not needed the manual minus top) and this would solve your hover problem

    – Pete
    Nov 19 '18 at 13:28






  • 1





    here is an example - easier way to line up your overlay to be on top of the card too: jsfiddle.net/waxzes0j

    – Pete
    Nov 19 '18 at 13:34











  • @Pete why you don't post this as an answer?

    – Coli
    Nov 19 '18 at 13:44














3












3








3








I am trying to place one overlay div over another so that on hover on the card div displays overlay div above it. But on hover, I don't know why there is some bug which creates a fluctuating effect before displaying the overlay div.






.overlay {
opacity: 0;
z-index: -2;
height: 290px;
width: 240px;
background: #000;
border-radius: 30px;
/*display: inline-block;*/
position: relative;
top: -310px;
transition: all .4s ease;
}
.card:hover + .overlay {
opacity: 1;
z-index: 1;
transition: all .4s ease;
}

	<div class="card" style="background: #fff; height: 290px; width: 240px; border-radius: 30px; display: inline-block; margin:20px; box-shadow: 0 2px 6px rgba(112,112,112,0.2);"><img src="thumb.png" height="60%;"></div>
<div class="overlay"></div>





What am I doing wrong here?










share|improve this question














I am trying to place one overlay div over another so that on hover on the card div displays overlay div above it. But on hover, I don't know why there is some bug which creates a fluctuating effect before displaying the overlay div.






.overlay {
opacity: 0;
z-index: -2;
height: 290px;
width: 240px;
background: #000;
border-radius: 30px;
/*display: inline-block;*/
position: relative;
top: -310px;
transition: all .4s ease;
}
.card:hover + .overlay {
opacity: 1;
z-index: 1;
transition: all .4s ease;
}

	<div class="card" style="background: #fff; height: 290px; width: 240px; border-radius: 30px; display: inline-block; margin:20px; box-shadow: 0 2px 6px rgba(112,112,112,0.2);"><img src="thumb.png" height="60%;"></div>
<div class="overlay"></div>





What am I doing wrong here?






.overlay {
opacity: 0;
z-index: -2;
height: 290px;
width: 240px;
background: #000;
border-radius: 30px;
/*display: inline-block;*/
position: relative;
top: -310px;
transition: all .4s ease;
}
.card:hover + .overlay {
opacity: 1;
z-index: 1;
transition: all .4s ease;
}

	<div class="card" style="background: #fff; height: 290px; width: 240px; border-radius: 30px; display: inline-block; margin:20px; box-shadow: 0 2px 6px rgba(112,112,112,0.2);"><img src="thumb.png" height="60%;"></div>
<div class="overlay"></div>





.overlay {
opacity: 0;
z-index: -2;
height: 290px;
width: 240px;
background: #000;
border-radius: 30px;
/*display: inline-block;*/
position: relative;
top: -310px;
transition: all .4s ease;
}
.card:hover + .overlay {
opacity: 1;
z-index: 1;
transition: all .4s ease;
}

	<div class="card" style="background: #fff; height: 290px; width: 240px; border-radius: 30px; display: inline-block; margin:20px; box-shadow: 0 2px 6px rgba(112,112,112,0.2);"><img src="thumb.png" height="60%;"></div>
<div class="overlay"></div>






html css






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 13:17









SayamKSayamK

297




297








  • 1





    I think it will work if you warp them and use the parent:hover > .overlay instead of selecting the next element like you do now (Edit: Coli's answer is better)

    – Alon Eitan
    Nov 19 '18 at 13:19








  • 1





    The problem is that you are putting another element over your hovered element - which makes your mouse cursor now rest on this element, meaning the other one is not in its hover state any more … Make that overlay a descendant of the triggering element to avoid this. (Hovering a descendant element always automatically means hovering the ancestor as well.)

    – misorude
    Nov 19 '18 at 13:20













  • if you put your overlay inside the card, then you could position it absolutely to the card (therefore not needed the manual minus top) and this would solve your hover problem

    – Pete
    Nov 19 '18 at 13:28






  • 1





    here is an example - easier way to line up your overlay to be on top of the card too: jsfiddle.net/waxzes0j

    – Pete
    Nov 19 '18 at 13:34











  • @Pete why you don't post this as an answer?

    – Coli
    Nov 19 '18 at 13:44














  • 1





    I think it will work if you warp them and use the parent:hover > .overlay instead of selecting the next element like you do now (Edit: Coli's answer is better)

    – Alon Eitan
    Nov 19 '18 at 13:19








  • 1





    The problem is that you are putting another element over your hovered element - which makes your mouse cursor now rest on this element, meaning the other one is not in its hover state any more … Make that overlay a descendant of the triggering element to avoid this. (Hovering a descendant element always automatically means hovering the ancestor as well.)

    – misorude
    Nov 19 '18 at 13:20













  • if you put your overlay inside the card, then you could position it absolutely to the card (therefore not needed the manual minus top) and this would solve your hover problem

    – Pete
    Nov 19 '18 at 13:28






  • 1





    here is an example - easier way to line up your overlay to be on top of the card too: jsfiddle.net/waxzes0j

    – Pete
    Nov 19 '18 at 13:34











  • @Pete why you don't post this as an answer?

    – Coli
    Nov 19 '18 at 13:44








1




1





I think it will work if you warp them and use the parent:hover > .overlay instead of selecting the next element like you do now (Edit: Coli's answer is better)

– Alon Eitan
Nov 19 '18 at 13:19







I think it will work if you warp them and use the parent:hover > .overlay instead of selecting the next element like you do now (Edit: Coli's answer is better)

– Alon Eitan
Nov 19 '18 at 13:19






1




1





The problem is that you are putting another element over your hovered element - which makes your mouse cursor now rest on this element, meaning the other one is not in its hover state any more … Make that overlay a descendant of the triggering element to avoid this. (Hovering a descendant element always automatically means hovering the ancestor as well.)

– misorude
Nov 19 '18 at 13:20







The problem is that you are putting another element over your hovered element - which makes your mouse cursor now rest on this element, meaning the other one is not in its hover state any more … Make that overlay a descendant of the triggering element to avoid this. (Hovering a descendant element always automatically means hovering the ancestor as well.)

– misorude
Nov 19 '18 at 13:20















if you put your overlay inside the card, then you could position it absolutely to the card (therefore not needed the manual minus top) and this would solve your hover problem

– Pete
Nov 19 '18 at 13:28





if you put your overlay inside the card, then you could position it absolutely to the card (therefore not needed the manual minus top) and this would solve your hover problem

– Pete
Nov 19 '18 at 13:28




1




1





here is an example - easier way to line up your overlay to be on top of the card too: jsfiddle.net/waxzes0j

– Pete
Nov 19 '18 at 13:34





here is an example - easier way to line up your overlay to be on top of the card too: jsfiddle.net/waxzes0j

– Pete
Nov 19 '18 at 13:34













@Pete why you don't post this as an answer?

– Coli
Nov 19 '18 at 13:44





@Pete why you don't post this as an answer?

– Coli
Nov 19 '18 at 13:44












2 Answers
2






active

oldest

votes


















5














That's because the z-index of the overlay increases by hovering the .card. But now you no longer hovering the .card, you are hovering the .overlay and so it disappears.

To fix this, you should add a .overlay:hover style too:






.overlay {
opacity: 0;
z-index: -2;
height: 290px;
width: 240px;
background: #000;
border-radius: 30px;
/*display: inline-block;*/
position: relative;
top: -310px;
transition: all .4s ease;
}
.card:hover + .overlay, .overlay:hover {
opacity: 1;
z-index: 1;
transition: all .4s ease;
}

<div class="card" style="background: #fff; height: 290px; width: 240px; border-radius: 30px; display: inline-block; margin:20px; box-shadow: 0 2px 6px rgba(112,112,112,0.2);"><img src="thumb.png" height="60%;"></div>
<div class="overlay"></div>








share|improve this answer

































    1














    Another idea is to prevent the overlay from catching events (hover in this case) by using pointer-events: none; so that you don't lose the initial hover applied to the card:






    .overlay {
    opacity: 0;
    z-index: -2;
    height: 290px;
    width: 240px;
    background: #000;
    border-radius: 30px;
    pointer-events: none;
    position: relative;
    top: -310px;
    transition: all .4s ease;
    }

    .card:hover+.overlay {
    opacity: 1;
    z-index: 1;
    transition: all .4s ease;
    }

    .card {
    background: red;
    height: 290px;
    width: 240px;
    border-radius: 30px;
    display: inline-block;
    margin: 20px;
    box-shadow: 0 2px 6px rgba(112, 112, 112, 0.2);
    }

    <div class="card"></div>
    <div class="overlay"></div>





    You can also simplify your code using pseudo element:






    .card:before {
    content:"";
    position: absolute;
    opacity: 0;
    z-index: -2;
    top:20px;
    right:20px;
    left:-20px;
    bottom:-20px;
    background: #000;
    border-radius: 30px;
    pointer-events: none;
    transition: all .4s ease;
    }

    .card:hover::before {
    opacity: 1;
    z-index: 1;
    transition: all .4s ease;
    }

    .card {
    position:relative;
    background: red;
    height: 290px;
    width: 240px;
    border-radius: 30px;
    display: inline-block;
    margin: 20px;
    box-shadow: 0 2px 6px rgba(112, 112, 112, 0.2);
    }

    <div class="card"></div>








    share|improve this answer


























    • Nice solution too. But you should mention, that every content on .overlay isn't clickable then.

      – Coli
      Nov 19 '18 at 13:43






    • 1





      @coli already said: to prevent the overlay from catching events

      – Temani Afif
      Nov 19 '18 at 13:47











    • I don't think that's clear enough to understand that you can't click links on it anymore.

      – Coli
      Nov 19 '18 at 13:49













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375486%2fcss-hover-bug-overlay-fluctuating-effect-on-hover%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    That's because the z-index of the overlay increases by hovering the .card. But now you no longer hovering the .card, you are hovering the .overlay and so it disappears.

    To fix this, you should add a .overlay:hover style too:






    .overlay {
    opacity: 0;
    z-index: -2;
    height: 290px;
    width: 240px;
    background: #000;
    border-radius: 30px;
    /*display: inline-block;*/
    position: relative;
    top: -310px;
    transition: all .4s ease;
    }
    .card:hover + .overlay, .overlay:hover {
    opacity: 1;
    z-index: 1;
    transition: all .4s ease;
    }

    <div class="card" style="background: #fff; height: 290px; width: 240px; border-radius: 30px; display: inline-block; margin:20px; box-shadow: 0 2px 6px rgba(112,112,112,0.2);"><img src="thumb.png" height="60%;"></div>
    <div class="overlay"></div>








    share|improve this answer






























      5














      That's because the z-index of the overlay increases by hovering the .card. But now you no longer hovering the .card, you are hovering the .overlay and so it disappears.

      To fix this, you should add a .overlay:hover style too:






      .overlay {
      opacity: 0;
      z-index: -2;
      height: 290px;
      width: 240px;
      background: #000;
      border-radius: 30px;
      /*display: inline-block;*/
      position: relative;
      top: -310px;
      transition: all .4s ease;
      }
      .card:hover + .overlay, .overlay:hover {
      opacity: 1;
      z-index: 1;
      transition: all .4s ease;
      }

      <div class="card" style="background: #fff; height: 290px; width: 240px; border-radius: 30px; display: inline-block; margin:20px; box-shadow: 0 2px 6px rgba(112,112,112,0.2);"><img src="thumb.png" height="60%;"></div>
      <div class="overlay"></div>








      share|improve this answer




























        5












        5








        5







        That's because the z-index of the overlay increases by hovering the .card. But now you no longer hovering the .card, you are hovering the .overlay and so it disappears.

        To fix this, you should add a .overlay:hover style too:






        .overlay {
        opacity: 0;
        z-index: -2;
        height: 290px;
        width: 240px;
        background: #000;
        border-radius: 30px;
        /*display: inline-block;*/
        position: relative;
        top: -310px;
        transition: all .4s ease;
        }
        .card:hover + .overlay, .overlay:hover {
        opacity: 1;
        z-index: 1;
        transition: all .4s ease;
        }

        <div class="card" style="background: #fff; height: 290px; width: 240px; border-radius: 30px; display: inline-block; margin:20px; box-shadow: 0 2px 6px rgba(112,112,112,0.2);"><img src="thumb.png" height="60%;"></div>
        <div class="overlay"></div>








        share|improve this answer















        That's because the z-index of the overlay increases by hovering the .card. But now you no longer hovering the .card, you are hovering the .overlay and so it disappears.

        To fix this, you should add a .overlay:hover style too:






        .overlay {
        opacity: 0;
        z-index: -2;
        height: 290px;
        width: 240px;
        background: #000;
        border-radius: 30px;
        /*display: inline-block;*/
        position: relative;
        top: -310px;
        transition: all .4s ease;
        }
        .card:hover + .overlay, .overlay:hover {
        opacity: 1;
        z-index: 1;
        transition: all .4s ease;
        }

        <div class="card" style="background: #fff; height: 290px; width: 240px; border-radius: 30px; display: inline-block; margin:20px; box-shadow: 0 2px 6px rgba(112,112,112,0.2);"><img src="thumb.png" height="60%;"></div>
        <div class="overlay"></div>








        .overlay {
        opacity: 0;
        z-index: -2;
        height: 290px;
        width: 240px;
        background: #000;
        border-radius: 30px;
        /*display: inline-block;*/
        position: relative;
        top: -310px;
        transition: all .4s ease;
        }
        .card:hover + .overlay, .overlay:hover {
        opacity: 1;
        z-index: 1;
        transition: all .4s ease;
        }

        <div class="card" style="background: #fff; height: 290px; width: 240px; border-radius: 30px; display: inline-block; margin:20px; box-shadow: 0 2px 6px rgba(112,112,112,0.2);"><img src="thumb.png" height="60%;"></div>
        <div class="overlay"></div>





        .overlay {
        opacity: 0;
        z-index: -2;
        height: 290px;
        width: 240px;
        background: #000;
        border-radius: 30px;
        /*display: inline-block;*/
        position: relative;
        top: -310px;
        transition: all .4s ease;
        }
        .card:hover + .overlay, .overlay:hover {
        opacity: 1;
        z-index: 1;
        transition: all .4s ease;
        }

        <div class="card" style="background: #fff; height: 290px; width: 240px; border-radius: 30px; display: inline-block; margin:20px; box-shadow: 0 2px 6px rgba(112,112,112,0.2);"><img src="thumb.png" height="60%;"></div>
        <div class="overlay"></div>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 '18 at 13:27

























        answered Nov 19 '18 at 13:21









        ColiColi

        372214




        372214

























            1














            Another idea is to prevent the overlay from catching events (hover in this case) by using pointer-events: none; so that you don't lose the initial hover applied to the card:






            .overlay {
            opacity: 0;
            z-index: -2;
            height: 290px;
            width: 240px;
            background: #000;
            border-radius: 30px;
            pointer-events: none;
            position: relative;
            top: -310px;
            transition: all .4s ease;
            }

            .card:hover+.overlay {
            opacity: 1;
            z-index: 1;
            transition: all .4s ease;
            }

            .card {
            background: red;
            height: 290px;
            width: 240px;
            border-radius: 30px;
            display: inline-block;
            margin: 20px;
            box-shadow: 0 2px 6px rgba(112, 112, 112, 0.2);
            }

            <div class="card"></div>
            <div class="overlay"></div>





            You can also simplify your code using pseudo element:






            .card:before {
            content:"";
            position: absolute;
            opacity: 0;
            z-index: -2;
            top:20px;
            right:20px;
            left:-20px;
            bottom:-20px;
            background: #000;
            border-radius: 30px;
            pointer-events: none;
            transition: all .4s ease;
            }

            .card:hover::before {
            opacity: 1;
            z-index: 1;
            transition: all .4s ease;
            }

            .card {
            position:relative;
            background: red;
            height: 290px;
            width: 240px;
            border-radius: 30px;
            display: inline-block;
            margin: 20px;
            box-shadow: 0 2px 6px rgba(112, 112, 112, 0.2);
            }

            <div class="card"></div>








            share|improve this answer


























            • Nice solution too. But you should mention, that every content on .overlay isn't clickable then.

              – Coli
              Nov 19 '18 at 13:43






            • 1





              @coli already said: to prevent the overlay from catching events

              – Temani Afif
              Nov 19 '18 at 13:47











            • I don't think that's clear enough to understand that you can't click links on it anymore.

              – Coli
              Nov 19 '18 at 13:49


















            1














            Another idea is to prevent the overlay from catching events (hover in this case) by using pointer-events: none; so that you don't lose the initial hover applied to the card:






            .overlay {
            opacity: 0;
            z-index: -2;
            height: 290px;
            width: 240px;
            background: #000;
            border-radius: 30px;
            pointer-events: none;
            position: relative;
            top: -310px;
            transition: all .4s ease;
            }

            .card:hover+.overlay {
            opacity: 1;
            z-index: 1;
            transition: all .4s ease;
            }

            .card {
            background: red;
            height: 290px;
            width: 240px;
            border-radius: 30px;
            display: inline-block;
            margin: 20px;
            box-shadow: 0 2px 6px rgba(112, 112, 112, 0.2);
            }

            <div class="card"></div>
            <div class="overlay"></div>





            You can also simplify your code using pseudo element:






            .card:before {
            content:"";
            position: absolute;
            opacity: 0;
            z-index: -2;
            top:20px;
            right:20px;
            left:-20px;
            bottom:-20px;
            background: #000;
            border-radius: 30px;
            pointer-events: none;
            transition: all .4s ease;
            }

            .card:hover::before {
            opacity: 1;
            z-index: 1;
            transition: all .4s ease;
            }

            .card {
            position:relative;
            background: red;
            height: 290px;
            width: 240px;
            border-radius: 30px;
            display: inline-block;
            margin: 20px;
            box-shadow: 0 2px 6px rgba(112, 112, 112, 0.2);
            }

            <div class="card"></div>








            share|improve this answer


























            • Nice solution too. But you should mention, that every content on .overlay isn't clickable then.

              – Coli
              Nov 19 '18 at 13:43






            • 1





              @coli already said: to prevent the overlay from catching events

              – Temani Afif
              Nov 19 '18 at 13:47











            • I don't think that's clear enough to understand that you can't click links on it anymore.

              – Coli
              Nov 19 '18 at 13:49
















            1












            1








            1







            Another idea is to prevent the overlay from catching events (hover in this case) by using pointer-events: none; so that you don't lose the initial hover applied to the card:






            .overlay {
            opacity: 0;
            z-index: -2;
            height: 290px;
            width: 240px;
            background: #000;
            border-radius: 30px;
            pointer-events: none;
            position: relative;
            top: -310px;
            transition: all .4s ease;
            }

            .card:hover+.overlay {
            opacity: 1;
            z-index: 1;
            transition: all .4s ease;
            }

            .card {
            background: red;
            height: 290px;
            width: 240px;
            border-radius: 30px;
            display: inline-block;
            margin: 20px;
            box-shadow: 0 2px 6px rgba(112, 112, 112, 0.2);
            }

            <div class="card"></div>
            <div class="overlay"></div>





            You can also simplify your code using pseudo element:






            .card:before {
            content:"";
            position: absolute;
            opacity: 0;
            z-index: -2;
            top:20px;
            right:20px;
            left:-20px;
            bottom:-20px;
            background: #000;
            border-radius: 30px;
            pointer-events: none;
            transition: all .4s ease;
            }

            .card:hover::before {
            opacity: 1;
            z-index: 1;
            transition: all .4s ease;
            }

            .card {
            position:relative;
            background: red;
            height: 290px;
            width: 240px;
            border-radius: 30px;
            display: inline-block;
            margin: 20px;
            box-shadow: 0 2px 6px rgba(112, 112, 112, 0.2);
            }

            <div class="card"></div>








            share|improve this answer















            Another idea is to prevent the overlay from catching events (hover in this case) by using pointer-events: none; so that you don't lose the initial hover applied to the card:






            .overlay {
            opacity: 0;
            z-index: -2;
            height: 290px;
            width: 240px;
            background: #000;
            border-radius: 30px;
            pointer-events: none;
            position: relative;
            top: -310px;
            transition: all .4s ease;
            }

            .card:hover+.overlay {
            opacity: 1;
            z-index: 1;
            transition: all .4s ease;
            }

            .card {
            background: red;
            height: 290px;
            width: 240px;
            border-radius: 30px;
            display: inline-block;
            margin: 20px;
            box-shadow: 0 2px 6px rgba(112, 112, 112, 0.2);
            }

            <div class="card"></div>
            <div class="overlay"></div>





            You can also simplify your code using pseudo element:






            .card:before {
            content:"";
            position: absolute;
            opacity: 0;
            z-index: -2;
            top:20px;
            right:20px;
            left:-20px;
            bottom:-20px;
            background: #000;
            border-radius: 30px;
            pointer-events: none;
            transition: all .4s ease;
            }

            .card:hover::before {
            opacity: 1;
            z-index: 1;
            transition: all .4s ease;
            }

            .card {
            position:relative;
            background: red;
            height: 290px;
            width: 240px;
            border-radius: 30px;
            display: inline-block;
            margin: 20px;
            box-shadow: 0 2px 6px rgba(112, 112, 112, 0.2);
            }

            <div class="card"></div>








            .overlay {
            opacity: 0;
            z-index: -2;
            height: 290px;
            width: 240px;
            background: #000;
            border-radius: 30px;
            pointer-events: none;
            position: relative;
            top: -310px;
            transition: all .4s ease;
            }

            .card:hover+.overlay {
            opacity: 1;
            z-index: 1;
            transition: all .4s ease;
            }

            .card {
            background: red;
            height: 290px;
            width: 240px;
            border-radius: 30px;
            display: inline-block;
            margin: 20px;
            box-shadow: 0 2px 6px rgba(112, 112, 112, 0.2);
            }

            <div class="card"></div>
            <div class="overlay"></div>





            .overlay {
            opacity: 0;
            z-index: -2;
            height: 290px;
            width: 240px;
            background: #000;
            border-radius: 30px;
            pointer-events: none;
            position: relative;
            top: -310px;
            transition: all .4s ease;
            }

            .card:hover+.overlay {
            opacity: 1;
            z-index: 1;
            transition: all .4s ease;
            }

            .card {
            background: red;
            height: 290px;
            width: 240px;
            border-radius: 30px;
            display: inline-block;
            margin: 20px;
            box-shadow: 0 2px 6px rgba(112, 112, 112, 0.2);
            }

            <div class="card"></div>
            <div class="overlay"></div>





            .card:before {
            content:"";
            position: absolute;
            opacity: 0;
            z-index: -2;
            top:20px;
            right:20px;
            left:-20px;
            bottom:-20px;
            background: #000;
            border-radius: 30px;
            pointer-events: none;
            transition: all .4s ease;
            }

            .card:hover::before {
            opacity: 1;
            z-index: 1;
            transition: all .4s ease;
            }

            .card {
            position:relative;
            background: red;
            height: 290px;
            width: 240px;
            border-radius: 30px;
            display: inline-block;
            margin: 20px;
            box-shadow: 0 2px 6px rgba(112, 112, 112, 0.2);
            }

            <div class="card"></div>





            .card:before {
            content:"";
            position: absolute;
            opacity: 0;
            z-index: -2;
            top:20px;
            right:20px;
            left:-20px;
            bottom:-20px;
            background: #000;
            border-radius: 30px;
            pointer-events: none;
            transition: all .4s ease;
            }

            .card:hover::before {
            opacity: 1;
            z-index: 1;
            transition: all .4s ease;
            }

            .card {
            position:relative;
            background: red;
            height: 290px;
            width: 240px;
            border-radius: 30px;
            display: inline-block;
            margin: 20px;
            box-shadow: 0 2px 6px rgba(112, 112, 112, 0.2);
            }

            <div class="card"></div>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 19 '18 at 13:46

























            answered Nov 19 '18 at 13:39









            Temani AfifTemani Afif

            72.9k94081




            72.9k94081













            • Nice solution too. But you should mention, that every content on .overlay isn't clickable then.

              – Coli
              Nov 19 '18 at 13:43






            • 1





              @coli already said: to prevent the overlay from catching events

              – Temani Afif
              Nov 19 '18 at 13:47











            • I don't think that's clear enough to understand that you can't click links on it anymore.

              – Coli
              Nov 19 '18 at 13:49





















            • Nice solution too. But you should mention, that every content on .overlay isn't clickable then.

              – Coli
              Nov 19 '18 at 13:43






            • 1





              @coli already said: to prevent the overlay from catching events

              – Temani Afif
              Nov 19 '18 at 13:47











            • I don't think that's clear enough to understand that you can't click links on it anymore.

              – Coli
              Nov 19 '18 at 13:49



















            Nice solution too. But you should mention, that every content on .overlay isn't clickable then.

            – Coli
            Nov 19 '18 at 13:43





            Nice solution too. But you should mention, that every content on .overlay isn't clickable then.

            – Coli
            Nov 19 '18 at 13:43




            1




            1





            @coli already said: to prevent the overlay from catching events

            – Temani Afif
            Nov 19 '18 at 13:47





            @coli already said: to prevent the overlay from catching events

            – Temani Afif
            Nov 19 '18 at 13:47













            I don't think that's clear enough to understand that you can't click links on it anymore.

            – Coli
            Nov 19 '18 at 13:49







            I don't think that's clear enough to understand that you can't click links on it anymore.

            – Coli
            Nov 19 '18 at 13:49




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375486%2fcss-hover-bug-overlay-fluctuating-effect-on-hover%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini