Relative position and ownership

I always thought setting the div to relative, and the right 0 would put the div all the way to the right if its parent was 100% of the width. Apparently, I am mistaken, and only absolute work is similar to this. So there is no way to make it work with relative?

+6
source share
3 answers

You must set the parent as relative and the child as absolute positioning.

.parent{ position: relative; width: 100%; } .right{ position: absolute; width: 200px; height: 200px; background: red; top:0; right:0; } 

Like here: http://jsfiddle.net/willemvb/n9Vrv/

+14
source

There is a way to get him to work with a relative.

One way is to first set the display parent to inline-flex .

Then set the element (child) position:relative; margin-left:auto; right:0; position:relative; margin-left:auto; right:0; .

+3
source

So there is no way to make it work with relative?

Right. Relative positioning is the offset of a position from where it would be with static positioning.

You need absolute positioning relative to the edges of the containing block.

+1
source

Source: https://habr.com/ru/post/927862/


All Articles