两个 div 彼此重叠,每个 div 占据浏览器屏幕的整个高度(和宽度)



我的页面中有两个div,我希望将其宽度和高度都设置为浏览器的宽度和高度,因此我必须向下滚动才能看到第二个div。

这怎么可能?

Eidt:我忘了提,我也希望这些div中的内容垂直居中。

到目前为止,我所做的是获取此链接的第一个答案,并将该代码放入另一个div中,该div占用100%的高度。

这是我目前所拥有的:

.HTML:

<body>
    <div id="first"><div class="outer"><div class="middle"><div class="inner"><h1>Title 1</h1></div></div></div></div>
    <div id="second"><div class="outer"><div class="middle"><div class="inner"><h1>DTitle 2</h1></div></div></div></div>
</body>

.CSS:

.outer {
display: table;
position: absolute;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.inner {
margin-left: auto;
margin-right: auto;
width: 100%;
}
#first, #second {
height: 100%;
}
h1 {
text-align: center;
}

你可以执行以下操作:

.HTML

html, body { height:100%; }
.box { height:100%; }
.one { background:#eee; }
.two {  background:#ccc; }

.CSS

<div class="box one">1</div>
<div class="box two">2</div>

JSFiddle Demo

最新更新