0

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>

enter image description here

peter flanagan
  • 9,195
  • 26
  • 73
  • 127
  • 1
    *Yes*, that happens, what do you want to do exactly? Do you want to solve the problem (what's your expected output?) or do you want to know the reason? It's kinda related to having `position: absolute;`. – Sally loves Lightning Jul 27 '23 at 17:31
  • @SallylovesLightning I guess I want to know the reason, and ideally the fix would mean it would remain within the red outer div. Updated the question – peter flanagan Jul 27 '23 at 17:36
  • The `left: 100%` CSS statement moves it to the right of the container. If you use `left: 0%` it will position the nested div into the upper-left corner. – Michael Abeln Jul 27 '23 at 17:38
  • I don't think the `reactjs` tag is required. It's more of a CSS thing. – Sally loves Lightning Jul 27 '23 at 18:24

3 Answers3

2

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.

Pro Dev
  • 71
  • 3
  • It's not recommended to use W3schools, it's better to use [MDN](https://developer.mozilla.org), they are pretty much up to date. – Sally loves Lightning Jul 27 '23 at 17:41
  • 1
    https://github.com/mdn/yari/issues/9208 – peter flanagan Jul 27 '23 at 17:46
  • @SallylovesLightning If W3Schools is up to date, there's no reason not to use it as a resource, I've used it to get started on a ton of projects when I was still learning the basics and it's never once let me down. – Borisonekenobi Jul 27 '23 at 17:47
  • Well, it's agreed that MDN is pretty much up to date, compared to W3schools, I'm not really rasing a debate just making sure everything is up to date. – Sally loves Lightning Jul 27 '23 at 17:48
  • The answer above *is* up to date, and the link is also up to date as of now. If in the future the link is no longer up to date, that would be a good time to edit/comment on the answer to provide an update for any future viewers – Borisonekenobi Jul 27 '23 at 17:51
0

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:

  1. 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>
  1. position the element at the right of it's parent div/box using right: 0 instead of 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>
Dhia
  • 328
  • 2
  • 18
0

Problem

You have two <div> elements, the inner one and the outer one. I believe the issue is because the inner <div> element has position: absolute;. 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 position: relative;.

The combination of position: absolute; and left: 100%; 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

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

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

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

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