CSS 绝对位置比固定位置具有更大的 z-index
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16315125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Position absolute has greater z-index than position fixed
提问by dinodsaurus
I have a poblem with an element that is positioned relative. The problem is that I have a header with position fixed and content which is positioned relative. If I scroll down the content the element is put in front of the header. I tried with z-index but I just can't get it to work. I have put z-index:999
on header.
我有一个相对定位的元素的问题。问题是我有一个位置固定的标题和相对定位的内容。如果我向下滚动内容,则该元素将放在标题前面。我尝试使用 z-index,但我无法让它工作。我已经把头z-index:999
。
Here you can see my jsFiddle
在这里你可以看到我的jsFiddle
Here is a picture:
这是一张图片:
回答by Kevin Bowersox
The z-index
on the relative positioned element should be lower than the z-index
on the fixed position element. Here is a quick example:
的z-index
相对定位的元件上应比下z-index
固定位置元件上。这是一个快速示例:
HTML
HTML
<div id="fixed">Fixed Position</div>
<div id="relative">Relative Position</div>
CSS
CSS
body{
height: 3000px;
}
#fixed{
top: 0;
height; 100px;
background: green;
width: 100%;
position: fixed;
z-index: 2;
}
#relative{
position: relative;
top: 100px;
left: 50px;
height: 100px;
width: 100px;
background: red;
z-index: 1;
}
Working Example:http://jsfiddle.net/XZ4tM/1/
工作示例:http : //jsfiddle.net/XZ4tM/1/
Fixing Your Example
修正你的例子
The header styling has an issue, there are two colons ::
proceeding the z-index properties value.
标题样式有问题,::
z-index 属性值前有两个冒号。
.header{
width:960px;
background: #43484A;
height:80px;
position: fixed;
top:0;
z-index: 9999; /* Removed extra : here */
}
Fixed Examplehttp://jsfiddle.net/kUW66/2/
回答by sanman
What I think you did is correct that using z-index in only a option. I have some work for you to understand.
我认为您所做的是正确的,仅在一个选项中使用 z-index。我有一些工作要你理解。
Please follow the JS Fiddle link
请按照JS Fiddle 链接
HTML
HTML
<div id="header">Header</div>
<div id="content1"><div id="content2"></div></div>
CSS
CSS
body{
margin:0px auto;
color:#FFF;
}
#header{
background-color:#006666;
width:100%;
height:50px;
position:fixed;
text-align:center;
font:bold 12px Arial, Helvetica, sans-serif;
line-height:50px;
display:block;
z-index:10;
}
#content1{
width:70%;
height:1200px;
margin:0px auto;
background-color:#FFFF66;
position:relative;
top:50px;
z-index:9;
}
#content2{
width:50px;
height:250px;
margin:0px auto;
background-color:#F60;
postition:absolute;
left:50px;
top:50px;
}
Hope that helps.
希望有帮助。
回答by brunoais
"Content" is relative to the window, not the grey square.
“内容”相对于窗口,而不是灰色方块。
Did you try to make that grey square position:relative
?
你有没有尝试制作那个灰色方块position:relative
?
Without the HTML and the CSS, it's really hard to know the real cause.
没有 HTML 和 CSS,很难知道真正的原因。