我有以下html:
<div class="fix-to-top">
<div class="background-image"></div>
</div>
我希望能够将图像的位置固定在页面顶部 - 这样无论用户向下滚动多远,它始终位于页面顶部。此外,我希望图像始终位于页面中心,因此如果用户调整浏览器大小,图像将保持在中心。这是我尝试过的,但没有得到结果:
.fix-to-top {
position: fixed;
top: 0px;
width: 2000px;
}
.fix-to-top .background-image {
margin: 0 auto;
}
但是,侧边距并没有"自动"。我将如何正确执行此操作?
img {
position: fixed;
right: 50%;
width: 100px;
height: 100px;
margin-top: -50px;
margin-right: -50px;
background: orange;
top: 50%;
}
http://jsfiddle.net/jXdxr/1/
检查这个小提琴
无需使用两个div
您可以使用background-attachment
和background-postion
属性来实现它