私は簡単なページを持っています。ここでは、固定ヘッダーと固定フッター、およびコンテンツの本文をスクロール可能にしたいと考えています。
HTML
<div class="container">
<div class="header">This is Header</div>
<div class="body">This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body
<br/>This is Body</div>
<div class="footer">This is Footer</div>
</div>
CSS
* {
margin: 0;
padding: 0;
}
.container {
width: 100%;
height: 100%;
background: #DDD;
}
.header {
width: 100%;
height: 40px;
background: red;
position: absolute;
top: 0;
}
.footer {
width: 100%;
height: 40px;
background: green;
position: absolute;
bottom: 0;
}
.body {
width: 100%;
background: blue;
overflow: auto;
}
ただし、スクロールすると、ページ全体がスクロールする傾向があり、ヘッダーとフッターが移動します。これは、javascript を使用して解決できます。画面の高さを取得し、ヘッダーとフッターの高さを差し引きます。
しかし、css だけを使用してこれを達成する方法はありますか。はいの場合、どのように?