Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stacking with z-index, child element on top over parent sibling

I ran into this challenge: fiddle. The short story is, I want to have the green block in the middle of the z-order, without having to change the HTML. So yellow on the bottom, green in the middle, and red on top.

Yellow, red, and green boxes representing the elements

.parent {
  background-color: yellow;
  position: absolute;
  top: 0;
  left: 0;
  height: 200px;
  width: 200px;
  z-index: 1;
}

.child {
  background-color: red;
  position: relative;
  top: 10px;
  left: 20px;
  height: 50px;
  width: 150px;
  z-index: 100;
}

.other-guy {
  background-color: green;
  position: absolute;
  top: 40px;
  left: 100px;
  height: 200px;
  width: 200px;
  z-index: 50;
}
<div class="parent">
  Chillin in the background
  <div class="child">
    I really want to be on top.
  </div>
</div>
<div class="other-guy"> I want to be in the middle! </div>

The longer story is, in my solution I'm using bootstraps grid system to position the child element so the whole thing is responsive. The middle layer is a Google Maps element that needs to be manipulated by the user. My previous solution had an absolutely positioned child element on the map, which works, but I don't know how to make that responsive.

My new solution works great from a responsive angle, but then I found out that the parent is blocking interaction with the maps.

So I now need a solution have some responsive elements on top of Google Maps.

like image 723
Coo Avatar asked Sep 20 '15 09:09

Coo


People also ask

Can a child element have a higher z-index than parent?

This is impossible as a child's z-index is set to the same stacking index as its parent.

Does Z-index place the element behind its parent?

z-index is based on elements at the same level in the DOM, in other words, elements with the same parent. Since your class="dropdown" element is a child of the class="background" element it can never be below its parent. Save this answer.

Do child elements inherit Z-index?

The parent has a z-index of 2 and the child element also has a z-index of 2 . Because both parents create a stacking context, the z-index of all children is based on that of their parent. The z-index of elements inside of a stacking context are always relative to the parent's current order in its own stacking context.

How do you stack items on top of each other in CSS?

Using CSS position property: The position: absolute; property is used to position any element at the absolute position and this property can be used to stack elements on top of each other. Using this, any element can be positioned anywhere regardless of the position of other elements.


1 Answers

I removed the position absolute from the yellow div and removed the z-index from the green div. Maybe this is something as you said.

.parent {
    background-color: yellow;
    
    top: 0;
    left: 0;
    height: 200px;
    width: 200px;
    z-index: 1;
}
.child {
    background-color: red;
    position: relative;
    top: 10px;
    left: 20px;
    height: 50px;
    width: 150px;
    z-index: 2;
}
.other-guy {
    background-color: green;
    position: absolute;
    top: 40px;
    left: 100px;
    height: 200px;
    width: 200px;
    
}
<div class="parent">Chillin in the background
    <div class="child">I really want to be on top.</div>
</div>
<div class="other-guy">I want to be in the middle!</div>
like image 78
CodeRomeos Avatar answered Oct 23 '22 13:10

CodeRomeos