2011-11-08 41 views
4

我有這樣的風格:z-index的不尊重

header{ 
    width:400px; 
    min-height:640px; 
    background:#9B9B76; 
    float:left; 
    z-index:1000; /*high*/ 
} 
nav{ 
    width:100%; 
    z-index:1001; /*highest*/ 
} 

nav a{ 
    float:left; 
    width:80%; 
    padding:10%; 
    font-size:20px; 
} 
nav a:hover{ 
    background:#997548; 
} 
.LevelCnt_1{ 
    float:left; 
    width:300px; 
    z-index:1;  /*Lowest*/ 
    position:absolute; 
    left:400px; 
    background:#baafa9; 
} 

的問題是,在視覺上.LevelCnt_1停留在頭頂部,如果我設置left:0px

爲什麼會出現這種情況?

+1

嘗試把'位置:relative'到頁眉 – Alex

+0

的z-index只有當元素的定位工作。 TRy向CSS添加位置聲明並查看會發生什麼。 – Kyle

回答

12

z-index只能與position relative, absolute & fixed一起使用。所以,給你的header & nav DIV。

像這樣

header{ 
    width:400px; 
    min-height:640px; 
    background:#9B9B76; 
    float:left; 
    z-index:1000; /*high*/ 
position:relative 
} 
nav{ 
    width:100%; 
    z-index:1001; /*highest*/ 
position:relative 
} 
0

z-index僅適用於絕對位置。一個浮點數也是這個錯誤的盒子上下文。

+0

不正確 - 它也適用於'相對'和'固定'的位置... – BenM

+0

好的......請原諒我的立場,這是傾銷!但是float設置了一個不同的box-context,它會覆蓋位置相關的上下文。所以我的意思是,兩者不能同時在一起工作。這是你必須清理浮動的一個原因。 – philipp

+0

仍然沒有相關的zIndex –