绝对定位在相对定位的 div 中会重叠下一个 div。

huangapple go评论56阅读模式
英文:

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 -->

&lt;div id=&quot;before&quot;&gt;
  some div
&lt;/div&gt;
&lt;div id=&quot;content&quot;&gt;
  &lt;div id=&quot;overlay&quot;&gt;
    &lt;div&gt;Some text.&lt;/div&gt;
    &lt;div&gt;Some text.&lt;/div&gt;
    &lt;div&gt;Some text.&lt;/div&gt;
    &lt;div&gt;Some text.&lt;/div&gt;
    &lt;div&gt;Some text.&lt;/div&gt;
    &lt;div&gt;Some text.&lt;/div&gt;
    &lt;div&gt;Some text.&lt;/div&gt;
    &lt;div&gt;Some text.&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;div id=&quot;after&quot;&gt;
  other div
&lt;/div&gt;

<!-- end snippet -->

I wonder why #content, positioned as relative, overlaps #after.

答案1

得分: 1

来自MDN文档的绝对定位,

元素从正常文档流中移除,页面布局中不为该元素创建空间。它是相对定位于最近的已定位祖先(如果有的话); 否则,它相对于初始的包含块放置。其最终位置由 toprightbottomleft 的值确定。

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 -->

&lt;div id=&quot;before&quot;&gt;
  some div
&lt;/div&gt;
&lt;div id=&quot;content&quot;&gt;
  &lt;div id=&quot;overlay&quot;&gt;
    &lt;div&gt;Some text.&lt;/div&gt;
    &lt;div&gt;Some text.&lt;/div&gt;
    &lt;div&gt;Some text.&lt;/div&gt;
    &lt;div&gt;Some text.&lt;/div&gt;
    &lt;div&gt;Some text.&lt;/div&gt;
    &lt;div&gt;Some text.&lt;/div&gt;
    &lt;div&gt;Some text.&lt;/div&gt;
    &lt;div&gt;Some text.&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;div id=&quot;after&quot;&gt;
  other div
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月30日 13:06:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定