この記事の要点
• display: flexで親要素をFlexコンテナにする
• justify-content(主軸)とalign-items(交差軸)で配置を制御
• flex: 1で余白を均等分配、flex: 0 0 Npxで固定幅
基本構造
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
.container {
display: flex;
}
コンテナのプロパティ
flex-direction(主軸の方向)
| 値 | 説明 |
|---|
row | 左→右(デフォルト) |
row-reverse | 右→左 |
column | 上→下 |
column-reverse | 下→上 |
flex-wrap(折り返し)
| 値 | 説明 |
|---|
nowrap | 折り返さない(デフォルト) |
wrap | 折り返す |
wrap-reverse | 逆方向に折り返す |
ポイント: justify-contentは主軸方向、align-itemsは交差軸方向の配置。この2つの違いを理解することがFlexbox攻略の鍵です。
justify-content(主軸の配置)
| 値 | 説明 |
|---|
flex-start | 開始位置(デフォルト) |
flex-end | 終了位置 |
center | 中央 |
space-between | 両端揃え |
space-around | 均等配置(両端に半分の余白) |
space-evenly | 均等配置(均等な余白) |
align-items(交差軸の配置)
| 値 | 説明 |
|---|
stretch | 伸縮(デフォルト) |
flex-start | 開始位置 |
flex-end | 終了位置 |
center | 中央 |
baseline | ベースライン揃え |
align-content(複数行の配置)
| 値 | 説明 |
|---|
flex-start | 開始位置 |
flex-end | 終了位置 |
center | 中央 |
space-between | 両端揃え |
space-around | 均等配置 |
stretch | 伸縮(デフォルト) |
gap(アイテム間の余白)
| プロパティ | 説明 |
|---|
gap: 20px | 行と列の余白 |
row-gap: 20px | 行の余白 |
column-gap: 10px | 列の余白 |
gap: 20px 10px | row column |
アイテムのプロパティ
実践メモ: gapプロパティを使えばmarginハックは不要。アイテム間の余白を簡潔に指定できます。
flex(flex-grow, flex-shrink, flex-basis)
.item {
flex: 1;
flex: 1 1 auto;
flex: 0 0 200px;
}
.item {
flex-grow: 1;
flex-shrink: 0;
flex-basis: 200px;
}
align-self(個別の交差軸配置)
| 値 | 説明 |
|---|
auto | 親のalign-itemsに従う |
flex-start | 開始位置 |
flex-end | 終了位置 |
center | 中央 |
baseline | ベースライン揃え |
stretch | 伸縮 |
order(並び順)
| 値 | 説明 |
|---|
order: 0 | デフォルト |
order: -1 | 先頭に移動 |
order: 1 | 後ろに移動 |
注意: flex: 1はflex: 1 1 0%の省略形です。flex-basisが0%になるため、コンテンツ幅を考慮したい場合はflex: 1 1 autoを使いましょう。
よく使うパターン
中央揃え
.container {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
ナビゲーション
.nav {
display: flex;
justify-content: space-between;
align-items: center;
}
.nav {
display: flex;
justify-content: flex-end;
gap: 20px;
}
カードグリッド
.cards {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.card {
flex: 1 1 300px;
}
サイドバーレイアウト
.layout {
display: flex;
}
.sidebar {
flex: 0 0 250px;
}
.main {
flex: 1;
}
ポイント: flex: 0 0 Npxで固定幅、flex: 1で可変幅を作れます。サイドバー+メインのレイアウトはこの組み合わせが定番です。
フッターを下に固定
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
main {
flex: 1;
}
footer {
flex-shrink: 0;
}
等幅カラム
.columns {
display: flex;
}
.column {
flex: 1;
}
入力フォーム
.input-group {
display: flex;
}
.input-group input {
flex: 1;
}
.input-group button {
flex-shrink: 0;
}
アイコン + テキスト
.icon-text {
display: flex;
align-items: center;
gap: 8px;
}
メディアオブジェクト
.media {
display: flex;
gap: 16px;
}
.media-image {
flex-shrink: 0;
}
.media-body {
flex: 1;
}
実践メモ: 完全中央揃えはdisplay: flex; justify-content: center; align-items: center;の3行で実現。CSS Gridならplace-items: centerの1行でもOKです。
注意: flex-directionをcolumnにすると、justify-contentが垂直方向、align-itemsが水平方向になります。主軸と交差軸が入れ替わるので注意しましょう。
ショートハンド
| プロパティ | 説明 |
|---|
flex-flow: row wrap | flex-direction + flex-wrap |
flex: 1 0 auto | flex-grow + flex-shrink + flex-basis |
参考リソース
関連記事
この技術を体系的に学びたいですか?
未来学では東証プライム上場企業のITエンジニアが24時間サポート。月額24,800円から、退会金0円のオンラインIT塾です。
メールで無料相談する
← 一覧に戻る