我試圖用透明覆蓋層覆蓋除了一個div以外的整個頁面。問題是,我想要頂部的div是fixed
div的孩子。我如何將它放在頂層,但將其父層放在覆蓋層下面?在內部div上應用z-index以在覆蓋層上出現
在下面的代碼中,我想要覆蓋層上的元素是.holder
。這是JS Fiddle。
HTML:
<!DOCTYPE html>
<html>
<head>
<title>z-index tests</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="root">
Root
<div class="header">
Header
<div class="holder">
Holder
</div>
</div>
<div class="content">
content
<div class="box"></div>
</div>
</div>
<div class="overlay"></div>
</body>
</html>
CSS:
.box {
height: 100px;
width: 100px;
border: 1px solid #000;
}
.root {
display: block;
background-color: grey;
}
.header {
background-color: red;
position: fixed;
top: 0px;
width: 1600px;
}
.holder {
background-color: green;
height: 60px;
width: 1600px;
z-index: 1002;
position: absolute;
}
.content {
background-color: blue;
}
.overlay {
position: fixed;
top:0;
bottom:0;
left:0;
right:0;
background-color: rgb(50,50,50);
opacity:0.8;
z-index:10;
}
還是要謝謝你Eryk。我要用一種不同的方法來做我想做的事情。 [這](http://stackoverflow.com/a/28616718/1840918)將爲我的目的工作,並可能爲我節省很多頭痛! – Abdul