Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a child element to show behind (lower z-index) than its parent?

Tags:

html

dom

css

I need a certain dynamic element to always appear on top of another element, no matter what order in the DOM tree they are. Is this possible? I've tried z-index (with position: relative), and it doesn't seem to work.

I need:

<div class="a">     <div class="b"></div> </div>  <div class="b">     <div class="a"></div> </div> 

To display exactly the same when rendered. And for flexibility purposes (I'm planning on distributing a plugin that needs this functionality), I'd really like to not have to resort to absolute or fixed positioning.

For what it's worth, to perform the function I was wanting, I made a conditional statement where the overlapping child element would become transparent in the case it was blocking the view of its parent. It's not perfect, but it's something.

like image 268
dclowd9901 Avatar asked Mar 23 '10 21:03

dclowd9901


People also ask

Can a child have 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?

CSS using z-index doesn't place child element behind its parent.


1 Answers

If the elements make a hierarchy, it cannot be done that way, because every positioned element creates new stacking context, and z-index is relative to the elements of the same stacking context.

like image 76
jholster Avatar answered Sep 24 '22 03:09

jholster