position relative and right property

前端 未结 3 1470
攒了一身酷
攒了一身酷 2021-01-11 18:10

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 abs

相关标签:
3条回答
  • 2021-01-11 18:26

    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/

    0 讨论(0)
  • 2021-01-11 18:27

    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.

    0 讨论(0)
  • 2021-01-11 18:28

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

    0 讨论(0)
提交回复
热议问题