2012-05-30 32 views
2

我似乎無法找出一種方法來分配更大的z-index優先級的元素具有比overlay元素更低的z-index的父級。更低z-索引內的z-index?

考慮這個例子:

<div style="width: 100px; height: 100px; background: #F00; position: absolute; left: 0; top: 0; z-index: 1;"> 
    <div style="width: 50px; height: 100px; background: #00F; position: absolute; left: 50px; top: 0; z-index: 3;"></div> 
</div> 
<div style="width: 100px; height: 50px; background: #0F0; position: absolute; left: 0; top: 50px; z-index: 2;"></div> 

結果:

enter image description here

預期的結果:

enter image description here

+0

結果是預期結果。據我所知,一旦你給它一個「職位」,而不是文件,z-索引是相對於父親的。 – LeeR

+0

您可以對第一個div使用「position:absolute; z-index:auto」。 – starikovs

+0

@infous不是一個解決方案,因爲該元素的「z-index」很重要。只需將元素移到外面。 – Gajus

回答

4

你可以這樣做,但僅在父項的屬性上沒有position: relativeabsolute屬性。因此,如果您從第一個div中刪除position: absolute,它將按預期工作。

的jsfiddle:http://jsfiddle.net/GEnHC/

+0

只要您沒有對創建堆棧上下文的元素執行任何其他操作(如轉換) –

1

元素,會下令對每個級別第一。所以它訂購你的紅色和綠色的盒子,然後在裏面訂購你的藍色盒子紅色盒子,它已經被確定在綠色盒子後面。這是沒有辦法的。你不得不考慮你的元素的替代佈局,這將允許你想要的佈局,比如簡單的removing the blue box from its parent