html - 固定位置 div 出现在同级 div 位于 margin-top 而不是顶部的位置

标签 html css

此处演示:http://codepen.io/geoyws/pen/zqobWe

app-header {
  position: fixed;
  /*top: 0px;*/
  /* Without this, app-header appears after the margin of his sibling below. Intuitively you would think that he would ignore his sibling's margin-top and stick at the top without having to state top: 0px;*/
  width: 100%;
  height: 25px;
  background: red;
}
[b] {
  display: block;
}
another-margin {
  margin-top: 100px;
  width: 100%;
  height: 50px;
  background: blue;
}
margin-generator {
  margin-top: 55px;
  width: 100%;
  height: 50px;
  background: green;
}
<div>
  <app-header b>HEADER</app-header>
  <another-margin b></another-margin>
  <margin-generator b></margin-generator>
</div>

谁能解释这种行为?根据 sibling 的 margin-top 影响固定定位元素对我来说似乎一点也不直观。

最佳答案

我找不到任何关于它的引用资料,除了一个小例子 here在示例 15 中,但根据经验,我可以保证 absolutefixed 定位元素保持其正常位置,除非通过 top right< 设置 底部左侧

这是一个例子:

.abs {
  position: relative;
  width: 50px;
  height: 50px;
  background: red;
}
.abs-2 {
  position: fixed;
  left: 60px;
  background: blue;
}
.abs-4 {
  position: absolute;
  left: 60px;
  background: green;
}
.abs-6 {
  position: fixed;
  background: yellow;
}
<div class="abs abs-1"></div>
<div class="abs abs-2"></div>
<div class="abs abs-3"></div>
<div class="abs abs-4"></div>
<div class="abs abs-5"></div>
<div class="abs abs-6"></div>

非红色元素是 absolutefixed 定位在 left 属性集中的两个集合中。

关于html - 固定位置 div 出现在同级 div 位于 margin-top 而不是顶部的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36026101/

相关文章:

html - Safari 不尊重边距

html - CSS 行高和字体大小 : shrink relatively to the container?

html - css div定位与 float

css - 用CSS3绘制45度 Angular

html - CSS 菜单不适用于手机或平板电脑

jquery mobile center 在页脚中内联两个按钮

html - CSS - 围绕 div 组的边框

javascript - 有没有办法将javascript文件加载到一个文件到页面以优化站点速度?

jquery - jQuery 如何处理评论元素?

html - "display:none"是否使控件无效?