在HTML页面中,如果我将某些<div>与“ right:0px”对齐,则它们看起来都很好,正如我期望的那样。但是,如果我缩小浏览器窗口并显示水平滚动条,则当我向右滚动页面时,会看到意外的空白(而不是<div>的背景色)。我的<div>似乎相对于页面的可见区域对齐了。请参见下面的示例代码:

<html>
    <head>
        <style>
        <!--
        #parent {
            position: absolute;
            left: 0px;
            right: 0px;
            top: 0px;
            bottom: 0px;
            background-color: yellow;
        }

        #child {
            position: absolute;
            left: 100px;
            top: 300px;
            width: 1000px;
            height: 400px;
            background-color: blue;
        }
        -->
        </style>
    </head>
    <body>
        <div id="parent"><div id="child">some text here</div></div>
    </body>
</html>


有什么方法可以使“ right:0px”属性相对于整个页面的大小(不仅是可见区域)对齐控件吗?

谢谢。

最佳答案

问题是父元素中的“绝对”位置,因为它可以按定义滚动。

如果将位置设置为“固定”,并且将其他属性设置为“滚动”,则它应该看起来像预期的那样。

#parent {position: fixed;
         overflow: scroll;
         left: 0px;
         top: 0px;
         right: 0px;
         bottom: 0px;
         background-color: yellow;
        }

07-28 02:26
查看更多