![見出し画像](https://assets.st-note.com/production/uploads/images/88751160/rectangle_large_type_2_9537677c831f25e638b51ac358a69795.png?width=1200)
FlexBoxを使ってheaderとメインコンテンツとfooterの高さを調整する
ga実現したいこと
headerとfooterの間のコンテンツ(メインコンテンツ)の高さが低い時でも、常にheaderは画面上に、footerは画面下に固定して表示されている
メインコンテンツの要素が増えて高さが出た時は、メインコンテンツの高さに合わせてfooterが画面下に表示されている
OKパターン
![](https://assets.st-note.com/img/1665456113080-oz4S2iDOhK.png?width=1200)
![](https://assets.st-note.com/production/uploads/images/88751827/picture_pc_e790bbe95da99a649db81eb98198505c.gif?width=1200)
NGパターン
![](https://assets.st-note.com/img/1665456777676-KBGU24l6nY.png?width=1200)
![](https://assets.st-note.com/production/uploads/images/88752462/picture_pc_3d119af75e91685fc23dabe80d23801e.gif?width=1200)
実装
index.html
<div class="container">
<header>Header</header>
<main>
<div class="main">メインコンテンツ</div>
</main>
<footer>Footer</footer>
</div>
全体を囲う container には display: flex; と min-height: 100vh; を指定
メインコンテンツの部分には flex: 1; を指定
.container {
display: flex;
flex-direction: column;
min-height: 100vh;
height: auto;
width: 100%;
}
header {
height: 56px;
width: 100%;
background: gray;
}
main {
flex: 1;
width: 100%;
}
footer {
height: 80px;
width: 100%;
background: blue;
}
この記事が気に入ったらサポートをしてみませんか?