【发布时间】:2012-03-08 21:20:42
【问题描述】:
我想在一个固定位置设置一个顶部标题并在其下方滚动内容,但遇到了一些奇怪的问题。如果我为内容 div 设置了一个 margin-top,即使它设置为 position:fixed,margin 也会影响标题并将其向下移动。我通过将内容 div 设置为 position:relative 并使用 top: 将其偏移相同的数量找到了一种解决方法,但我觉得奇怪的是非嵌套 div 会影响固定位置的元素并且想知道它为什么会发生.
我在 Safari、Firefox 和 Chrome 中得到了同样的结果。在 Opera 中,margin 会压低内容并将标题留在原处,这是我所期望的,但与其他结果相比,可能是 Opera 出错了。我所说的可以在this JSFIDDLE 中看到(不要使用 Opera!:))。
代码如下:
css:
body {
background:#FFD;
}
#header {
position:fixed;
height:15px;
width:100%;
text-align:center;
border-bottom:1mm dashed #7F7F7F;
background-color:#EEE;
}
#content {
width:90%;
margin-top:25px;
margin-left:auto;
margin-right:auto;
background-color:#E5E5FF;
border: 1px solid #000;
}
html:
<body>
<div id="header">
HEADER
</div>
<div id="content">
<p>Text1</p>
<p>Text2</p>
<p>Text3</p>
<p>Text4</p>
</div>
</body>
【问题讨论】: