英文:
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>
英文:
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
<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>
答案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>
超出其父元素,你可以采取以下两种方法之一:
- 使用
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>
- 使用
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: "100%"
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:
- Shift the green div/box 100% of it's width using
transform: translateX(-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>
- position the element at the right of it's parent div/box using
right: 0
instead ofleft: 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>
答案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:
元素从正常文档流中移除,并且在页面布局中不会为该元素创建任何空间。元素相对于其最近的定位祖先(如果有)或初始包含块定位。其最终位置由
top
、right
、bottom
和left
的值确定。
这里最近的定位祖先是外部的 <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 <div>
elements, the inner one and the outer one. I believe the issue is because the inner <div>
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 <div>
element which has the <code>position: relative;</code>.
The combination of position: absolute;
and <code>left: 100%;</code> takes the inner <div>
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 <div>
.
This causes the inner <div>
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 -->
<div style="position: relative; background-color: red; width: 100%; height: 100px;">
<div style="border-radius: 10; background-color: green; height: 20px; width: 20px; position: absolute; left: 100%;">
Hello
</div>
</div>
<!-- end snippet -->
Solution
The solution would be to adjust the positioning of the inner <div>
by subtracting its own width from the full width of its containing outer <div>
.
In this case, it'll be:
left: calc(100% - 20px);
Solution
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<div style="position: relative; background-color: red; width: 100%; height: 100px;">
<div style="border-radius: 10px; background-color: green; height: 20px; width: 20px; position: absolute; left: calc(100% - 20px);">
Hello
</div>
</div>
<!-- end snippet -->
Also, see: Why does setting the right
CSS attribute push an element to the left?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论