【发布时间】:2018-03-05 23:35:26
【问题描述】:
总结:一个绝对定位的元素在任何情况下都关心它的兄弟姐妹吗(兄弟姐妹是相对的、绝对的还是静态的)?
我的理解是绝对定位忽略同级。
.absoluteBox {
position: absolute;
width: 100%;
height: 100%;
background-color: yellow;
border: 1px solid red;
box-sizing: border-box;
top: 0;
/*
why is top 0 necessary?
If i do not set it, the inner yellow box will position itself after the grey box
The grey box has no position property assigned to it, and even if it did, it should not matter
-- because the inner-absolute box should just be looking to its ancestor for its position
*/
}
.greyBox {
display: flex;
height: 20px;
width: 50px;
background-color: grey;
}
<div class="absoluteBox">
outer yellow box
<div class="greyBox"></div>
<div class="absoluteBox">
inner yellow box should completely fill up screen
</div>
</div>
我有一个容器(“外部容器”)。绝对位置,100% x 100% 宽度和高度。它应该占据 jsFiddle 中的整个屏幕。
我将一个孩子放入“Outer-Container”,没有与位置相关的属性。 (小提琴中的“greyBox”)。
然后我将另一个 Absolute 100x100 div 放入“外部容器”中。将此称为“内部容器”。
问题:为什么 Inner-Container 将自身定位在 grayBox 之后?它应该忽略兄弟姐妹,脱离正常的定位“流程”,并在确定其位置时只查看其最近定位的祖先(外部容器)。
【问题讨论】:
-
如果
absolute元素没有位置属性(上、右、下、左),那么它将根据它在 HTML 中的位置作为普通元素落入适当位置。 -
这种行为经过深思熟虑更有意义。谢谢
-
我已经在答案中更详细地解释了这一点。
-
是否有任何解决方案解决了您的问题?
标签: css position absolute siblings