Search code examples
htmlcssgoogle-chromepositioningz-index

z-index of elements in different parents in Chrome


There are two containers. One is fixed and covers 100% of the screen. Second is relative - contains scrollable content. The problem is that there are four elements to be positioned relative to each other as follows: 1st in a fixed container, 2nd in absolute, 3rd fixed, 4th in absolute.

<div class="container fixed">
    <div class="el z1"></div>
    <div class="el z3"></div>
</div>
<div class="container relative">
    <div class="el z2"></div>
    <div class="el z4"></div>
</div>

However, z-index of one of the parents will always be less than the other, which not allow to do order for 4 children elements.

There is fiddle http://jsfiddle.net/only_dimon/zkY4C/6/ I want order: yellow, green, white, black from bottom. But as you can see - yellow, white, green, black.

The result I want:
enter image description here

But elements should be in different containers.

Thanks in advance!

This helps for me: For many elements helps only html reconstruction. Luckily I don't need to use fixed container for fixed elements, because fixed element's position and sizes depends on window size, even if I put them into relative container.


Solution

  • After some further investigation, child element can't/aren't suppose to be able to overrule the parent z-index, some browser for example Firefox though ignores parent index for some reason and displays your elements are you wish but every other browser I've tried (Opera,Safari,Chrome) does not, so no it's not possible to accomplish what you want without changing the mark-up of your HTML for cross-browser compatibility.

    Edit:

    You can even find quite some posts in the subject.

    How can I get a child of an element with lower z-index on front of a child of another element with higher z-index?

    How to get a parent element to appear above child

    can I override z-index inheritance from parent element?

    Edit - 2:

    Depending on what you want to accomplish there is many alternative way you could get a fake "zindex" behaviour, one thing worth looking at is pointer-events:none; together with a transparent background you'd never know it's above the other element.

    If it's for the background, you could use box-shadows with diffrent settings to fake it.

    can't come up with any other reason atm but if you tell me i'll try to give you a solution for it aswell.

    Regards