The problem of flickering when hiding and displaying hover in css

there is the following code, when the mouse hover, there is a flickering problem, how to solve it?

<html>
<head>
    <title></title>
</head>
<body>
    <div class="box">
        <div class="child1"></div>
        <div class="child2"></div>
    </div>
</body>
</html>

<style type="text/css">
    body{
        background-color: -sharpf2f2f2;
    }
    .child1, .child2{
        width: 50px;
        height: 50px;
        background-color: deeppink;
    }
    .child1:hover{
        display: none;
    }
    .child1:hover ~ .child2{
        display: block;
    }
    .child2{
        display: none;
        background-color: blue;
    }
</style>



Css
Jul.15,2021

you can add hover to box

.box:hover .child1 {
  display: none;
}
.box:hover .child2 {
  display: block;
}

when the effect you want to achieve is hover, show .child1, hide .child2?


display: none replace it with opacity: 0

The

display:none element becomes non-interactive and the hover effect is eliminated.


thought about why it flashed,

hover to child1, child1 hides, child2 shows and obtains the hover effect. At the same time, the hover css of child1 immediately fails, and the display effect of child2 disappears

.

therefore, the hover effect should be added to the parent node, and only the hover of the parent node is persistent. So @ Shanjia is more appropriate

.

child2 plus pointer-events: none;


flicker is mainly caused by the parent hover. You need to load the hover, of the child element first and then the parent hover

.
Menu