CSSのみが必要です。次のコードは、高さ 100 ピクセルのヘッダー、高さ 50 ピクセルのフッター、およびページの残りの部分を埋める「コンテンツ」という名前の div を中央に作成します。全体がビューポート内の利用可能なすべてのスペースを占有します。ブラウザ ウィンドウのサイズを変更すると、それに応じてページが拡大/縮小されます。
「コンテンツ」divに十分なものがある場合、その中にスクロールバーが表示されます。スクロールバーはヘッダーまたはフッターのどの部分もカバーせず、「コンテンツ」div 内になります。
<div id="header"></div>
<div id="content"></div>
<div id="footer"></div>
div#header {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 100px;
}
div#content {
position: fixed;
top: 100px;
left: 0;
right: 0;
bottom: 50px;
overflow: auto;
}
div#footer {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 50px;
}
「左」と「右」を別々に設定すると、利用可能なスペースの特定の量だけを使用することができます。
div#header {
position: fixed;
top: 0;
left: 20%;
right: 20%;
height: 100px;
}
div#content {
position: fixed;
top: 100px;
left: 20%;
right: 20%;
bottom: 50px;
overflow: auto;
}
div#footer {
position: fixed;
bottom: 0;
left: 20%;
right: 20%;
height: 50px;
}
上記の CSS を使用すると、全体が中央に配置され、水平方向に使用可能なスペースの 40% が空のままになります。