Search code examples
htmlcssz-indexoverlapping

My one element is overlaping other element


I am a Beginner to web development.

I can't figure out how to bring my logo to front using CSS. I've already tried setting z-index to 1000 and position to relative, but it still fails.

Here I've provided full code because I was not able to figure Out where am I going wrong!

What should I do bring element to front?

¯\_(ツ)_/¯

*{
    margin: 0;
    padding: 0;
}
body{
    font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
    background: linear-gradient(to right, #9c27b0, #8ecdff);
}

.logo{
    display: inline-block;
    position: relative;
    flex-basis: 20%;
    z-index: 1;
}
.logo img{
    width: 13vw;
    margin-left: 1vw;
    margin-top: 1vw;
}
nav{
    position: sticky;
}
.navbar{
    float: right;
}
.navbar ul{
    display: flex;
    list-style: none;
}
.navbar ul li{
    margin-top: 1.8vw;
    margin-right: 3vw;
}
.navbar ul li a{
    text-decoration: none;
    color: #fff;
}
.info{
    z-index: 0;
    width: 60vw;
    height: 140vh;
    position: absolute;
    top: 50%;
    left: -10%;
    transform: translateY(-50%);
}
#circle{
    width: 60vw;
    height: 140vh;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    border-radius: 50%;
    background-color: #ccc ;    
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Real Phone</title>
</head>
<body>
    <div class="wrapper">
        <nav>
            <div class="logo">
                <!-- I've commented the actual logo -->
                <!-- <img src="images/resourses/logo.png" alt=""> -->
                <h1>LOGO</h1>
            </div>
            <div class="navbar">
                <ul>
                    <li><a href="#">Home</a></li>
                    <li><a href="#">Phones</a></li>
                    <li><a href="#">Accessories</a></li>
                    <li><a href="#">Cart</a></li>
                </ul>
            </div>
        </nav>
        <div class="info">
            <div id="circle">
                <div class="feature" id="f1"></div>
                <div class="feature" id="f2"></div>
                <div class="feature" id="f3"></div>
                <div class="feature" id="f4"></div> 
            </div>
        </div>
    </div>
</body>
</html>


Solution

  • Your solution didn't work because you were setting the z-index inside of the navigation element that was already behind the circle. z-index only applies to all elements inside the container, so only inside your nav element.

    To fix the problem, set the z-index of .info to be behind everything else, so the #circle inside the .info is also behind everything else.

    *{
        margin: 0;
        padding: 0;
    }
    body{
        font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
        background: linear-gradient(to right, #9c27b0, #8ecdff);
    }
    
    .logo{
        display: inline-block;
        position: relative;
        flex-basis: 20%;
        z-index: 1;
    }
    .logo img{
        width: 13vw;
        margin-left: 1vw;
        margin-top: 1vw;
    }
    nav{
        position: sticky;
    }
    .navbar{
        float: right;
    }
    .navbar ul{
        display: flex;
        list-style: none;
    }
    .navbar ul li{
        margin-top: 1.8vw;
        margin-right: 3vw;
    }
    .navbar ul li a{
        text-decoration: none;
        color: #fff;
    }
    .info{
        /* set the z-index of the entire info to be behind everything else */
        z-index: -1;
        width: 60vw;
        height: 140vh;
        position: absolute;
        top: 50%;
        left: -10%;
        transform: translateY(-50%);
    }
    #circle{
        width: 60vw;
        height: 140vh;
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        border-radius: 50%;
        background-color: #ccc ;    
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="style.css">
        <title>Real Phone</title>
    </head>
    <body>
        <div class="wrapper">
            <nav>
                <div class="logo">
                    <!-- I've commented the actual logo -->
                    <img src="images/resourses/logo.png" alt="">
                    <h1>LOGO</h1>
                </div>
                <div class="navbar">
                    <ul>
                        <li><a href="#">Home</a></li>
                        <li><a href="#">Phones</a></li>
                        <li><a href="#">Accessories</a></li>
                        <li><a href="#">Cart</a></li>
                    </ul>
                </div>
            </nav>
            <div class="info">
                <div id="circle">
                    <div class="feature" id="f1"></div>
                    <div class="feature" id="f2"></div>
                    <div class="feature" id="f3"></div>
                    <div class="feature" id="f4"></div> 
                </div>
            </div>
        </div>
    </body>
    </html>