HTMLとCSSでフッターをページ下部に固定する方法【完全ガイド】
HTMLとCSSでフッターをページ下部に固定する方法
方法 1: position: fixed を使用する
これは、フッターをブラウザウィンドウの下部に固定する最も簡単な方法です。
HTML:
<footer>
</footer>
CSS:
footer {
position: fixed;
bottom: 0;
width: 100%;
}
この方法は、フッターの高さを固定している場合に最適です。 フッターの高さが可変の場合、この方法を使用すると、コンテンツがフッターの下に表示される可能性があります。
方法 2: position: absolute と min-height を使用する
この方法は、フッターの高さが可変する場合に役立ちます。
<div class="wrapper">
<main>
</main>
<footer>
</footer>
</div>
.wrapper {
position: relative;
min-height: 100vh;
}
footer {
position: absolute;
bottom: 0;
width: 100%;
}
この方法は、ラッパー要素に min-height: 100vh
を設定することで、コンテンツ領域がブラウザウィンドウの高さ全体を占めるようにします。 その後、フッターに position: absolute
と bottom: 0
を設定して、ラッパー要素の下部に固定します。
方法 3: Flexbox を使用する
この方法は、より新しい方法で、より柔軟なレイアウトを実現できます。
<div class="flex-container">
<main>
</main>
<footer>
</footer>
</div>
.flex-container {
display: flex;
flex-direction: column;
min-height: 100vh;
}
main {
flex: 1 0 auto;
}
footer {
flex: 0 0 auto;
}
この方法は、フレックスボックスレイアウトを使用して、コンテンツ領域とフッターを垂直方向に配置します。 flex: 1 0 auto
を main
要素に設定すると、メインコンテンツが利用可能なすべてのスペースを占有するようにします。 flex: 0 0 auto
を footer
要素に設定すると、フッターがそのコンテンツに必要なスペースのみを占有するようにします。
補足:
- 上記は基本的な方法です。 それぞれの方法には、さまざまなバリエーションとオプションがあります。
- フッターのコンテンツが長い場合、スクロールバーが表示される可能性があります。 スクロールバーのデザインを調整したい場合は、CSS で
overflow-y
プロパティを使用できます。 - レスポンシブデザインを作成する場合は、さまざまな画面サイズでフッターが正しく表示されるように CSS を調整する必要があります。
これらの方法を参考に、ご自身のWebサイトに合った方法でフッターを固定してください。
フッターをページ下部に固定するサンプルコード
方法 1: position: fixed を使用する
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>フッターを固定する</title>
<style>
footer {
position: fixed;
bottom: 0;
width: 100%;
background-color: #f0f0f0;
color: #333;
text-align: center;
padding: 10px 0;
}
</style>
</head>
<body>
<header>
<h1>フッター固定サンプル</h1>
</header>
<main>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed et quam eget ipsum ullamcorper hendrerit. Nullam id nisi sit amet elit ullamcorper semper. Donec ac diam nec lectus varius elementum. Sed mollis enim eget quam faucibus, eget tincidunt quam faucibus. Maecenas sit amet quam ac nunc tincidunt ullamcorper. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed elementum turpis eu leo tincidunt, vitae tincidunt ante molestie. Nullam vel lectus ac ipsum semper tincidunt. Sed euismod risus eget arcu semper, et malesuada lectus aliquam.</p>
<p>Donec sed odio dui. Nullam non quam vitae orci luctus ullamcorper. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed non quam vitae orci luctus ullamcorper. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed elementum turpis eu leo tincidunt, vitae tincidunt ante molestie. Nullam vel lectus ac ipsum semper tincidunt. Sed euismod risus eget arcu semper, et malesuada lectus aliquam.</p>
<p>Mauris sit amet quam ac nunc tincidunt ullamcorper. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed elementum turpis eu leo tincidunt, vitae tincidunt ante molestie. Nullam vel lectus ac ipsum semper tincidunt. Sed euismod risus eget arcu semper, et malesuada lectus aliquam. Donec sed odio dui. Nullam non quam vitae orci luctus ullamcorper. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</main>
<footer>
© 2024 フッター固定サンプル
</footer>
</body>
</html>
この例では、フッターにシンプルな背景色とテキストを追加しています。 独自のスタイルを自由にカスタマイズしてください。
方法 2: position: absolute と min-height を使用する
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>フッターを固定する</title>
<style>
.wrapper {
position: relative;
min-height: 100vh;
}
footer {
position: absolute;
bottom: 0;
width: 100%;
background-color: #f0f0f0;
color: #333;
text-align: center;
padding: 10px 0;
}
</style>
</head>
<body>
<div class="wrapper">
<header>
<h1>フッター固定サンプル</h1>
</header>
<main>
<
フッターをページ下部に固定するその他の方法
方法 4: Grid レイアウトを使用する
この方法は、より新しいCSS機能であるGrid レイアウトを使用して、フッターを固定します。
<div class="grid-container">
<header>
<h1>フッター固定サンプル</h1>
</header>
<main>
</main>
<footer>
</footer>
</div>
.grid-container {
display: grid;
grid-template-areas:
"header"
"main"
"footer";
height: 100vh;
}
header {
grid-area: header;
}
main {
grid-area: main;
}
footer {
grid-area: footer;
}
この方法は、フッターをグリッドレイアウトの最後の行に配置します。
方法 5: Sticky positioning を使用する
この方法は、CSSの position: sticky
プロパティを使用して、フッターを固定します。 これは、フッターが画面の上部にスクロールされるまでは固定し、その後はスクロール可能なコンテンツとともにスクロールされるという、方法 1 と方法 2 の間のハイブリッドな方法です。
<footer>
</footer>
footer {
position: sticky;
bottom: 0;
width: 100%;
background-color: #f0f0f0;
color: #333;
text-align: center;
padding: 10px 0;
}
方法 6: JavaScript を使用する
この方法は、JavaScriptを使用して、フッターの高さを動的に計算し、それに基づいてフッターの位置を調整します。 これは、フッターのコンテンツが可変する場合に役立ちます。
<footer>
</footer>
footer {
position: fixed;
bottom: 0;
width: 100%;
background-color: #f0f0f0;
color: #333;
text-align: center;
padding: 10px 0;
}
JavaScript:
function adjustFooter() {
const footerHeight = document.querySelector('footer').offsetHeight;
const bodyHeight = document.body.offsetHeight;
document.body.style.height = (bodyHeight - footerHeight) + 'px';
}
window.addEventListener('load', adjustFooter);
window.addEventListener('resize', adjustFooter);
この方法は、より複雑ですが、より柔軟なソリューションを提供します。
使用する方法は、要件と好みによって異なります。 フッターの高さが固定されていて、シンプルな方法が必要な場合は、方法 1 が適しています。 フッターの高さが可変である場合は、方法 2 または 方法 3 が適しています。 より新しいレイアウト機能を試してみたい場合は、方法 4 または 方法 5 を検討してください。 フッターのコンテンツが動的に変化する場合は、方法 6 が必要になる可能性があります。
どの方法を選択する場合でも、コードをテストして、Webサイトで正しく動作することを確認してください。
html css