-1

有人可以告诉我为什么绝对定位的红色 DIV 没有填满整个页面以及如何解决它?它只是填充视口。我已经看到了很多相同的问题,但是要么这个人的代码太乱了,难怪它不起作用,要么解决方案不起作用。

http://jsfiddle.net/YysvK/

body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
}

<div style="position: absolute; z-index: 999; background-color: #f00; left: 0px; top: 0px; right: 0px; bottom: 0px;"></div>
4

3 回答 3

2

将位置从绝对更改为固定。

<div style="position: fixed; z-index: 999; background-color: #f00; left: 0px; top: 0px; right: 0px; bottom: 0px;"></div>

jsFiddle 示例

于 2013-09-21T21:57:19.687 回答
1

您只需要具备以下条件:

position:absolute;
z-index:999;
top:0;
left:0;
width:100%;
height:100%;
background:#f00;
于 2013-09-21T22:24:42.803 回答
0

我想到了。出于某种原因,即使在 body 元素上指定了位置、宽度和高度之后,这也不起作用,而是将所有内容都包装在 body 内部的 DIV 中并将其设置为position: relative有效。

http://jsfiddle.net/k5uZ5/

我仍然不确定为什么<body>不像其他元素那样表现。如果有人能解释一下,我将不胜感激。

于 2013-09-22T20:33:44.353 回答