inner absolute div sits outside parent relative div left 100%

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

inner absolute div sits outside parent relative div left 100%

问题

left: 100%的情况下,absolute定位的内部div位于relative定位的外部div之外的原因令我感到困惑。

如果有人能解释原因,最好的修复方法是让它保持在红色外部div内部。

<div
  style={{
    position: "relative",
    backgroundColor: "red",
    width: "100%",
    height: 100,
  }}
>
  <div
    style={{
      borderRadius: 10,
      backgroundColor: "green",
      height: 20,
      width: 20,
      position: "absolute",
      left: "100%"
    }}
  />
</div>

inner absolute div sits outside parent relative div left 100%

英文:

I have the following code. I am confused why at left: 100% the absolute position inner div sits outside the relative containing div.

If anyone can explain why and ideally the fix would mean it would remain within the red outer div

&lt;div
  style={{
     position: &quot;relative&quot;,
     backgroundColor: &quot;red&quot;,
     width: &quot;100%&quot;,
     height: 100,
   }}
&gt;
  &lt;div
     style={{
       borderRadius: 10,
       backgroundColor: &quot;green&quot;,
       height: 20,
       width: 20,
       position: &quot;absolute&quot;,
       left: &quot;100%&quot;
     }}
  /&gt;
&lt;/div&gt;

inner absolute div sits outside parent relative div left 100%

答案1

得分: 2

坐在父元素外部的原因是子元素具有绝对定位和left:100%的CSS属性。在CSS中,left:100%表示相对于父元素的宽度的100%,当子元素的position属性设置为absolute时。所以如果你想将子元素放在父元素内部,你必须将left属性设置为小于100%减去子元素的宽度。
https://www.w3schools.com/css/css_positioning.asp
请查看此链接以获取更多详细信息。

英文:

The reason of sitting outside of parent div is child div has absolute position and left:100% css property now. In css, left:100% means 100% of the relative div's width when child div's position property set as absolute. So if you want to set the child div inside the parent div, you have to set left property less than 100%-child's width.
https://www.w3schools.com/css/css_positioning.asp
please check this link for more detail.

答案2

得分: 0

你的绿色 <div> 超出其父元素边界的原因是它的位置是绝对定位,相对于红色 <div> 它的父元素以及它的最近定位祖先(即最近的不是静态定位的祖先元素),并且它具有 CSS 属性 left: "100%",将它移动到其父元素的左侧100%,使其位于其边界之外。

为了阻止绿色 <div> 超出其父元素,你可以采取以下两种方法之一:

  1. 使用 transform: translateX(-100%) 将绿色 <div>/盒子向左移动其宽度的100%。
<div
  style={{
     position: "relative",
     backgroundColor: "red",
     width: "100%",
     height: 100,
   }}
>
  <div
     style={{
       borderRadius: 10,
       backgroundColor: "green",
       height: 20,
       width: 20,
       position: "absolute",
       left: "100%",
       transform: `translateX(-100%)`,
     }}
  />
</div>
  1. 使用 right: 0 将元素定位在其父元素的右侧,而不是使用 left: 100%
<div
  style={{
     position: "relative",
     backgroundColor: "red",
     width: "100%",
     height: 100,
   }}
>
  <div
     style={{
       borderRadius: 10,
       backgroundColor: "green",
       height: 20,
       width: 20,
       position: "absolute",
       right: "0",
     }}
  />
</div>
英文:

The reason your green div went outside of it's parent border is because it's positioned absolute in relative to the red div which is it's parent and it's nearest positioned ancestor (i.e., the nearest ancestor that is not static)
and it has the CSS property left: &quot;100%&quot; which moves it 100% to the left of it's parent and puts it right outside of it's border.

to stop the green div from going outside it's parent you can do one of these two:

  1. Shift the green div/box 100% of it's width using transform: translateX(-100%)
&lt;div
  style={{
     position: &quot;relative&quot;,
     backgroundColor: &quot;red&quot;,
     width: &quot;100%&quot;,
     height: 100,
   }}
