Flexboxを使ったレスポンシブな4カラムフッター。flex-wrapとflex-basisで、画面サイズに応じてカラム数が自動的に変化します。
<footer>
<div class="footer-column">
<h3>会社概要</h3>
<p>...</p>
</div>
<div class="footer-column">
<h3>サービス</h3>
<ul>...</ul>
</div>
<div class="footer-column">
<h3>サポート</h3>
<ul>...</ul>
</div>
<div class="footer-column">
<h3>お問い合わせ</h3>
<p>...</p>
</div>
</footer>
footerの直下に4つの.footer-columnを配置。各カラムには見出しとコンテンツが入ります。
footer {
display: flex;
flex-wrap: wrap; /* 折り返しを有効に */
justify-content: space-between;
padding: 20px;
}
nowrapなので、子要素が縮小されて1行に収まろうとします。レスポンシブ対応にはwrapが必須です。
.footer-column {
flex: 1 1 22%; /* grow shrink basis */
padding: 10px;
box-sizing: border-box;
border-left: 1px solid #111;
}
box-sizing: border-boxを使っていても、親のpadding分は考慮が必要です。
/* よく使う値 */
justify-content: flex-start; /* 左寄せ */
justify-content: flex-end; /* 右寄せ */
justify-content: center; /* 中央寄せ */
justify-content: space-between; /* 両端揃え(間に均等余白) */
justify-content: space-around; /* 各要素の周りに均等余白 */
justify-content: space-evenly; /* 完全に均等な余白 */
今回はspace-betweenを使用し、最初と最後の要素が端に、その間に均等な余白が入ります。
@media (max-width: 768px) {
.footer-column {
flex: 1 1 48%; /* 2カラム表示 */
}
}
@media (max-width: 480px) {
.footer-column {
flex: 1 1 100%; /* 1カラム表示 */
}
}
/* Flexbox向きの例 */
- ナビゲーションメニュー
- ボタングループ
- フッターのカラムレイアウト
- カードの横並び
/* Grid向きの例 */
- 複雑なギャラリーレイアウト
- ダッシュボード
- フォームのラベルと入力欄
- 新聞のような多列レイアウト
orderプロパティでモバイル時の表示順を変更
align-itemsで縦方向の揃え位置を調整