简体   繁体   English

单独元素的余量如何影响固定元素的位置?

[英]How does margin of separate element affect position of fixed element?

I have two separate DIV s one position : fixed for notification bar, and one for main container, when I give the container some margin-top , this also pushes my notification bar! 我有两个单独的DIV一个position : fixed为通知栏,一个为主容器,当我给container一些margin-top ,这也会推送我的通知栏!

Here is a fiddle: http://jsfiddle.net/Zh9k8/2/ 这是一个小提琴: http//jsfiddle.net/Zh9k8/2/

and, I know I can stick it to top of the page with top : 0 but I want to know why does it happen. 而且,我知道我可以将它贴在页面的top : 0但我想知道它为什么会发生。

Common issue caused by collapsing margins: http://www.w3.org/TR/CSS21/box.html#collapsing-margins 折扣利润导致的常见问题: http//www.w3.org/TR/CSS21/box.html#collapsing-margins

The fix is to not use a margin, but create space another way. 修复是不使用边距,而是以另一种方式创建空间。 You could add top padding to .container and remove the margin: 您可以将顶部填充添加到.container并删除边距:

http://jsfiddle.net/Zh9k8/4/ http://jsfiddle.net/Zh9k8/4/

The top margin of an in-flow block element collapses with its first in-flow block-level child's top margin if the element has no top border, no top padding, and the child has no clearance. 如果元素没有顶部边框,没有顶部填充,并且子节点没有间隙,则插入块元素的上边距将与其第一个流入块级子节点的上边距折叠。

If the element's margins are collapsed with its parent's top margin, the top border edge of the box is defined to be the same as the parent's. 如果元素的边距与其父元素的上边距折叠,则框的上边框边缘定义为与父元素相同。

Very confusing lingo, but it describe's exactly what is happening. 非常令人困惑的术语,但它描述的正是发生了什么。

You can still use margins to create space. 您仍然可以使用边距来创建空间。 And if you set minimal padding on the body it will eliminate the margin collapse with the fixed block. 如果在body上设置最小填充,它将消除固定块的边缘崩溃。 See updated fiddle: http://jsfiddle.net/BMCc9/ . 请参阅更新的小提琴: http//jsfiddle.net/BMCc9/

body {
    padding: 0.1px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM