Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

position relative and right property

Tags:

html

css

I always thought by setting the div to relative and right 0 would position the div all the way to the right, if its parent was 100% width. Apparently I'm wrong and only absolute works like that. So is there no way to make it work with relative?

like image 289
Chapsterj Avatar asked Oct 16 '12 16:10

Chapsterj


3 Answers

You have to set the parent to be relative, and it's child to 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/

like image 119
Willem Van Bockstal Avatar answered Nov 07 '22 12:11

Willem Van Bockstal


There's a way to make it work to a relative.

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

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

like image 26
George Avatar answered Nov 07 '22 11:11

George


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

Correct. Relative positioning is the position offset from where it would be with static positioning.

You need absolute positioning to position with respect to the edges of the containing block.

like image 1
Quentin Avatar answered Nov 07 '22 10:11

Quentin