有没有一种方法可以将内部 div 的绝对位置固定在页面顶部,忽略父元素的相对位置?
不行,除非你将其重新放置在DOM中。
(如果您希望它与窗口相关而不是文档相关,则可以使用position:fixed
作为替代方案)
您可以使用 position: absolute;
和负值:
HTML:
<div id="parent">
<div id="child">
The child's content
</div>
</div>
CSS:
#parent
{
position: relative;
top: 200px;
border: 1px dashed #f00;
height: 50px;
width: 200px;
}
#child
{
position: absolute;
top: -200px;
}
应该可以了。这里有一个例子供你参考。