英文:
absolute positioning in a relative div overlaps the following div
问题
I can't understand why a div
positioned as absolute
inside a relative
div
overlaps other elements. As far as I can understand, its position should be fixed within its relative
div
container and encompassed by it, shouldn't it?
My attempt:
#content {
position: relative;
}
#overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
background-image: url(https://picsum.photos/800);
background-size: cover;
}
<div id="before">
some div
</div>
<div id="content">
<div id="overlay">
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
</div>
</div>
<div id="after">
other div
</div>
I wonder why #content
, positioned as relative
, overlaps #after
.
英文:
I can't understand why a div
positioned as absolute
inside a relative
div
overlaps other elements. As far as I can understand, its position should be fix in its relative
div
container and encompassed in it, shouldn't it?
My attempt:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
#content {
position: relative;
}
#overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
background-image: url(https://picsum.photos/800);
background-size: cover;
}
<!-- language: lang-html -->
<div id="before">
some div
</div>
<div id="content">
<div id="overlay">
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
</div>
</div>
<div id="after">
other div
</div>
<!-- end snippet -->
I wonder why #content
, positioned as relative
, overlaps #after
.
答案1
得分: 1
来自MDN文档的绝对定位,
元素从正常文档流中移除,页面布局中不为该元素创建空间。它是相对定位于最近的已定位祖先(如果有的话); 否则,它相对于初始的包含块放置。其最终位置由
top
、right
、bottom
和left
的值确定。当
z-index
的值不是auto
时,该值会创建一个新的层叠上下文。绝对定位的盒子的外边距不会与其他外边距发生折叠。
如果要保持流程,那么需要从父级中移除 position: relative
。
通常,如果您希望子元素相对于其当前位置定位,而不更改其周围的布局,则在子元素上应用 position: relative
。
应用 position: absolute
会将子元素放置在相对于父元素位置,从而改变其周围的布局。
英文:
Absolute positioning from MDN docs,
> The element is removed from the normal document flow, and no space is
> created for the element in the page layout. It is positioned relative
> to its closest positioned ancestor, if any; otherwise, it is placed
> relative to the initial containing block. Its final position is
> determined by the values of top
, right
, bottom
, and left
.
>
> This value creates a new stacking context when the value of z-index
is
> not auto
. The margins of absolutely positioned boxes do not collapse
> with other margins.
If you want to maintain the flow then you need to remove position: relative
from the parent.
Generally if you want child elements to be positioned relative to its current position without changing the layout around it, you apply position: relative
on child.
Applying position: absolute
places the child relative to the parent's position, changing the layout around it.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
#content {
background: red;
}
#overlay {
position: relative;
top: 200px;
left: 0;
width: 100%;
background-image: url(https://picsum.photos/800);
background-size: cover;
}
<!-- language: lang-html -->
<div id="before">
some div
</div>
<div id="content">
<div id="overlay">
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
<div>Some text.</div>
</div>
</div>
<div id="after">
other div
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论