position:fixed when left/top/right/bottom aren't specified - desired results in FF/IE, but not in Safari

后端 未结 3 2092
梦毁少年i
梦毁少年i 2020-12-01 10:31

Sorry to have to ask yet another position:fixed related question, but reading through various other questions and forum threads hasn\'t helped me with this one.

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 11:25

    I believe the answer you're looking for is as follows...

    The position of an element does not default to 0,0. It's position is set by default relative to the containing element, therefor in your example, "#container-1" has a padding-left: 20px whereas the rest of the padding is set to 0 making the "default" position of the element 20px left of the wall of #container-1 and the top is 0px from the top of #container-1.

    The default position of this element by default and my viewport not scrolled would be 63px from the top and the left is obviously dependent on the width of your browser. However, if you do not override the top and left, they are already defined by the rendering engine as the top:63px, left:893px.

    Then on window resize, that position is adjusted to reflect the position based on the viewport so as you scroll down the position is changed to keep it fixed.

    So, by simply adding "position:fixed;" your properties would (as far as the browser is concerned) be as follows:

    position:fixed;
    top:63px; // defined by default during browser rendering in some browsers (based on scroll position on load)
    left:893px; // defined by default during browser rendering in some browsers (based on scroll position / width of browser at time of load)
    

    Also, IE6 and the likes, do not support position:fixed at all. http://fiddle.jshell.net/uaE4g/4/show/

    I hope this helps!

提交回复
热议问题