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