Search code examples
javascripthtmlcssfrontendvisual-web-developer

When i am hovering the element (.box) that is just below the element(.box) then only the above box is hovered ? I am not able to understand


Here is CodePen link Please check it

HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="custom.css">
    <script src="app.js"></script>
</head>
<body>
    <div class="main">
        <div class="row">
            <input type="search" id="search" autofocus autocomplete="off" placeholder="Search...">
        </div>
        <div class="row" id="moviebox">
            <div class="box">
                <img src="https://picsum.photos/400/500" alt="">
                <div class="overlay">
                    <div class="title">
                        <h2>Title</h2>
                        <span>8.2</span>  
                    </div>
                    <h3>Overview:</h3>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit explicabo voluptates et!</p>
                </div>
            </div>
            <div class="box">
                <img src="https://picsum.photos/400/500" alt="">
                <div class="overlay">
                    <div class="title">
                        <h2>Title</h2><span>8.2</span>  
                    </div>
                    <h3>Overview:</h3>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit explicabo voluptates et!</p>
                </div>
            </div>
            <div class="box">
                <img src="https://picsum.photos/400/500" alt="">
                <div class="overlay">
                    <div class="title">
                        <h2>Title</h2><span>8.2</span>  
                    </div>
                    <h3>Overview:</h3>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit explicabo voluptates et!</p>
                </div>
            </div>         
        </div>
    </div>
</body>
</html>

CSS code

.main{
    border: 2px solid magenta;
    background-color: #bebdbd;
    position: absolute;
    width: 30vw;

    display:grid;
    flex-direction: column;
}

*{
    margin: 0;
    padding: 0;
}
.box{
    /* border: 2px solid black;  */
    overflow: hidden;
}
.box img{
    width: 200px;
    border-radius: 4px;
}

.main .box:hover .overlay{
    height: 100px;
    /* position: absolute; */
    z-index: 999;
    top: 0.5%;
    transition-delay:0s;
    transition-timing-function: linear;
    transition-duration: 300ms;
}

.overlay{
    position: absolute;
    top: -11%;
    background-color: #ffffff4e;
    width: 190px;
    height: 0px;
    overflow: hidden;
    padding: 5px;
    backdrop-filter: blur(16px);
}

#moviebox{
    position: relative;
    /* border: 2px solid red; */
    height: fit-content;
    width: 100%;
    display: flex;
    gap: 1rem;
    overflow: hidden;
    flex-wrap: wrap;
    padding: 5px;

}

#search{
    border: none;
    margin: 5vh 0vw 5vh 4vw;
    height: 5vh;
    width: 30vw;
    border-radius: 3px;
    padding-left: 2%;
    background-color: #949292;
    color: white;
    outline: none;
    font-size: 23px;
}

I was trying to make the overlay come on the top of each image element on hovering the image element. But when i hover the image elements that are below the top elements then top elements are hover triggered , i was expecting that each element will show overlays on hovering the corresponding box element .


Solution

  • Because you are using absolute positioning, each overlay is being positioned at the very top. What you need to do is add position: relative to the .box rule.

    .main{
        border: 2px solid magenta;
        background-color: #bebdbd;
        position: absolute;
        width: 30vw;
    
        display:grid;
        flex-direction: column;
    }
    
    *{
        margin: 0;
        padding: 0;
    }
    .box{
        /* border: 2px solid black;  */
        overflow: hidden;
        position: relative; /* <-- */
    }
    .box img{
        width: 200px;
        border-radius: 4px;
    }
    
    .main .box:hover .overlay{
        height: 100px;
        /* position: absolute; */
        z-index: 999;
        top: 0.5%;
        transition-delay:0s;
        transition-timing-function: linear;
        transition-duration: 300ms;
    }
    
    .overlay{
        position: absolute;
        top: -11%;
        background-color: #ffffff4e;
        width: 190px;
        height: 0px;
        overflow: hidden;
        padding: 5px;
        backdrop-filter: blur(16px);
    }
    
    #moviebox{
        position: relative;
        /* border: 2px solid red; */
        height: fit-content;
        width: 100%;
        display: flex;
        gap: 1rem;
        overflow: hidden;
        flex-wrap: wrap;
        padding: 5px;
    
    }
    
    #search{
        border: none;
        margin: 5vh 0vw 5vh 4vw;
        height: 5vh;
        width: 30vw;
        border-radius: 3px;
        padding-left: 2%;
        background-color: #949292;
        color: white;
        outline: none;
        font-size: 23px;
    }
    <div class="main">
        <div class="row">
            <input type="search" id="search" autofocus autocomplete="off" placeholder="Search...">
        </div>
        <div class="row" id="moviebox">
            <div class="box">
                <img src="https://picsum.photos/400/500" alt="">
                <div class="overlay">
                    <div class="title">
                        <h2>Title</h2>
                        <span>8.2</span>  
                    </div>
                    <h3>Overview:</h3>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit explicabo voluptates et!</p>
                </div>
            </div>
            <div class="box">
                <img src="https://picsum.photos/400/500" alt="">
                <div class="overlay">
                    <div class="title">
                        <h2>Title</h2><span>8.2</span>  
                    </div>
                    <h3>Overview:</h3>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit explicabo voluptates et!</p>
                </div>
            </div>
            <div class="box">
                <img src="https://picsum.photos/400/500" alt="">
                <div class="overlay">
                    <div class="title">
                        <h2>Title</h2><span>8.2</span>  
                    </div>
                    <h3>Overview:</h3>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit explicabo voluptates et!</p>
                </div>
            </div>         
        </div>
    </div>