如何让DIV始终浮在屏幕的右上角,这样即使我向下滚动页面,DIV仍会显示在同一个固定位置?感谢。
答案 0 :(得分:77)
使用position: fixed
,并将其锚定到页面的top
和right
两侧:
#fixed-div {
position: fixed;
top: 1em;
right: 1em;
}
但是, IE6不支持position: fixed
。如果您在IE6中需要此功能,this purely-CSS solution似乎可以解决问题。您需要一个包装器<div>
来包含一些可以使用的样式,如stylesheet中所示。
答案 1 :(得分:11)
使用position:fixed
,如前所述,IE6无法识别position:fixed
,但使用某些css魔法可以让IE6表现出来:
html, body {
height: 100%;
overflow:auto;
}
body #fixedElement {
position:fixed !important;
position: absolute; /*ie6 */
bottom: 0;
}
!important
标志使得您不必对IE使用条件注释。这将在所有浏览器中#fixedElement
使用position:fixed
,但IE,IE
,position:absolute
将在bottom:0
生效。这将模拟IE6的position:fixed