我有一个我想要的 div,我将其放置在距离顶部 25% 的位置。但是,这 25% 是根据背景图像的大小计算的,而不是根据可见屏幕的大小计算的。如何解决?
更新:现在顶部边距有效,但左侧无效:(
有什么线索吗?
body {
background: #eeeeee url('pix/bg-noether-2.jpg') no-repeat center top;
background-size: auto 100%;
background-attachment: fixed;
overflow: hidden;
align: center;
}
#container {
background-color: #ffffe4;
position: absolute;
width: 776px;
height: 400px;
top: 25%;
margin-left: auto;
text-align: center;
overflow: auto;
}
最佳答案
1) 使用绝对定位:
#myDiv { position: absolute; top: 25%; }
2) 确保你的 div 不在另一个定位元素内(如果你不确定这一点,只需将它放在 <body>
标签内,别无其他)
关于css - 与背景图片的相对位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6308850/