Html 奇怪的行为

Html 奇怪的行为,html,css,Html,Css,我有一段非常简单的代码: <body> <div style="position:fixed; width: 100%; height: 40px; background: #333"></div> <div style='margin-top:40px; border: 1px solid green'> Some contents!<br>Some contents!<br>Some contents!<br&g

我有一段非常简单的代码:

<body>
<div style="position:fixed; width: 100%; height: 40px; background: #333"></div>

<div style='margin-top:40px; border: 1px solid green'>
Some contents!<br>Some contents!<br>Some contents!<br>
</div>

</body> 

一些内容<一些内容<一些内容
但不知何故,固定div的边距为40px,与第二个div重叠!有人能指出我在这里遗漏了什么吗

JS小提琴:


谢谢

您在顶部div上设置了一个
位置:绝对
,没有
顶部:0
位置集。试试这个:

<body>
<div style="position:absolute; width: 100%; height: 40px; background: #333; top: 0"></div>

<div style='margin-top:40px; border: 1px solid green'>
Some contents!<br>Some contents!<br>Some contents!<br>
</div>

</body> 

一些内容<一些内容<一些内容

您在顶部div上设置了一个
位置:绝对
,没有
顶部:0
位置设置。试试这个:

<body>
<div style="position:absolute; width: 100%; height: 40px; background: #333; top: 0"></div>

<div style='margin-top:40px; border: 1px solid green'>
Some contents!<br>Some contents!<br>Some contents!<br>
</div>

</body> 

一些内容<一些内容<一些内容

将顶部、左侧、右侧或底部css属性添加到固定div

比如说

{
    top: 0; /*Fixes it to the top*/
}

将顶部、左侧、右侧或底部css属性添加到固定div

比如说

{
    top: 0; /*Fixes it to the top*/
}

介意解释下一票吗?这里有
位置:固定的
,在小提琴中有
位置:绝对的
?介意解释下一票吗?这里有
位置:固定的
,在小提琴中有
位置:绝对的