&gt;
  &lt;div
     style={{
       borderRadius: 10,
       backgroundColor: &quot;green&quot;,
       height: 20,
       width: 20,
       position: &quot;absolute&quot;,
       left: &quot;100%&quot;,
       transform: `translateX(-100%)` ,
     }}
  /&gt;
&lt;/div&gt;
  1. position the element at the right of it's parent div/box using right: 0 instead of left: 100%
&lt;div
  style={{
     position: &quot;relative&quot;,
     backgroundColor: &quot;red&quot;,
     width: &quot;100%&quot;,
     height: 100,
   }}
&gt;
  &lt;div
     style={{
       borderRadius: 10,
       backgroundColor: &quot;green&quot;,
       height: 20,
       width: 20,
       position: &quot;absolute&quot;,
       right: &quot;0&quot;,
     }}
  /&gt;
&lt;/div&gt;

答案3

得分: 0

问题

您有两个 <div> 元素,内部和外部。我认为问题出在内部 <div> 元素上,因为它具有 <code>[position](https://developer.mozilla.org/en-US/docs/Web/CSS/position): [absolute](https://developer.mozilla.org/en-US/docs/Web/CSS/position#absolute);</code>。引用自 MDN:

元素从正常文档流中移除,并且在页面布局中不会为该元素创建任何空间。元素相对于其最近的定位祖先(如果有)或初始包含块定位。其最终位置由 toprightbottomleft 的值确定。

这里最近的定位祖先是外部的 <div> 元素,它具有 <code>position: [relative](https://developer.mozilla.org/en-US/docs/Web/CSS/position#relative);</code>

position: absolute;<code>[left](https://developer.mozilla.org/en-US/docs/Web/CSS/left): 100%;</code> 的组合使内部的 <div> 脱离正常文档流,并将其定位到其最近的定位祖先的右侧,距离等于其最近的定位祖先的宽度的 100%

这导致内部的 <div> 向右溢出并完全位于其父容器的可见区域之外,使其看起来好像在容器外部。

解决方案

解决方案是通过从包含的外部 <div> 的完整宽度中减去其自身的宽度来调整内部 <div> 的定位。

在这种情况下,可以使用以下方式:

left: calc(100% - 20px);

解决方案

另请参阅:为什么设置 right CSS 属性会将元素向左推?

英文:

Problem

You have two &lt;div&gt; elements, the inner one and the outer one. I believe the issue is because the inner &lt;div&gt; element has <code>position: absolute;</code>. Citing from MDN:

> The element is removed from the normal document flow, and no space is
> created for the element in the page layout. The element is positioned
> relative to its closest positioned ancestor (if any) or to the initial
> containing block. Its final position is determined by the values of
> top, right, bottom, and left.

The closest positioned ancestor here is the outer &lt;div&gt; element which has the <code>position: relative;</code>.

The combination of position: absolute; and <code>left: 100%;</code> takes the inner &lt;div&gt; out of the normal document flow and positions it to the right by a distance equal to 100% of the width of its closest positioned ancestor, which is the outer &lt;div&gt;.

This causes the inner &lt;div&gt; to overflow to the right and be completely outside the visible area of its parent container, making it appear as if it's sitting outside the container.

Problem

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;div style=&quot;position: relative; background-color: red; width: 100%; height: 100px;&quot;&gt;
   &lt;div style=&quot;border-radius: 10; background-color: green; height: 20px; width: 20px; position: absolute; left: 100%;&quot;&gt;
      Hello
   &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Solution

The solution would be to adjust the positioning of the inner &lt;div&gt; by subtracting its own width from the full width of its containing outer &lt;div&gt;.

In this case, it'll be:

left: calc(100% - 20px);

Solution

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;div style=&quot;position: relative; background-color: red; width: 100%; height: 100px;&quot;&gt;
   &lt;div style=&quot;border-radius: 10px; background-color: green; height: 20px; width: 20px; position: absolute; left: calc(100% - 20px);&quot;&gt;
      Hello
   &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Also, see: Why does setting the right CSS attribute push an element to the left?

huangapple
  • 本文由 发表于 2023年7月28日 01:18:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782104.html
匿名

发表评论

匿名网友

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

确定