#box1{
width: 200px;
height: 200px;
background-color: olive;
position: absolute;
z-index: 1;
right: 0;
}
#box2 {
width:100px;
height: 100px;
background-color: orange;
position: absolute;
z-index: 2;
right: 0 ;
}
Instead of using negative margins or complex adjustments, you can simply modify your current CSS to resolve the issue. I tested it with my code and it worked perfectly. This solution is ideal for your situation.
When layering elements, make sure to use either position: absolute or position: fixed (both serve similar purposes in this scenario).
By using position: absolute (or fixed), you can align one or more edges of each div using properties like top, right, bottom, and left. While these are not required, setting at least one will fix that edge to its specified pixel position within the containing div.
If you place these two divs within the body tag or ensure they do not need to extend beyond their outer container, setting "right: 0;" for each div will position them similarly to using float: right for relatively positioned divs (as in your original code). Since they are absolutely positioned, they can overlap while occupying the same space.
Lastly, you can control which div appears on top by adjusting the z-index value.
Cheers! 😄