Is position:absolute always counted from the top of the page?

时间:2015-06-26 09:34:04

标签: html css

If I make element inside and give it properties position:absolute;top:10px; it will count the 10px from the top of the page and not inside the .

Is this normal or should I use instead position:relative; or something?

Thanks for all answers!

2 个答案:

答案 0 :(得分:1)

Absolute is not always related to the border of your page. If you take this as example:

<div class="box1" style="position:relative;">
  <div class="box2" style="position:absolute;top:5;"></div>
</div>

The position of box2 would be dependent on the position of box1.

答案 1 :(得分:0)

Position relative is used to tell the inner block where to start count from. So if don't wrap your block that you gave him position absolute with another block who have position relative it will count from the top of the page. You can read about it: https://developer.mozilla.org/en-US/docs/Web/CSS/position