这是一种解决方案,用于具有固定高度的标题。
您的 HTML 需要具有以下模式:
<div class="header">Header</div>
<div class="main-wrap">
<div class="container">
<div class="content">Content...</div>
</div>
</div>
<div class="footer">Footer</div>
和CSS:
html, body {
height: 100%;
}
body {
margin: 0;
}
.header {
height: 50px;
background-color: beige;
}
.main-wrap {
position: absolute;
top: 50px;
bottom: 50px;
width: 100%;
background-color: silver;
overflow: auto;
}
.container {
display: table;
height: 100%;
width: 100%;
}
.content {
border: 1px solid blue;
display: table-cell;
height: 100%;
width: 100%;
vertical-align: middle;
}
.footer {
height: 50px;
width: 100%;
background-color: lightblue;
position: fixed;
bottom: 0;
}
参见演示:http: //jsfiddle.net/audetwebdesign/aGTKs/
可以将其调整为灵活高度的标题。
.main-wrap
容器定义页眉和页脚之间的空间。
该.container
块使用display: table
并继承高度.main-wrap
。
最后,.content
uses display: table-cell
,它允许您使用vertical-align: middle
(默认值)将内容垂直居中。
您需要将body
和的高度设置html
为 100% 以捕获视口的高度。