1

There are two divs side by side with same zIndex. I want to display a child div of first div over the second div kinda like an overlay. But no matter what zIndex i give to the child div, it remains below the second div.

.parent {
  border: 5px dotted red;
  height: 500px;
  width: 500px;
  display: flex;
  justify-content: center;
  align-items: start;
  padding: 50px;
  background-color: black;
}

.child {
  border: 5px solid blue;
  height: 100px;
  width: 100px;
  margin: 0 0 50px 50px;
  background-color: midnightblue;
  position: relative;
  z-index: 10;
}

.child-hover {
  position: absolute;
  border: 3px solid green;
  border-radius: 40px;
  height: 60px;
  width: 80px;
  top: 0;
  left: 90%;
  background-color: green;
  z-index: 9999 !important;
}
<div class="parent">
  <div class="child">
    <div class="child-hover"></div>
  </div>
  <div class="child"></div>
</div>

(Fiddle link)

In this fiddle you'll see two blue squares and a green ellipse. Part of green ellipse is hidden beneath the second blue square. I understand that's because the second div came after the first div in the html. I don't want that to happen.

Edit: I do not wish to remove the zIndex from .child.

S V
  • 68
  • 6

3 Answers3

2

Remove .child class z-index

 .parent {
            border: 5px dotted red;
            height: 500px;
            width: 500px;
            display:flex;
            justify-content: center;
            align-items: start;
            padding: 50px;
            background-color: black;
        }
        .child {
            border: 5px solid blue;
            height: 100px;
            width: 100px;
            margin: 0 0 50px 50px;
            background-color: midnightblue;
            position: relative;
        }

        .child-hover {
            position: absolute;
            border: 3px solid green;
            border-radius: 40px;
            height: 60px;
            width: 80px;
            top: 0;
            left: 90%;
            background-color: green;
            z-index: 9999 !important;
        }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dropdown</title>
</head>
<body>

    <div class="parent">
        <div class="child">
            <div class="child-hover">

            </div>
        </div>
        <div class="child"></div>
    </div>
</body>
</html>
Jaswinder Kaur
  • 1,606
  • 4
  • 15
2

All you have to do is remove the z-index in the .child class. Please try this.

.parent {
  border: 5px dotted red;
  height: 500px;
  width: 500px;
  display: flex;
  justify-content: center;
  align-items: start;
  padding: 50px;
  background-color: black;
}

.child {
  border: 5px solid blue;
  height: 100px;
  width: 100px;
  margin: 0 0 50px 50px;
  background-color: midnightblue;
  position: relative;

}

.child-hover {
  position: absolute;
  border: 3px solid green;
  border-radius: 40px;
  height: 60px;
  width: 80px;
  top: 0;
  left: 90%;
  background-color: green;
  z-index: 9999 !important;
}
<div class="parent">
  <div class="child">
    <div class="child-hover"></div>
  </div>
  <div class="child"></div>
</div>
0

z-index works with the positioned elements on the same level. Try adding this to your CSS code.

.child:first-of-type {
  z-index: 11;
}